Usage

Installation

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

Tool Usage

Version

To show the version

  1. kratos -v

Output:

  1. kratos version v2.0.0

Project Creation

To create a new project:

  1. kratos new helloworld

Output:

  1. helloworld
  2. |____api
  3. | |____helloworld
  4. | | |____v1
  5. | | | |____helloworld_grpc.pb.go
  6. | | | |____helloworld.proto
  7. | | | |____helloworld.pb.go
  8. | | | |____helloworld_http.pb.go
  9. | | |____errors
  10. | | | |____helloworld_errors.pb.go
  11. | | | |____helloworld.proto
  12. | | | |____helloworld.pb.go
  13. |____cmd
  14. | |____helloworld
  15. | | |____main.go
  16. |____internal
  17. | |____biz
  18. | | |____README.md
  19. | |____service
  20. | | |____README.md
  21. | | |____greeter.go
  22. | |____data
  23. | | |____README.md
  24. |____README.md
  25. |____Makefile
  26. |____LICENSE
  27. |____go.mod
  28. |____go.sum

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. 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. }