三種安裝方式任選其一
go get -u github.com/go-kratos/kratos/cmd/kratos/v2@latest
go install github.com/go-kratos/kratos/cmd/kratos/v2
# go 1.16版本以上需要指定版本號(hào)或使用最新版
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
git clone https://github.com/go-kratos/kratos
cd kratos
make install
通過 kratos 命令創(chuàng)建項(xiàng)目模板:
kratos new helloworld
使用 ?-r
? 指定源
# 國(guó)內(nèi)拉取失敗可使用gitee源
kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git
# 亦可使用自定義的模板
kratos new helloworld -r xxx-layout.git
# 同時(shí)也可以通過環(huán)境變量指定源
KRATOS_LAYOUT_REPO=xxx-layout.git
kratos new helloworld
使用 ?-b
? 指定分支
kratos new helloworld -b main
kratos-layout 項(xiàng)目中對(duì) proto 文件進(jìn)行了版本劃分,放在了 v1 子目錄下
kratos proto add api/helloworld/demo.proto
輸出:
api/helloworld/demo.proto
syntax = "proto3";
package api.helloworld;
option go_package = "helloworld/api/helloworld;helloworld";
option java_multiple_files = true;
option java_package = "api.helloworld";
service Demo {
rpc CreateDemo (CreateDemoRequest) returns (CreateDemoReply);
rpc UpdateDemo (UpdateDemoRequest) returns (UpdateDemoReply);
rpc DeleteDemo (DeleteDemoRequest) returns (DeleteDemoReply);
rpc GetDemo (GetDemoRequest) returns (GetDemoReply);
rpc ListDemo (ListDemoRequest) returns (ListDemoReply);
}
message CreateDemoRequest {}
message CreateDemoReply {}
message UpdateDemoRequest {}
message UpdateDemoReply {}
message DeleteDemoRequest {}
message DeleteDemoReply {}
message GetDemoRequest {}
message GetDemoReply {}
message ListDemoRequest {}
message ListDemoReply {}
# 可以直接通過 make 命令生成
make api
# 或使用 kratos cli 進(jìn)行生成
kratos proto client api/helloworld/demo.proto
會(huì)在proto文件同目錄下生成:
api/helloworld/demo.pb.go
api/helloworld/demo_grpc.pb.go
# 注意 http 代碼只會(huì)在 proto 文件中聲明了 http 時(shí)才會(huì)生成
api/helloworld/demo_http.pb.go
通過 proto文件,可以直接生成對(duì)應(yīng)的 Service 實(shí)現(xiàn)代碼:
使用 ?-t
? 指定生成目錄
kratos proto server api/helloworld/demo.proto -t internal/service
輸出:
internal/service/demo.go
package service
import (
"context"
pb "helloworld/api/helloworld"
)
type DemoService struct {
pb.UnimplementedDemoServer
}
func NewDemoService() *DemoService {
return &DemoService{}
}
func (s *DemoService) CreateDemo(ctx context.Context, req *pb.CreateDemoRequest) (*pb.CreateDemoReply, error) {
return &pb.CreateDemoReply{}, nil
}
func (s *DemoService) UpdateDemo(ctx context.Context, req *pb.UpdateDemoRequest) (*pb.UpdateDemoReply, error) {
return &pb.UpdateDemoReply{}, nil
}
func (s *DemoService) DeleteDemo(ctx context.Context, req *pb.DeleteDemoRequest) (*pb.DeleteDemoReply, error) {
return &pb.DeleteDemoReply{}, nil
}
func (s *DemoService) GetDemo(ctx context.Context, req *pb.GetDemoRequest) (*pb.GetDemoReply, error) {
return &pb.GetDemoReply{}, nil
}
func (s *DemoService) ListDemo(ctx context.Context, req *pb.ListDemoRequest) (*pb.ListDemoReply, error) {
return &pb.ListDemoReply{}, nil
}
如子目錄下有多個(gè)項(xiàng)目則出現(xiàn)選擇菜單
kratos run
查看工具版本:
kratos -v
輸出:
kratos version v2.2.0
將升級(jí)以下工具
kratos upgrade
# 等同于打印 https://github.com/go-kratos/kratos/releases/latest 的版本更新日志
kratos changelog
# 打印指定版本更新日志
kratos changelog v2.1.4
# 查看自上次版本發(fā)布后的更新日志
kratos changelog dev
任何命令下加 ?-h
? 查看幫助
kratos -h
kratos new -h
更多建議: