kratos v2版本命令行工具使用


使用

下載

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

查看是否安裝成功

kratos -v

kratos version v2.1.3

升級

kratos upgrade

查看幫助

kratos --help
Kratos: An elegant toolkit for Go microservices.

Usage:
  kratos [command]

Available Commands:
  changelog   Get a kratos change log
  completion  generate the autocompletion script for the specified shell
  help        Help about any command
  new         Create a service template
  proto       Generate the proto files
  run         Run project
  upgrade     Upgrade the kratos tools

Flags:
  -h, --help      help for kratos
  -v, --version   version for kratos

Use "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

創建一個項目

kratos 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中。

kratos new helloworld -r https://gitee.com/go-kratos/kratos-layout

proto命令

proto命令下有 add clientserver子命令

add

kratos proto add 為創建一個proto模板

kratos proto add api/helloworld/v2/hello.proto

在目錄api/helloworld/v2 下可以看到生成的文件

syntax = "proto3";

package api.helloworld.v2;

option go_package = "helloworld/api/helloworld/v2;v2";
option java_multiple_files = true;
option java_package = "api.helloworld.v2";

service Hello {
    rpc CreateHello (CreateHelloRequest) returns (CreateHelloReply);
    rpc UpdateHello (UpdateHelloRequest) returns (UpdateHelloReply);
    rpc DeleteHello (DeleteHelloRequest) returns (DeleteHelloReply);
    rpc GetHello (GetHelloRequest) returns (GetHelloReply);
    rpc ListHello (ListHelloRequest) returns (ListHelloReply);
}

message CreateHelloRequest {}
message CreateHelloReply {}

message UpdateHelloRequest {}
message UpdateHelloReply {}

message DeleteHelloRequest {}
message DeleteHelloReply {}

message GetHelloRequest {}
message GetHelloReply {}

message ListHelloRequest {}
message ListHelloReply {}

client

kratos proto client 為生成 Proto 代碼

使用這個命令需要下載 protobuf 工具 protoc,可以在官網下載對應版本 Protobuf release版本

kratos proto client api/helloworld/v2/

這條命令就可以編譯api/helloworld/v2/下的所有.proto文件

如果我們需要 import 其他proto文件 可以在命令后面加上protoc的參數

比如

kratos proto client api/helloworld/v2/ --proto_path=api/helloworld/v2

默認也會把 ./third_party 下import 進來 需要第三方的proto文件 可以放在這里

server

kratos proto server為指定proto文件生成簡單的service代碼

參數:

  • -t 生成代碼的位置 默認是internal/service

比如

kratos proto server api/helloworld/v2/hello.proto -t=internal/service/hello

生成的代碼

package service

import (
    "context"

    pb "helloworld/api/helloworld/v2"
)

type HelloService struct {
    pb.UnimplementedHelloServer
}

func NewHelloService() *HelloService {
    return &HelloService{}
}

func (s *HelloService) ListHello(ctx context.Context, req *pb.ListHelloRequest) (*pb.ListHelloReply, error) {
    return &pb.ListHelloReply{}, nil
}

run命令

啟動服務

kratos run

原文地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM