go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
通过 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.gitkratos new helloworld
使用 -b
指定分支
kratos new helloworld -b main
使用 --nomod
添加服务,共用 go.mod
,大仓模式
kratos new helloworldcd helloworldkratos 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
kratos-layout 项目中对 proto 文件进行了版本划分,放在了 v1 子目录下
kratos proto add api/helloworld/v1/demo.proto
输出:
api/helloworld/v1/demo.proto
syntax = "proto3";
package api.helloworld.v1;
option go_package = "helloworld/api/helloworld/v1;v1";option java_multiple_files = true;option java_package = "api.helloworld.v1";
service Demo { rpc CreateDemo (CreateDemoRequest) returns (CreateDemoReply); rpc UpdateDemo (UpdateDemoRequest) returns (UpdateDemoReply); rpc DeleteDemo (DeleteDemoRequest) returns (DeleteDemoReply); rpc GetDemo (GetDemoRequest) returns (GetDemoReply); rpc ListDemo (ListDemoRequest) returns (ListDemoReply);}
message CreateDemoRequest {}message CreateDemoReply {}
message UpdateDemoRequest {}message UpdateDemoReply {}
message DeleteDemoRequest {}message DeleteDemoReply {}
message GetDemoRequest {}message GetDemoReply {}
message ListDemoRequest {}message ListDemoReply {}
# 可以直接通过 make 命令生成make api
# 或使用 kratos cli 进行生成kratos proto client api/helloworld/v1/demo.proto
会在 proto 文件同目录下生成:
api/helloworld/v1/demo.pb.goapi/helloworld/v1/demo_grpc.pb.go# 注意 http 代码只会在 proto 文件中声明了 http 时才会生成api/helloworld/v1/demo_http.pb.go
通过 proto 文件,可以直接生成对应的 Service 实现代码:
使用 -t
指定生成目录
kratos proto server api/helloworld/v1/demo.proto -t internal/service
输出:
internal/service/demo.go
package service
import ( "context"
pb "helloworld/api/helloworld")
type DemoService struct { pb.UnimplementedDemoServer}
func NewDemoService() *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}
kratos run
查看工具版本:
kratos -v
输出:
kratos version v2.2.0
将升级以下工具
kratos upgrade
# 等同于打印 https://github.com/go-kratos/kratos/releases/latest 的版本更新日志kratos changelog
# 打印指定版本更新日志kratos changelog v2.1.4
# 查看从 latest 版本发布后至今的更新日志kratos changelog dev
任何命令下加 -h
查看帮助
kratos -hkratos new -h