安装

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

工具使用

版本

查看工具版本:

  1. kratos -v

输出:

  1. kratos version v2.0.0

创建项目

通过 kratos 命令创建项目模板:

  1. kratos new helloworld

输出:

  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

添加 Proto 文件

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

输出:

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

生成 Proto 代码

可以通过 make api 直接生成,或者:

  1. kratos proto client api/helloworld/demo.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() 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. }