CLI工具

    创建项目

    使用 -r 指定源

    1. # 国内拉取失败可使用gitee源
    2. kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git
    3. # 亦可使用自定义的模板
    4. kratos new helloworld -r xxx-layout.git
    5. # 同时也可以通过环境变量指定源
    6. KRATOS_LAYOUT_REPO=xxx-layout.git
    7. kratos new helloworld

    使用 -b 指定分支

    1. kratos new helloworld -b main

    使用 --nomod 添加服务, 共用 go.mod ,大仓模式

    1. kratos new helloworld
    2. cd helloworld
    3. kratos new app/user --nomod

    输出:

    1. .
    2. ├── Dockerfile
    3. ├── LICENSE
    4. ├── Makefile
    5. ├── README.md
    6. ├── api
    7. └── helloworld
    8. └── v1
    9. ├── error_reason.pb.go
    10. ├── error_reason.proto
    11. ├── greeter.pb.go
    12. ├── greeter.proto
    13. ├── greeter_grpc.pb.go
    14. └── greeter_http.pb.go
    15. ├── app
    16. └── user
    17. ├── Dockerfile
    18. ├── Makefile
    19. ├── cmd
    20. └── user
    21. ├── main.go
    22. ├── wire.go
    23. └── wire_gen.go
    24. ├── configs
    25. └── config.yaml
    26. ├── internal
    27. ├── biz
    28. ├── biz.go
    29. └── greeter.go
    30. ├── conf
    31. ├── conf.pb.go
    32. └── conf.proto
    33. ├── data
    34. ├── data.go
    35. └── greeter.go
    36. ├── server
    37. ├── grpc.go
    38. ├── http.go
    39. └── server.go
    40. └── service
    41. ├── greeter.go
    42. └── service.go
    43. └── openapi.yaml
    44. └── helloworld
    45. ├── main.go
    46. ├── wire.go
    47. ├── configs
    48. └── config.yaml
    49. ├── go.mod
    50. ├── go.sum
    51. ├── internal
    52. ├── biz
    53. ├── README.md
    54. ├── biz.go
    55. └── greeter.go
    56. ├── conf
    57. ├── conf.pb.go
    58. └── conf.proto
    59. ├── data
    60. ├── README.md
    61. ├── data.go
    62. └── greeter.go
    63. ├── server
    64. ├── grpc.go
    65. ├── http.go
    66. └── server.go
    67. └── service
    68. ├── README.md
    69. ├── greeter.go
    70. └── service.go
    71. ├── openapi.yaml
    72. └── third_party
    73. ├── README.md
    74. ├── errors
    75. └── errors.proto
    76. ├── google
    77. ├── api
    78. ├── annotations.proto
    79. ├── client.proto
    80. ├── field_behavior.proto
    81. ├── http.proto
    82. └── httpbody.proto
    83. └── protobuf
    84. └── descriptor.proto
    85. └── validate
    86. ├── README.md
    87. └── validate.proto

    添加 Proto 文件

    输出:

    api/helloworld/demo.proto

    1. syntax = "proto3";
    2. package api.helloworld;
    3. option go_package = "helloworld/api/helloworld;helloworld";
    4. option java_multiple_files = true;
    5. option java_package = "api.helloworld";
    6. service Demo {
    7. rpc CreateDemo (CreateDemoRequest) returns (CreateDemoReply);
    8. rpc UpdateDemo (UpdateDemoRequest) returns (UpdateDemoReply);
    9. rpc DeleteDemo (DeleteDemoRequest) returns (DeleteDemoReply);
    10. rpc GetDemo (GetDemoRequest) returns (GetDemoReply);
    11. rpc ListDemo (ListDemoRequest) returns (ListDemoReply);
    12. }
    13. message CreateDemoRequest {}
    14. message CreateDemoReply {}
    15. message UpdateDemoRequest {}
    16. message DeleteDemoRequest {}
    17. message DeleteDemoReply {}
    18. message GetDemoRequest {}
    19. message GetDemoReply {}
    20. message ListDemoRequest {}
    21. message ListDemoReply {}
    1. # 可以直接通过 make 命令生成
    2. make api
    3. # 或使用 kratos cli 进行生成
    4. kratos proto client api/helloworld/demo.proto

    会在proto文件同目录下生成:

    1. api/helloworld/demo.pb.go
    2. api/helloworld/demo_grpc.pb.go
    3. # 注意 http 代码只会在 proto 文件中声明了 http 时才会生成
    4. api/helloworld/demo_http.pb.go

    生成 Service 代码

    通过 proto文件,可以直接生成对应的 Service 实现代码:

    1. kratos proto server api/helloworld/demo.proto -t internal/service

    输出:
    internal/service/demo.go

    1. package service
    2. import (
    3. "context"
    4. pb "helloworld/api/helloworld"
    5. )
    6. type DemoService struct {
    7. pb.UnimplementedDemoServer
    8. }
    9. func NewDemoService() *DemoService {
    10. return &DemoService{}
    11. }
    12. func (s *DemoService) CreateDemo(ctx context.Context, req *pb.CreateDemoRequest) (*pb.CreateDemoReply, error) {
    13. return &pb.CreateDemoReply{}, nil
    14. }
    15. func (s *DemoService) UpdateDemo(ctx context.Context, req *pb.UpdateDemoRequest) (*pb.UpdateDemoReply, error) {
    16. return &pb.UpdateDemoReply{}, nil
    17. }
    18. func (s *DemoService) DeleteDemo(ctx context.Context, req *pb.DeleteDemoRequest) (*pb.DeleteDemoReply, error) {
    19. return &pb.DeleteDemoReply{}, nil
    20. }
    21. func (s *DemoService) GetDemo(ctx context.Context, req *pb.GetDemoRequest) (*pb.GetDemoReply, error) {
    22. return &pb.GetDemoReply{}, nil
    23. }
    24. func (s *DemoService) ListDemo(ctx context.Context, req *pb.ListDemoRequest) (*pb.ListDemoReply, error) {
    25. return &pb.ListDemoReply{}, nil
    26. }

    运行项目

    • 如子目录下有多个项目则出现选择菜单

    查看工具版本:

    1. kratos -v

    输出:

    1. kratos version v2.2.0

    工具升级

    将升级以下工具

    • Kratos与工具自身
    • protoc相关的生成插件
    1. kratos upgrade

    更新日志

    1. # 等同于打印 https://github.com/go-kratos/kratos/releases/latest 的版本更新日志
    2. kratos changelog
    3. # 打印指定版本更新日志
    4. kratos changelog v2.1.4
    5. # 查看自上次版本发布后的更新日志
    6. kratos changelog dev