codecamp

Kratos CLI工具

安装

三种安装方式任选其一

  • go get 安装

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

  • go install 安装

go install github.com/go-kratos/kratos/cmd/kratos/v2
# go 1.16版本以上需要指定版本号或使用最新版
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest

  • 源码编译安装

git clone https://github.com/go-kratos/kratos
cd kratos
make install

创建项目

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

kratos new helloworld

使用 ​-r​ 指定源

# 国内拉取失败可使用gitee源
kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout.git
# 亦可使用自定义的模板
kratos new helloworld -r xxx-layout.git
# 同时也可以通过环境变量指定源
KRATOS_LAYOUT_REPO=xxx-layout.git
kratos new helloworld

使用 ​-b​ 指定分支

kratos new helloworld -b main

添加 Proto 文件

kratos-layout 项目中对 proto 文件进行了版本划分,放在了 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 {}

生成 Proto 代码

# 可以直接通过 make 命令生成
make api

# 或使用 kratos cli 进行生成
kratos proto client api/helloworld/demo.proto

会在proto文件同目录下生成:

api/helloworld/demo.pb.go
api/helloworld/demo_grpc.pb.go
# 注意 http 代码只会在 proto 文件中声明了 http 时才会生成
api/helloworld/demo_http.pb.go

生成 Service 代码

通过 proto文件,可以直接生成对应的 Service 实现代码:

使用 ​-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
}

运行项目

如子目录下有多个项目则出现选择菜单

kratos run 

查看版本

查看工具版本:

kratos -v

输出:

kratos version v2.2.0

工具升级

将升级以下工具

  • Kratos与工具自身
  • protoc相关的生成插件

kratos upgrade

更新日志

# 等同于打印 https://github.com/go-kratos/kratos/releases/latest 的版本更新日志
kratos changelog

# 打印指定版本更新日志
kratos changelog v2.1.4

# 查看自上次版本发布后的更新日志
kratos changelog dev

查看帮助

任何命令下加 ​-h​ 查看帮助

kratos -h
kratos new -h


Kratos 项目初始化
Kratos 插件
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Kratos 运维指南

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }