百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

kratos源码分析系列(1)(kvm源码解析与应用 pdf)

liuian 2025-05-09 20:03 13 浏览


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。

相关推荐

Optional是个好东西,如果用错了就太可惜了

原文出处:https://xie.infoq.cn/article/e3d1f0f4f095397c44812a5be我们都知道,在Java8新增了一个类-Optional,主要是用来解决程...

IDEA建议:不要在字段上使用@Autowire了!

在使用IDEA写Spring相关的项目的时候,在字段上使用@Autowired注解时,总是会有一个波浪线提示:Fieldinjectionisnotrecommended.纳尼?我天天用,咋...

Spring源码|Spring实例Bean的方法

Spring实例Bean的方法,在AbstractAutowireCapableBeanFactory中的protectedBeanWrappercreateBeanInstance(String...

Spring技巧:深入研究Java 14和SpringBoot

在本期文章中,我们将介绍Java14中的新特性及其在构建基于SpringBoot的应用程序中的应用。开始,我们需要使用Java的最新版本,也是最棒的版本,Java14,它现在还没有发布。预计将于2...

Java开发200+个学习知识路线-史上最全(框架篇)

1.Spring框架深入SpringIOC容器:BeanFactory与ApplicationContextBean生命周期:实例化、属性填充、初始化、销毁依赖注入方式:构造器注入、Setter注...

年末将至,Java 开发者必须了解的 15 个Java 顶级开源项目

专注于Java领域优质技术,欢迎关注作者:SnailClimbStar的数量统计于2019-12-29。1.JavaGuideGuide哥大三开始维护的,目前算是纯Java类型项目中Sta...

字节跨平台框架 Lynx 开源:一个 Web 开发者的原生体验

最近各大厂都在开源自己的跨平台框架,前脚腾讯刚宣布计划四月开源基于Kotlin的跨平台框架「Kuikly」,后脚字节跳动旧开源了他们的跨平台框架「Lynx」,如果说Kuikly是一个面向...

我要狠狠的反驳“公司禁止使用Lombok”的观点

经常在其它各个地方在说公司禁止使用Lombok,我一直不明白为什么不让用,今天看到一篇文章列举了一下“缺点”,这里我只想狠狠地反驳,看到列举的理由我竟无言以对。原文如下:下面,结合我自己使用Lomb...

SpringBoot Lombok使用详解:从入门到精通(注解最全)

一、Lombok概述与基础使用1.1Lombok是什么Lombok是一个Java库,它通过注解的方式自动生成Java代码(如getter、setter、toString等),从而减少样板代码的编写,...

Java 8之后的那些新特性(六):记录类 Record Class

Java是一门面向对象的语言,而对于面向对象的语言中,一个众所周知的概念就是,对象是包含属性与行为的。比如HR系统中都会有雇员的概念,那雇员会有姓名,ID身份,性别等,这些我们称之为属性;而雇员同时肯...

为什么大厂要求安卓开发者掌握Kotlin和Jetpack?优雅草卓伊凡

为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡一、Kotlin:Android开发的现代语言选择1.1Kotlin是什么?Kotlin是由...

Kotlin这5招太绝了!码农秒变优雅艺术家!

Kotlin因其简洁性、空安全性和与Java的无缝互操作性而备受喜爱。虽然许多开发者熟悉协程、扩展函数和数据类等特性,但还有一些鲜为人知的特性可以让你的代码从仅仅能用变得真正优雅且异常简洁。让我们来看...

自行部署一款免费高颜值的IT资产管理系统-咖啡壶chemex

在运维时,ICT资产太多怎么办,还是用excel表格来管理?效率太低,也不好多人使用。在几个IT资产管理系统中选择比较中,最终在Snipe-IT和chemex间选择了chemex咖啡壶。Snip...

PHP对接百度语音识别技术(php对接百度语音识别技术实验报告)

引言在目前的各种应用场景中,语音识别技术已经越来越常用,并且其应用场景正在不断扩大。百度提供的语音识别服务允许用户通过简单的接口调用,将语音内容转换为文本。本文将通过PHP语言集成百度的语音识别服务,...

知识付费系统功能全解析(知识付费项目怎么样)

开发知识付费系统需包含核心功能模块,确保内容变现、用户体验及运营管理需求。以下是完整功能架构:一、用户端功能注册登录:手机号/邮箱注册,第三方登录(微信、QQ)内容浏览:分类展示课程、文章、音频等付费...