暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

kratos源码分析系列(1)

https://github.com/go-kratos/kratos是b站开源的一个微服务框架,整体来看它结合grpc生态中的grpc-gateway,以及wire依赖注入和众多常用的trace,matrix,log等中间件提供了一套微服务框架。我先尝试一下使用。

    安装

    % go install github.com/go-kratos/kratos/cmd/kratos/v2@latest

    创建demo

       % kratos new helloworld
      🚀 Creating service helloworld, layout repo is https://github.com/go-kratos/kratos-layout.git, please wait a moment.


      当前分支没有跟踪信息。
      请指定您要合并哪一个分支。
      详见 git-pull(1)。


      git pull <远程> <分支>


      如果您想要为此分支创建跟踪信息,您可以执行:


      git branch --set-upstream-to=origin/<分支> master




      ERROR: Failed to create project(exit status 1)

      这是由于国内环境拉项目模板会失败。可以换个源。

        % kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git


        🚀 Creating service helloworld, layout repo is https://gitee.com/go-kratos/kratos-layout.git, please wait a moment.


        正克隆到 '/Users/xiazemin/.kratos/repo/gitee.com/go-kratos/kratos-layout@main'...


        CREATED helloworld/.gitignore (528 bytes)
        CREATED helloworld/Dockerfile (459 bytes)
        CREATED helloworld/LICENSE (1066 bytes)
        CREATED helloworld/Makefile (2399 bytes)
        CREATED helloworld/README.md (1062 bytes)
        CREATED helloworld/api/helloworld/v1/error_reason.pb.go (4991 bytes)
        CREATED helloworld/api/helloworld/v1/error_reason.proto (289 bytes)
        CREATED helloworld/api/helloworld/v1/greeter.pb.go (8074 bytes)
        CREATED helloworld/api/helloworld/v1/greeter.proto (678 bytes)
        CREATED helloworld/api/helloworld/v1/greeter_grpc.pb.go (3560 bytes)
        CREATED helloworld/api/helloworld/v1/greeter_http.pb.go (2139 bytes)
        CREATED helloworld/cmd/helloworld/main.go (1712 bytes)
        CREATED helloworld/cmd/helloworld/wire.go (584 bytes)
        CREATED helloworld/cmd/helloworld/wire_gen.go (1068 bytes)
        CREATED helloworld/configs/config.yaml (266 bytes)
        CREATED helloworld/go.mod (990 bytes)
        CREATED helloworld/go.sum (18962 bytes)
        CREATED helloworld/internal/biz/README.md (6 bytes)
        CREATED helloworld/internal/biz/biz.go (128 bytes)
        CREATED helloworld/internal/biz/greeter.go (1235 bytes)
        CREATED helloworld/internal/conf/conf.pb.go (20782 bytes)
        CREATED helloworld/internal/conf/conf.proto (761 bytes)
        CREATED helloworld/internal/data/README.md (7 bytes)
        CREATED helloworld/internal/data/data.go (472 bytes)
        CREATED helloworld/internal/data/greeter.go (834 bytes)
        CREATED helloworld/internal/server/grpc.go (825 bytes)
        CREATED helloworld/internal/server/http.go (829 bytes)
        CREATED helloworld/internal/server/server.go (150 bytes)
        CREATED helloworld/internal/service/README.md (10 bytes)
        CREATED helloworld/internal/service/greeter.go (688 bytes)
        CREATED helloworld/internal/service/service.go (136 bytes)
        CREATED helloworld/openapi.yaml (1130 bytes)
        CREATED helloworld/third_party/README.md (14 bytes)
        CREATED helloworld/third_party/errors/errors.proto (411 bytes)
        CREATED helloworld/third_party/google/api/annotations.proto (1051 bytes)
        CREATED helloworld/third_party/google/api/client.proto (3395 bytes)
        CREATED helloworld/third_party/google/api/field_behavior.proto (3011 bytes)
        CREATED helloworld/third_party/google/api/http.proto (15140 bytes)
        CREATED helloworld/third_party/google/api/httpbody.proto (2671 bytes)
        CREATED helloworld/third_party/google/protobuf/any.proto (5909 bytes)
        CREATED helloworld/third_party/google/protobuf/api.proto (7734 bytes)
        CREATED helloworld/third_party/google/protobuf/compiler/plugin.proto (8754 bytes)
        CREATED helloworld/third_party/google/protobuf/descriptor.proto (38497 bytes)
        CREATED helloworld/third_party/google/protobuf/duration.proto (4895 bytes)
        CREATED helloworld/third_party/google/protobuf/empty.proto (2429 bytes)
        CREATED helloworld/third_party/google/protobuf/field_mask.proto (8185 bytes)
        CREATED helloworld/third_party/google/protobuf/source_context.proto (2341 bytes)
        CREATED helloworld/third_party/google/protobuf/struct.proto (3779 bytes)
        CREATED helloworld/third_party/google/protobuf/timestamp.proto (6459 bytes)
        CREATED helloworld/third_party/google/protobuf/type.proto (6126 bytes)
        CREATED helloworld/third_party/google/protobuf/wrappers.proto (4042 bytes)
        CREATED helloworld/third_party/openapi/v3/annotations.proto (2196 bytes)
        CREATED helloworld/third_party/openapi/v3/openapi.proto (22082 bytes)
        CREATED helloworld/third_party/validate/README.md (81 bytes)
        CREATED helloworld/third_party/validate/validate.proto (31270 bytes)


        🍺 Project creation succeeded helloworld
        💻 Use the following command to start the project 👇:


        $ cd helloworld
        $ go generate ./...
        $ go build -o ./bin/ ./...
        $ ./bin/helloworld -conf ./configs


        🤝 Thanks for using Kratos
        📚 Tutorial: https://go-kratos.dev/docs/getting-started/start

        可以发现模板生成成功了

           %  cd helloworld
          % go mod download

          安装wire

            % go get github.com/google/wire/cmd/wire@v0.5.0
            go get: installing executables with 'go get' in module mode is deprecated.
            To adjust and download dependencies of the current module, use 'go get -d'.
            To install using requirements of the current module, use 'go install'.
            To install ignoring the current module, use 'go install' with a version,
            like 'go install example.com/cmd@latest'.
            For more information, see https://golang.org/doc/go-get-install-deprecation
            or run 'go help get' or 'go help install'.

            生成依赖

              % go generate ./...
              wire: helloworld/cmd/helloworld: wrote Users/xiazemin/bilibili/kratos/helloworld/cmd/helloworld/wire_gen.go

              启动服务

                 % kratos run
                DEBUG msg=config loaded: config.yaml format: yaml
                INFO ts=2022-09-04T21:36:08+08:00 caller=server.go:276 service.id=xiazemindeMacBook-Pro.local service.name= service.version= trace.id= span.id= msg=[HTTP] server listening on: [::]:8000
                INFO ts=2022-09-04T21:36:08+08:00 caller=server.go:193 service.id=xiazemindeMacBook-Pro.local service.name= service.version= trace.id= span.id= msg=[gRPC] server listening on: [::]:9000
                INFO ts=2022-09-04T21:36:25+08:00 caller=greeter.go:43 service.id=xiazemindeMacBook-Pro.local service.name= service.version= trace.id= span.id= msg=CreateGreeter: kratos
                ^@^@^@^@^CINFO ts=2022-09-04T21:42:10+08:00 caller=server.go:291 service.id=xiazemindeMacBook-Pro.local service.name= service.version= trace.id= span.id= msg=[HTTP] server stopping


                INFO ts=2022-09-04T21:42:10+08:00 caller=server.go:202 service.id=xiazemindeMacBook-Pro.local service.name= service.version= trace.id= span.id= msg=[gRPC] server stopping
                INFO ts=2022-09-04T21:42:10+08:00 caller=data.go:20 service.id=xiazemindeMacBook-Pro.local service.name= service.version= trace.id= span.id= msg=closing the data resources

                测试

                   % curl 'http://127.0.0.1:8000/helloworld/kratos'
                  {"message":"Hello kratos"}

                  接着我们看下目录结构

                    % tree
                    .
                    |____cmd
                    | |____helloworld
                    | | |____wire_gen.go
                    | | |____wire.go
                    | | |____main.go
                    |____go.mod
                    |____LICENSE
                    |____Dockerfile
                    |____Makefile
                    |____internal
                    | |____biz
                    | | |____README.md
                    | | |____greeter.go
                    | | |____biz.go
                    | |____server
                    | | |____server.go
                    | | |____grpc.go
                    | | |____http.go
                    | |____service
                    | | |____service.go
                    | | |____README.md
                    | | |____greeter.go
                    | |____data
                    | | |____README.md
                    | | |____data.go
                    | | |____greeter.go
                    | |____conf
                    | | |____conf.proto
                    | | |____conf.pb.go
                    |____go.sum
                    |____README.md
                    |____third_party
                    | |____validate
                    | | |____README.md
                    | | |____validate.proto
                    | |____google
                    | | |____api
                    | | | |____client.proto
                    | | | |____http.proto
                    | | | |____annotations.proto
                    | | | |____field_behavior.proto
                    | | | |____httpbody.proto
                    | | |____protobuf
                    | | | |____timestamp.proto
                    | | | |____field_mask.proto
                    | | | |____api.proto
                    | | | |____duration.proto
                    | | | |____struct.proto
                    | | | |____wrappers.proto
                    | | | |____source_context.proto
                    | | | |____any.proto
                    | | | |____type.proto
                    | | | |____empty.proto
                    | | | |____compiler
                    | | | | |____plugin.proto
                    | | | |____descriptor.proto
                    | |____openapi
                    | | |____v3
                    | | | |____annotations.proto
                    | | | |____openapi.proto
                    | |____README.md
                    | |____errors
                    | | |____errors.proto
                    |____.gitignore
                    |____configs
                    | |____config.yaml
                    |____api
                    | |____helloworld
                    | | |____v1
                    | | | |____greeter_http.pb.go
                    | | | |____error_reason.proto
                    | | | |____error_reason.pb.go
                    | | | |____greeter.pb.go
                    | | | |____greeter.proto
                    | | | |____greeter_grpc.pb.go
                    |____openapi.yaml

                    具体分层含义可以参考官方文档:https://go-kratos.dev/docs/intro/layout

                        .
                      ├── Dockerfile
                      ├── LICENSE
                      ├── Makefile
                      ├── README.md
                      ├── api // 下面维护了微服务使用的proto文件以及根据它们所生成的go文件
                      │   └── helloworld
                      │   └── v1
                      │   ├── error_reason.pb.go
                      │   ├── error_reason.proto
                      │   ├── error_reason.swagger.json
                      │   ├── greeter.pb.go
                      │   ├── greeter.proto
                      │   ├── greeter.swagger.json
                      │   ├── greeter_grpc.pb.go
                      │   └── greeter_http.pb.go
                      ├── cmd // 整个项目启动的入口文件
                      │   └── server
                      │   ├── main.go
                      │   ├── wire.go // 我们使用wire来维护依赖注入
                      │   └── wire_gen.go
                      ├── configs // 这里通常维护一些本地调试用的样例配置文件
                      │   └── config.yaml
                      ├── generate.go
                      ├── go.mod
                      ├── go.sum
                      ├── internal // 该服务所有不对外暴露的代码,通常的业务逻辑都在这下面,使用internal避免错误引用
                      │ ├── biz // 业务逻辑的组装层,类似 DDD 的 domain 层,data 类似 DDD 的 repo,而 repo 接口在这里定义,使用依赖倒置的原则。
                      │   │   ├── README.md
                      │   │   ├── biz.go
                      │   │   └── greeter.go
                      │   ├── conf // 内部使用的config的结构定义,使用proto格式生成
                      │   │   ├── conf.pb.go
                      │   │   └── conf.proto
                      │   ├── data // 业务数据访问,包含 cache、db 等封装,实现了 biz 的 repo 接口。我们可能会把 data 与 dao 混淆在一起,data 偏重业务的含义,它所要做的是将领域对象重新拿出来,我们去掉了 DDD 的 infra层。
                      │   │   ├── README.md
                      │   │   ├── data.go
                      │   │   └── greeter.go
                      │   ├── server // http和grpc实例的创建和配置
                      │   │   ├── grpc.go
                      │   │   ├── http.go
                      │   │   └── server.go
                      │   └── service // 实现了 api 定义的服务层,类似 DDD 的 application 层,处理 DTO 到 biz 领域实体的转换(DTO -> DO),同时协同各类 biz 交互,但是不应处理复杂逻辑
                      │   ├── README.md
                      │   ├── greeter.go
                      │   └── service.go
                      └── third_party // api 依赖的第三方proto
                      ├── README.md
                      ├── google
                      │   └── api
                      │   ├── annotations.proto
                      │   ├── http.proto
                      │   └── httpbody.proto
                      └── validate
                      ├── README.md
                      └── validate.proto

                      首先看下api目录它以api/helloworld/v1/greeter.proto为模板生成了对应的grpc pb文件和http pb文件,前者是通过proto-gen-go生成的,后者是通过grpc gateway生成的。

                              我们从main函数开始读下这个模板 cmd/helloworld/main.go核心代码下面两行

                        app, cleanup, err := wireApp(bc.Server, bc.Data, logger)
                        if err := app.Run(); err != nil {

                        wireApp进行了依赖注入,看下wire.go里面声明的依赖

                          func wireApp(*conf.Server, *conf.Data, log.Logger) (*kratos.App, func(), error) {
                          panic(wire.Build(server.ProviderSet, data.ProviderSet, biz.ProviderSet, service.ProviderSet, newApp))
                          }

                          最后一个参数newApp函数是在main.go里声明的。它new里一个kratos.App对象



                            func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App {
                            return kratos.New(
                            kratos.ID(id),
                            kratos.Name(Name),
                            kratos.Version(Version),
                            kratos.Metadata(map[string]string{}),
                            kratos.Logger(logger),
                            kratos.Server(
                            gs,
                            hs,
                            ),
                            )
                            }

                            最后就是通过app.Run启动server进行端口监听的。具体代码实现在github.com/go-kratos/kratos/v2@v2.4.1/app.go中,可以看到它时结合waitGroup和errorGroup来控制多个server的起动和停止的。

                              func (a *App) Run() error {
                              wg := sync.WaitGroup{}
                              for _, srv := range a.opts.servers {
                              srv := srv
                              eg.Go(func() error {
                              <-ctx.Done() // wait for stop signal
                              stopCtx, cancel := context.WithTimeout(NewContext(a.opts.ctx, a), a.opts.stopTimeout)
                              defer cancel()
                              return srv.Stop(stopCtx)
                              })
                              wg.Add(1)
                              eg.Go(func() error {
                              wg.Done()
                              return srv.Start(NewContext(a.opts.ctx, a))
                              })
                              }
                              wg.Wait()

                              可以看到核心代码是通过waitGroup等待所有的的server停止服务。但是对于每一个服务通过errorGroup来等待当前服务的超时或者完成服务正常结束的。

                              以上就是main服务的整体流程。下一讲我们分析依赖注入的server,biz,data和serverice。


                              文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                              评论