ego

package module
v0.6.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2021 License: MIT Imports: 31 Imported by: 82

README

EGO

All Contributors Go Go Report Card codecov goproxy.cn Release Example Doc

1 帮助文档

https://ego.gocn.vip

2 介绍

EGO是一个集成里各种工程实践的框架。通过组件化的设计模式,保证了业务方能够统一的调用方式启动各种组件

使用EGO的优势

  • 配置化驱动组件
  • 屏蔽底层组件启动细节
  • 微服务组件的可观测、可治理
  • 可插拔的Ego-Component组件
  • Fail Fast理念和错误友好提示

2.1 提升组件熟练度

我们工程师要想提升组件熟练度,首先要大量看开源组件文档和代码,然后坚持长时间使用,才能形成肌肉记忆,提升我们做业务的速度。而这一切所投入的时间和精力是非常大的。

要减少这个投入成本,让更多开发者使用好优秀的开源组件,EGO的做法是标准化所有开源组件,对其做一层封装,统一各种行为。

  • 统一组件文件名
  • 统一组件配置参数
  • 统一组件调用API
  • 统一组件错误行为
  • 统一组件监控行为

让人掌握了一种组件,就可以举一反三使用其他组件。

2.2 提升故障排查效率

  • 统一错误码
  • 组件错误、慢响应、链路、常规请求拦截器埋点(服务端、客户端均会拦截)
  • 收敛错误字段
  • 注入组件关键信息:代码行号、配置名、目标地址、耗时、请求数据、响应数据
  • 调试阶段,错误高亮、格式化友好提示
  • 调试阶段,组件内置debug拦截器

2.3 自动生成重复代码

  • 生成代码,配置、数据解析、模版分离
  • 不依赖于语言,构建项目代码
  • 利用Go1.16特性embed,启动web,生成代码
  • 项目地址: https://github.com/gotomicro/egoctl

3 EGO的定义

3.1 框架分层

我们框架分为三个层次

  • 核心层提供配置、日志、监控和链路,是其他组件的基石
  • 组件层提供客户端、服务端、任务端里的各种组件
  • 胶水层控制了各种组件的生命周期,错误处理

3.2 架构图

3.3 生命周期

3.4 组件分层

我们认为一切均是组件,我们将组件分为四个部分:

  • Container 处理组件类型、组件配置、组件启动
  • Config 配置参数
  • Component 组件的调用方法
  • Options 配置和组件可选项

4 功能

组件名称 代码 例子 文档
HTTP服务 代码 例子 文档
gRPC服务 代码 例子 文档
治理服务 代码 例子 文档
短时任务 代码 例子 文档
常规定时任务 代码 例子 文档
分布式定时任务 文档
调用HTTP 代码 例子 文档
直连调用gRPC 代码 例子 文档
通过etcd调用gRPC 例子 文档
通过k8s调用gRPC 例子 文档
调用MySQL 代码 例子 文档
调用Redis 代码 例子 文档
调用Redis分布式锁 代码
调用Mongo 代码
调用Kafka 代码
调用ETCD 代码
调用K8S 代码
调用Oauth2 代码
调用Wechat 代码
调用Dingtalk 代码
调用Jira 代码

5 特性介绍

  • 配置驱动 所有组件启动方式为组件名称.Load("配置名称").Build(),可以创建一个组件实例。如以下http serveregin是组件名称,server.http是配置名称
egin.Load("server.http").Build()
  • 友好的debug 通过开启debug配置和命令行的export EGO_DEBUG=true, 我们可以在测试环境里看到所有组件的请求里的行号、配置名、请求地址、耗时、请求数据、响应数据

并且使用Goland同学,可以直接通过行号点击到对应的代码路径(gRPC、HTTP客户端支持行号)

6 Quick Start

6.1 HelloWorld

配置

[server.http]
    port = 9001
    host = "0.0.0.0"

代码

