Go微服务框架go-kratos实战学习01 创建项目

Reads: 2360 Edit

学习资料

kratos学习资料
https://www.cnblogs.com/jiujuan/tag/%E5%BE%AE%E6%9C%8D%E5%8A%A1/
https://www.cnblogs.com/zly-go/tag/kratos/

go-kratos v2 + gorm 实现增删改查demo
https://www.cnblogs.com/zhouqi666/p/15160796.html

https://www.cnblogs.com/ailumiyana/category/1616148.html
https://junedayday.github.io/2021/12/20/go-tip/go-tip-9/
https://www.cnblogs.com/zly-go/p/15472055.html

kratos官网文档
https://go-kratos.dev/en/docs/getting-started/start/

kratos官方代码
https://github.com/go-kratos/kratos/blob/main/README_zh.md

视频资料
https://www.bilibili.com/video/BV1t3411h7uA/?spm_id_from=333.337.search-card.all.click

Kratos

Kratos, bilibili开源的一套Go微服务框架,包含大量微服务相关框架及工具

项目初始化

需要安装好对应的依赖环境

  • go
  • protoc
  • protoc-gen-go

创建项目

开启module模式 并配置GOPROXY

https://goproxy.cn/

建议开启GO111MODULE

$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct

安装 kratos 命令工具

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

安装成功后,查看karatos 的版本

D:\quant2\bigdata\go_projects>kratos -v
kratos version v2.5.0

使用默认模板创建项目

# 使用默认模板创建项目
kratos new helloworld

# 如在国内环境拉取失败, 可 -r 指定源
kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git

(base) MacBook-Pro:go_projects xinping$ kratos new helloworld
🚀 Creating service helloworld, layout repo is https://github.com/go-kratos/kratos-layout.git, please wait a moment.

Cloning into '/Users/xinping/.kratos/repo/github.com/go-kratos/kratos-layout@main'...

CREATED helloworld/.gitignore (557 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 (290 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 (1713 bytes)
CREATED helloworld/cmd/helloworld/wire.go (607 bytes)
CREATED helloworld/cmd/helloworld/wire_gen.go (1069 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 (1236 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 (473 bytes)
CREATED helloworld/internal/data/greeter.go (835 bytes)
CREATED helloworld/internal/server/grpc.go (826 bytes)
CREATED helloworld/internal/server/http.go (830 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 (2195 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

拉去项目依赖

# 进入项目目录
cd helloworld

# 拉取项目依赖
go mod download

代码生成与运行

生成

# 生成所有proto源码、wire等等
go generate ./...

运行

第一种方式 运行Kratos Project

# 运行项目
kratos run

# 输出
INFO msg=config loaded: config.yaml format: yaml # 默认载入 configs/config.yaml 配置文件
INFO msg=[gRPC] server listening on: [::]:9000 # gRPC服务监听 9000 端口
INFO msg=[HTTP] server listening on: [::]:8000 # HTTP服务监听 8000 端口

第二种方式 运行Kratos Project

cd D:\quant2\bigdata\go_projects\helloworld\cmd\helloworld

D:\work_software\go1_17\bin\go.exe  run  main.go wire_gen.go -conf ../../configs

命令输出

INFO msg=config loaded: config.yaml format: yaml # 默认载入 configs/config.yaml 配置文件
INFO msg=[gRPC] server listening on: [::]:9000 # gRPC服务监听 9000 端口
INFO msg=[HTTP] server listening on: [::]:8000 # HTTP服务监听 8000 端口

访问以下地址有消息就代表项目正常启动了

http://127.0.0.1:8000/helloworld/kratos

http://127.0.0.1:8000/helloworld/error

5个步骤搞定protoc环境安装

https://www.cnblogs.com/erfeng/p/15622382.html

https://blog.csdn.net/m0_37322399/article/details/115409688

https://blog.csdn.net/weixin_43440680/article/details/122178381

‘protoc-gen-go‘ 不是内部或外部命令,也不是可运行的程序解决方法
https://blog.csdn.net/suwei825/article/details/123770845

kratos 项目布局

基于 kratos-layout 创建的项目,使用的 kratos new 命令:

kratos new 项目名

生成的目录结构图如下:

  .
├── 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

go Wire实践

https://zhuanlan.zhihu.com/p/449115603

整个项目架构流程图,官方的一个架构图:

650581-20220529220545005-1708774495

上面虽然对代码结构做了文字说明,但是在 internal 里,有 DDD 这个概念,相信很多人看了后,不是很明白。

先来看看 DDD 的分层架构,架构图如下:

650581-20220529220544996-1213643788

再来对比看看 internal 目录里的 biz、data、service、server、conf 这 5 个目录。

  • biz:文档里说了类似 DDD 的 domain 层,也就是 DDD 架构中的领域层。这里还定义了对业务操作的接口。业务逻辑组装。
  • data:对数据库 db,缓存 cache 的封装,并且实现 biz 中定义的接口。它将领域对象重新拿出来,这里去掉了 DDD 的基础层。
  • service:实现 api 定义的服务层,类似 DDD 的应用层。处理数据传输对象到 biz(领域实体)的转换。同时协同各类 biz 交互,不应处理复杂逻辑。
  • server:http 和 grpc 实例的创建和配置,以及注册对应的 service。

service -> biz -> data

资料参考

https://go-kratos.dev/docs/getting-started/start kratos 项目初始化
https://go-kratos.dev/docs/intro/layout kratos 项目结构
https://developers.google.com/protocol-buffers
https://github.com/protocolbuffers/protobuf
https://grpc.io
https://github.com/grpc/grpc-go

GO语言使用protobuf

Protobuf3 语法指南 
https://colobu.com/2017/03/16/Protobuf3-language-guide/

Comments

Make a comment

www.ultrapower.com ,王硕的博客,专注于研究互联网产品和技术,提供中文精品教程。 本网站与其它任何公司及/或商标无任何形式关联或合作。