Usage

Installation

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

Project Creation

To create a new project:

  1. kratos new helloworld

Use -r to specify the source

  1. # If pull fails in China, you can use gitee source.
  2. kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git
  3. # You can also use custom templates
  4. kratos new helloworld -r xxx-layout.git
  5. # You can also specify the source through the environment variable
  6. KRATOS_LAYOUT_REPO=xxx-layout.git
  7. kratos new helloworld

Use -b to specify the branch

  1. kratos new helloworld -b main

Use --nomod to add services and working together using go.mod, large warehouse mode

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

Output:

  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. ├── cmd
  45. └── helloworld
  46. ├── main.go
  47. ├── wire.go
  48. └── wire_gen.go
  49. ├── configs
  50. └── config.yaml
  51. ├── go.mod
  52. ├── go.sum
  53. ├── internal
  54. ├── biz
  55. ├── README.md
  56. ├── biz.go
  57. └── greeter.go
  58. ├── conf
  59. ├── conf.pb.go
  60. └── conf.proto
  61. ├── data
  62. ├── README.md
  63. ├── data.go
  64. └── greeter.go
  65. ├── server
  66. ├── grpc.go
  67. ├── http.go
  68. └── server.go
  69. └── service
  70. ├── README.md
  71. ├── greeter.go
  72. └── service.go
  73. ├── openapi.yaml
  74. └── third_party
  75. ├── README.md
  76. ├── errors
  77. └── errors.proto
  78. ├── google
  79. ├── api
  80. ├── annotations.proto
  81. ├── client.proto
  82. ├── field_behavior.proto
  83. ├── http.proto
  84. └── httpbody.proto
  85. └── protobuf
  86. └── descriptor.proto
  87. └── validate
  88. ├── README.md
  89. └── validate.proto

Adding Proto files

  1. kratos proto add api/helloworld/demo.proto

Output:

api/helloworld/demo.proto

  1. syntax = "proto3";
  2. package api.helloworld;
  3. option go_package = "helloworld/api/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 UpdateDemoReply {}
  17. message DeleteDemoRequest {}
  18. message DeleteDemoReply {}
  19. message GetDemoRequest {}
  20. message GetDemoReply {}
  21. message ListDemoRequest {}
  22. message ListDemoReply {}

Generate Proto Codes

  1. kratos proto client api/helloworld/demo.proto

Output:

  1. api/helloworld/demo.pb.go
  2. api/helloworld/demo_grpc.pb.go
  3. # Attention: The http code will only be generated if http is declared in the proto file.
  4. api/helloworld/demo_http.pb.go

Generate Service Codes

kratos can generate the bootstrap codes from the proto file.

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

Output: 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() pb.DemoServer {
  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. }

Run project

  • If there are multiple items under the subdirectory, the selection menu appears
  1. kratos run

View Version

To show the tool version

  1. kratos -v

Output:

  1. kratos version v2.2.1

Tool upgrade

The following tools will be upgraded

  • Kratos and the tool itself
  • Protoc related build plugins
  1. kratos upgrade

Changelog

  1. # Equivalent to printing the version changelog of https://github.com/go-kratos/kratos/releases/latest
  2. kratos changelog
  3. # Print the update log of the specified version
  4. kratos changelog v2.1.4
  5. # View the changelog since the last release
  6. kratos changelog dev

View help

Add -h to any command for help

  1. kratos -h
  2. kratos new -h