kratos v2版本命令行工具使用
kratos命令行工具是什么?
kratos tool 是微服务框架 kratos 的命令行工具,提供创建模板,编译protobuf 文件,运行项目等功能。
使用
下载
1go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
查看是否安装成功
1kratos -v
2
3kratos version v2.1.3
升级
1kratos upgrade
查看帮助
1kratos --help
1Kratos: An elegant toolkit for Go microservices.
2
3Usage:
4 kratos [command]
5
6Available Commands:
7 changelog Get a kratos change log
8 completion generate the autocompletion script for the specified shell
9 help Help about any command
10 new Create a service template
11 proto Generate the proto files
12 run Run project
13 upgrade Upgrade the kratos tools
14
15Flags:
16 -h, --help help for kratos
17 -v, --version version for kratos
18
19Use "kratos [command] --help" for more information about a command.
new命令
kratos new 命令为创建一个kratos项目
参数:
-
-r
repo地址 默认为https://github.com/go-kratos/kratos-layout
-
-b
git版本 默认为main分支 -
-t
超时时间 默认为60s -
也可添加环境变量
KRATOS_LAYOUT_REPO
知道远程repo
创建一个项目
1kratos new helloworld
因为默认远程仓库地址是 github上的,在国内很容易创建失败,所以要需要设置终端或者git代理(什么是终端代理和git代理可以百度或者google一下)。
当然你也可以使用-r
知道国内仓库 我们提供一个国内镜像https://gitee.com/go-kratos/kratos-layout
。
如果嫌弃每次都要-r
指定麻烦,也可以把KRATOS_LAYOUT_REPO=https://gitee.com/go-kratos/kratos-layout
加入到path中。
1kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout
proto命令
proto命令下有 add
client
和 server
子命令
add
kratos proto add
为创建一个proto模板
1kratos proto add api/helloworld/v2/hello.proto
在目录api/helloworld/v2
下可以看到生成的文件
1syntax = "proto3";
2
3package api.helloworld.v2;
4
5option go_package = "helloworld/api/helloworld/v2;v2";
6option java_multiple_files = true;
7option java_package = "api.helloworld.v2";
8
9service Hello {
10 rpc CreateHello (CreateHelloRequest) returns (CreateHelloReply);
11 rpc UpdateHello (UpdateHelloRequest) returns (UpdateHelloReply);
12 rpc DeleteHello (DeleteHelloRequest) returns (DeleteHelloReply);
13 rpc GetHello (GetHelloRequest) returns (GetHelloReply);
14 rpc ListHello (ListHelloRequest) returns (ListHelloReply);
15}
16
17message CreateHelloRequest {}
18message CreateHelloReply {}
19
20message UpdateHelloRequest {}
21message UpdateHelloReply {}
22
23message DeleteHelloRequest {}
24message DeleteHelloReply {}
25
26message GetHelloRequest {}
27message GetHelloReply {}
28
29message ListHelloRequest {}
30message ListHelloReply {}
client
kratos proto client
为生成 Proto 代码
使用这个命令需要下载 protobuf 工具 protoc,可以在官网下载对应版本 Protobuf release版本
1kratos proto client api/helloworld/v2/
这条命令就可以编译api/helloworld/v2/
下的所有.proto
文件
如果我们需要 import 其他proto
文件 可以在命令后面加上protoc
的参数
比如
1kratos proto client api/helloworld/v2/ --proto_path=api/helloworld/v2
默认也会把 ./third_party
下import 进来 需要第三方的proto文件 可以放在这里
server
kratos proto server
为指定proto文件生成简单的service代码
参数:
-t
生成代码的位置 默认是internal/service
比如
1kratos proto server api/helloworld/v2/hello.proto -t=internal/service/hello
生成的代码
1package service
2
3import (
4 "context"
5
6 pb "helloworld/api/helloworld/v2"
7)
8
9type HelloService struct {
10 pb.UnimplementedHelloServer
11}
12
13func NewHelloService() *HelloService {
14 return &HelloService{}
15}
16
17func (s *HelloService) ListHello(ctx context.Context, req *pb.ListHelloRequest) (*pb.ListHelloReply, error) {
18 return &pb.ListHelloReply{}, nil
19}
run命令
启动服务
1kratos run