kratos 学习笔记 (二)
一.项目初始化
环境准备
首先,您需要安装好对应的依赖环境,以及工具:
go
protoc
protoc-gen-go
建议开启GO111MODULE go env -w GO111MODULE=on
1.kratos 命令工具
kratos 是与 Kratos 框架配套的脚手架工具,kratos 能够
通过模板快速创建项目
快速创建与生成 protoc 文件
使用开发过程中常用的命令
极大提高开发效率,减轻心智负担
详细使用请参照 CLI工具
为使接下来的步骤能够顺利进行,需要 安装 kratos 命令工具
2.创建项目
$ 使用默认模板创建项目
kratos new helloworld
$ 如在国内环境拉取失败, 可 -r 指定源
kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git
$ 进入项目目录
cd helloworld
$ 拉取项目依赖
go mod download
如果拉取依赖遇到网络问题,建议 配置GOPROXY
3.代码生成与运行
生成
$ 生成所有proto源码、wire等等
go generate ./...
运行
$ 运行项目
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 端口
4.测试接口
测试HTTP接口
相关逻辑代码位于 internal/service/greeter.go
curl ‘http://127.0.0.1:8000/helloworld/kratos’
$ 输出:
{
"message": "Hello kratos"
}
curl ‘http://127.0.0.1:8000/helloworld/error’
$ 输出
{
"code": 404,
"reason": "USER_NOT_FOUND",
"message": "user not found: error",
"metadata": {}
}
5.项目模板
Kratos 通过 Git 仓库进行模板管理,创建项目时通过拉取模板进行初始化。对应模板地址:
⭐ 项目布局详解 Go工程化 - Project Layout 最佳实践
二,CLI工具
1.安装
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
2.创建项目
通过 kratos 命令创建项目模板:
kratos new helloworld
使用 -r
指定源
# 国内拉取失败可使用gitee源
kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git
# 亦可使用自定义的模板
kratos new helloworld -r xxx-layout.git
# 同时也可以通过环境变量指定源
KRATOS_LAYOUT_REPO=xxx-layout.git
kratos new helloworld
使用 -b
指定分支
kratos new helloworld -b main
使用 --nomod
添加服务,共用 go.mod
,大仓模式
kratos new helloworld
cd helloworld
kratos new app/user --nomod
输出:
.
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── api
│ └── helloworld
│ └── v1
│ ├── error_reason.pb.go
│ ├── error_reason.proto
│ ├── greeter.pb.go
│ ├── greeter.proto
│ ├── greeter_grpc.pb.go
│ └── greeter_http.pb.go
├── app
│ └── user
│ ├── Dockerfile
│ ├── Makefile
│ ├── cmd
│ │ └── user
│ │ ├── main.go
│ │ ├── wire.go
│ │ └── wire_gen.go
│ ├── configs
│ │ └── config.yaml
│ ├── internal
│ │ ├── biz
│ │ │ ├── biz.go
│ │ │ └── greeter.go
│ │ ├── conf
│ │ │ ├── conf.pb.go
│ │ │ └── conf.proto
│ │ ├── data
│ │ │ ├── data.go
│ │ │ └── greeter.go
│ │ ├── server
│ │ │ ├── grpc.go
│ │ │ ├── http.go
│ │ │ └── server.go
│ │ └── service
│ │ ├── greeter.go
│ │ └── service.go
│ └── openapi.yaml
├── cmd
│ └── helloworld
│ ├── main.go
│ ├── wire.go
│ └── wire_gen.go
├── configs
│ └── config.yaml
├── go.mod
├── go.sum
├── internal
│ ├── biz
│ │ ├── README.md
│ │ ├── biz.go
│ │ └── greeter.go
│ ├── conf
│ │ ├── conf.pb.go
│ │ └── conf.proto
│ ├── data
│ │ ├── README.md
│ │ ├── data.go
│ │ └── greeter.go
│ ├── server
│ │ ├── grpc.go
│ │ ├── http.go
│ │ └── server.go
│ └── service
│ ├── README.md
│ ├── greeter.go
│ └── service.go
├── openapi.yaml
└── third_party
├── README.md
├── errors
│ └── errors.proto
├── google
│ ├── api
│ │ ├── annotations.proto
│ │ ├── client.proto
│ │ ├── field_behavior.proto
│ │ ├── http.proto
│ │ └── httpbody.proto
│ └── protobuf
│ └── descriptor.proto
└── validate
├── README.md
└── validate.proto
3.添加 Proto 文件
kratos-layout 项目中对 proto 文件进行了版本划分,放在了 v1 子目录下
kratos proto add api/helloworld/demo.proto
输出:
api/helloworld/demo.proto
syntax="proto3";
package api.helloworld;
option go_package ="helloworld/api/helloworld;helloworld";
option java_multiple_files =true;
option java_package ="api.helloworld";
serviceDemo{
rpcCreateDemo(CreateDemoRequest)returns(CreateDemoReply);
rpcUpdateDemo(UpdateDemoRequest)returns(UpdateDemoReply);
rpcDeleteDemo(DeleteDemoRequest)returns(DeleteDemoReply);
rpcGetDemo(GetDemoRequest)returns(GetDemoReply);
rpcListDemo(ListDemoRequest)returns(ListDemoReply);
}
messageCreateDemoRequest{}
messageCreateDemoReply{}
messageUpdateDemoRequest{}
messageUpdateDemoReply{}
messageDeleteDemoRequest{}
messageDeleteDemoReply{}
messageGetDemoRequest{}
messageGetDemoReply{}
messageListDemoRequest{}
messageListDemoReply{}
4.生成 Proto 代码
# 可以直接通过 make 命令生成
make api
# 或使用 kratos cli 进行生成
kratos proto client api/helloworld/demo.proto
会在proto文件同目录下生成:
api/helloworld/demo.pb.go
api/helloworld/demo_grpc.pb.go
# 注意 http 代码只会在 proto 文件中声明了 http 时才会生成
api/helloworld/demo_http.pb.go
5.生成 Service 代码
通过 proto 文件,可以直接生成对应的 Service 实现代码:
使用 -t
指定生成目录
kratos proto server api/helloworld/demo.proto -t internal/service
输出:
internal/service/demo.go
package service
import(
"context"
pb "helloworld/api/helloworld"
)
type DemoService struct{
pb.UnimplementedDemoServer
}
funcNewDemoService()*DemoService {
return&DemoService{}
}
func(s *DemoService)CreateDemo(ctx context.Context, req *pb.CreateDemoRequest)(*pb.CreateDemoReply,error){
return&pb.CreateDemoReply{},nil
}
func(s *DemoService)UpdateDemo(ctx context.Context, req *pb.UpdateDemoRequest)(*pb.UpdateDemoReply,error){
return&pb.UpdateDemoReply{},nil
}
func(s *DemoService)DeleteDemo(ctx context.Context, req *pb.DeleteDemoRequest)(*pb.DeleteDemoReply,error){
return&pb.DeleteDemoReply{},nil
}
func(s *DemoService)GetDemo(ctx context.Context, req *pb.GetDemoRequest)(*pb.GetDemoReply,error){
return&pb.GetDemoReply{},nil
}
func(s *DemoService)ListDemo(ctx context.Context, req *pb.ListDemoRequest)(*pb.ListDemoReply,error){
return&pb.ListDemoReply{},nil
}
6.运行项目
- 如子目录下有多个项目则出现选择菜单
kratos run
7.查看版本
查看工具版本:
kratos -v
输出:
kratos version v2.2.0
8.工具升级
将升级以下工具
- Kratos与工具自身
- protoc相关的生成插件
kratos upgrade
9.更新日志
# 等同于打印 https://github.com/go-kratos/kratos/releases/latest 的版本更新日志
kratos changelog
# 打印指定版本更新日志
kratos changelog v2.1.4
# 查看自上次版本发布后的更新日志
kratos changelog dev
10.查看帮助
任何命令下加 -h
查看帮助
kratos -h
kratos new -h
三,插件
1.插件的分类
Kratos提供了一系列插件帮助您增强微服务的功能、或与其它系统进行集成。服务发现&服务注册
2. 日志
3.配置中心
4.监控告警
5.API文档
四,代码示例
Kratos提供了丰富的示例代码/项目供参考
1. 组件使用
2.配置
3.服务发现&服务注册
- etcd 使用 etcd 插件,在 server 端进行服务注册和在 client 端进行服务注册的示例
- nacos 使用 nacos 插件,在 server 端进行服务注册和在 client 端进行服务注册的示例
- consul 使用 consul 插件,在 server 端进行服务注册和在 client 端进行服务注册的示例
- zookeeper 使用 zookeeper 插件,在 server 端进行服务注册和在 client 端进行服务注册的示例
4. HTTP
- cors 跨域设置示例
- gin 将 gin 作为 router 集成进 Kratos 项目的示例
- mux 将 mux 作为 router 集成进 Kratos 项目的示例
- echo 将 echo 作为 router 集成进 Kratos 项目的示例
- static 通过 HTTP 提供静态文件服务的示例
- upload 通过 HTTP 上传文件的示例
- redirect 重定向的示例
- middleware 在路由中使用中间件的示例
- errors 使用统一的错误处理进行错误响应的示例
5. RPC
- helloworld 分别使用 HTTP 和 gRPC 进行远程调用的样例
6.Trace
- traces 使用 Jaeger 对两个服务进行分布式追踪的样例
7.WebSocket
- ws 提供 WebSocket 接口的样例
8.鉴权
- jwt 在 HTTP、gRPC 中使用JWT进行鉴权的示例
9.日志
10.其他
- i18n 国际化支持的示例
- header 请求头处理示例
- selector 选择器示例,可通过负载均衡和 Filter 进行选择
- swagger 使用 Swagger 插件自动生成接口文档并提供在线服务的示例