package main
import (
   "github.com/gin-gonic/gin"
   "github.com/gotomicro/ego"
   "github.com/gotomicro/ego/core/elog"
   "github.com/gotomicro/ego/server"
   "github.com/gotomicro/ego/server/egin"
)
//  export EGO_DEBUG=true && go run main.go --config=config.toml
func main() {
   if err := ego.New().Serve(func() *egin.Component {
      server := egin.Load("server.http").Build()
      server.GET("/hello", func(ctx *gin.Context) {
         ctx.JSON(200, "Hello EGO")
         return
      })
      return server
   }()).Run(); err != nil {
      elog.Panic("startup", elog.FieldErr(err))
   }
}

6.2 使用命令行运行

export EGO_DEBUG=true # 默认日志输出到logs目录,开启dev后日志输出到终端
go run main.go --config=config.toml

6.3 如下所示

图片

这个时候我们可以发送一个指令,得到如下结果

➜  helloworld git:(master) ✗ curl http://127.0.0.1:9001/hello
"Hello Ego"%  

6.4 更加友好的包编译

使用scripts文件夹里的包编译,可以看到优雅的version提示

图片

7 更新日志

Releases

8 加入我们

加入我们,请在验证信息里添加ego关键字

image

Contributors

Thanks for these wonderful people:


askuy

Wei Zheng

shaoyuan

Panda

刘文哲

zhangjunjun

devincd

Ming Deng

Angelia

Wbofeng

clannadxr

Link Duan

Costa

MEX7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ego added in v0.4.0

type Ego struct {
	// contains filtered or unexported fields
}

Ego 分为三大部分 第一部分 系统数据:生命周期,配置前缀,锁,日志,错误 第二部分 运行程序:系统初始化函数,用户初始化函数,服务,定时任务,短时任务 第三部分 可选方法:是否悬挂,注册中心,运行停止前清理,运行停止后清理

func New

func New(options ...Option) *Ego

New new Ego

func (*Ego) Cron added in v0.4.0

func (e *Ego) Cron(w ...ecron.Ecron) *Ego

Cron 设置定时任务

func (*Ego) Invoker added in v0.4.0

func (e *Ego) Invoker(fns ...func() error) *Ego

Invoker 传入所需要的函数

func (*Ego) Job added in v0.4.0

func (e *Ego) Job(runners ...ejob.Ejob) *Ego

Job 设置短时任务

func (*Ego) Registry added in v0.4.0

func (e *Ego) Registry(reg eregistry.Registry) *Ego

Registry 设置注册中心

func (*Ego) Run added in v0.4.0

func (e *Ego) Run() error

Run 运行程序

func (*Ego) Serve added in v0.4.0

func (e *Ego) Serve(s ...server.Server) *Ego

Serve 设置服务

func (*Ego) Stop added in v0.4.0

func (e *Ego) Stop(ctx context.Context, isGraceful bool) (err error)

Stop 停止程序

type Option

type Option func(a *Ego)

Option 可选项

func WithAfterStopClean

func WithAfterStopClean(fns ...func() error) Option

WithAfterStopClean 设置运行后清理

func WithBeforeStopClean

func WithBeforeStopClean(fns ...func() error) Option

WithBeforeStopClean 设置运行前清理

func WithConfigPrefix

func WithConfigPrefix(configPrefix string) Option

WithConfigPrefix 设置配置前缀

func WithDisableBanner

func WithDisableBanner(disableBanner bool) Option

WithDisableBanner 禁止banner

func WithDisableFlagConfig added in v0.4.3

func WithDisableFlagConfig(disableFlagConfig bool) Option

WithDisableFlagConfig 禁止config

func WithHang

func WithHang(flag bool) Option

WithHang 是否允许系统悬挂起来,0 表示不悬挂, 1 表示悬挂。目的是一些脚本操作的时候,不想主线程停止

func WithShutdownSignal added in v0.3.0

func WithShutdownSignal(signals ...os.Signal) Option

WithShutdownSignal 设置停止信号量

func WithStopTimeout added in v0.3.0

func WithStopTimeout(timeout time.Duration) Option

WithStopTimeout 设置停止的超时时间

Directories

Path Synopsis
client
cmd
protoc-gen-go-errors
MIT License Copyright (c) 2020 go-kratos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
MIT License Copyright (c) 2020 go-kratos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
core
examples
internal
task

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL