kratos源码分析系列(1)(kvm源码解析与应用 pdf)
liuian 2025-05-09 20:03 5 浏览
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。
相关推荐
- GCI: Another key public good for international community
-
MembersofadelegationofhighschoolstudentsfromtheU.S.stateofWashingtonposeforaphotoa...
- kube on kube 实现思路分享(kube-scheduler)
-
这里的kubeonkube,是指建立K8s元集群,纳管其他业务K8s集群,通过声明式API管理集群的创建、增删节点等。参考https://github.com/kubean-i...
- China and India hold the key to a more inclusive global future
-
ByMayaMajueranLead:AsChinaandIndiamark75yearsofdiplomaticties,theircooperationcouldse...
- 日本真子公主的婚礼又要提上日程了吗?未婚夫:债务问题已解决
-
日本明仁天皇将于今年3月31日退位,德仁皇太子即将成为新一任的天皇。在平成时代最后的倒计时中,明仁天皇的孙女真子公主的婚事却又一次进入了人们的视野。(viaTheTelegraph)关注日本皇室的...
- kratos源码分析系列(1)(kvm源码解析与应用 pdf)
-
https://github.com/go-kratos/kratos是b站开源的一个微服务框架,整体来看它结合grpc生态中的grpc-gateway,以及wire依赖注入和众多常用的trace,m...
- 【2.C#基础】6.循环语句(c#循环语句例子)
-
6.循环语句当需要多次执行同一个处理时,就需要用到循环语句。一般情况下,循环的流程图如下:6.1while循环C#中的while循环语句在给定的条件为真的情况下会重复执行目标语句。格式如下:...
- 使用 Google Wire 在 Go 中进行依赖注入
-
关注点分离、松耦合系统和依赖反转原则等概念在软件工程中是众所周知的,并且在创建良好的计算机程序过程中至关重要。在本文中,我们将讨论一个同时应用了这三个原则的技术,称为依赖注入。我们将尽可能地实践,更加...
-
- 用 Golang封装你的API(golang封装dll)
-
每日分享最新,最流行的软件开发知识与最新行业趋势,希望大家能够一键三连,多多支持,跪求关注,点赞,留言。@头条创作挑战赛本文探讨了在用Golang封装你的API的过程以及几个不同的编程步骤。我做了一个非常有限的时间来证明如何为客户正在开...
-
2025-05-09 20:03 liuian
- Terraform 实战 | 万字长文(terrify是什么意思中文)
-
Terraform是什么Terraform(https://www.terraform.io/)是HashiCorp旗下的一款开源(Go语言开发)的DevOps基础架构资源管理运维工具,可...
- Go 语言入门:环境安装(go语言安装 window)
-
一、前言这里不同于其他人的Go语言入门,环境安装我向来注重配置,比如依赖包、缓存的默认目录。因为前期不弄好,后面要整理又影响这影响那的,所以就干脆写成文章,方便后期捡起。二、安装1.安装包htt...
- Go语言进阶之Go语言高性能Web框架Iris项目实战-项目结构优化EP05
-
前文再续,上一回我们完成了用户管理模块的CURD(增删改查)功能,功能层面,无甚大观,但有一个结构性的缺陷显而易见,那就是项目结构过度耦合,项目的耦合性(Coupling),也叫耦合度,进而言之,模块...
- 如何将Go项目与Docker结合实现高效部署
-
在现代软件开发中,使用Docker部署应用程序已经成为一种标准实践。本文将深入探讨如何将Go项目与Docker结合,实现高效、可靠的部署过程。通过详细的步骤和丰富的示例,你将能够迅速掌握这一流程。准备...
- 五分钟轻松熟悉一个k8s Operator应用制作
-
简介:operator是一种kubernetes的扩展形式,可以帮助用户以Kubernetes的声明式API风格自定义来管理应用及服务,operator已经成为分布式应用在k8s集群部...
- 程序员的副业秘籍!一款可以快速搭建各类系统的后台管理系统
-
系统简介这是一个基于Gin+Vue+ElementUI(或ArcoDesign、AntDesign)的系统快速开发平台,采用了前后端分离,旨在帮助用户快速完成各类系统的基础功能搭建。平...
- 使用 Go 语言开发区块链钱包的项目目录结构设计
-
在开发区块链钱包时,项目的目录结构应该清晰、模块化,确保代码的可维护性和扩展性。基于Go的惯例,结合区块链钱包的功能需求,以下是一个较为合理的目录结构示例:1.目录结构blockchain-wa...
- 一周热门
-
-
Python实现人事自动打卡,再也不会被批评
-
Psutil + Flask + Pyecharts + Bootstrap 开发动态可视化系统监控
-
一个解决支持HTML/CSS/JS网页转PDF(高质量)的终极解决方案
-
再见Swagger UI 国人开源了一款超好用的 API 文档生成框架,真香
-
【验证码逆向专栏】vaptcha 手势验证码逆向分析
-
网页转成pdf文件的经验分享 网页转成pdf文件的经验分享怎么弄
-
C++ std::vector 简介
-
python使用fitz模块提取pdf中的图片
-
《人人译客》如何规划你的移动电商网站(2)
-
Jupyterhub安装教程 jupyter怎么安装包
-
- 最近发表
-
- GCI: Another key public good for international community
- kube on kube 实现思路分享(kube-scheduler)
- China and India hold the key to a more inclusive global future
- 日本真子公主的婚礼又要提上日程了吗?未婚夫:债务问题已解决
- kratos源码分析系列(1)(kvm源码解析与应用 pdf)
- 【2.C#基础】6.循环语句(c#循环语句例子)
- 使用 Google Wire 在 Go 中进行依赖注入
- 用 Golang封装你的API(golang封装dll)
- Terraform 实战 | 万字长文(terrify是什么意思中文)
- Go 语言入门:环境安装(go语言安装 window)
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- table.render (33)
- uniapp textarea (33)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- react-admin (33)
- vscode切换git分支 (35)
- vscode美化代码 (33)
- python bytes转16进制 (35)