kratos快速使用

61 阅读1分钟
kratos new babytree -r https://gitee.com/go-kratos/kratos-layout.git

$ cd babytree
$ go generate ./...
$ go build -o ./bin/ ./...
$ ./bin/babytree -conf ./configs

添加pb文件
kratos proto add api/helloworld/v1/student.proto


生成 proto 对应代码#
通过 make 命令生成:

make api
或者通过 kratos cli 生成:

kratos proto client api/helloworld/v1/student.proto


生成 Service 代码#
通过 proto 文件,直接生成对应的 Service 代码。使用 -t 指定生成目录:

kratos proto server api/helloworld/v1/student.proto -t internal/service


https://www.cnblogs.com/jiujuan/p/16331967.html

错误处理
go get github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2



Makefile 文件

GOHOSTOS:=$(shell go env GOHOSTOS)
GOPATH:=$(shell go env GOPATH)
VERSION=$(shell git describe --tags --always)

ifeq ($(GOHOSTOS), windows)
  #the `find.exe` is different from `find` in bash/shell.
  #to see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find.
  #changed to use git-bash.exe to run find cli or other cli friendly, caused of every developer has a Git.
  #Git_Bash= $(subst cmd\,bin\bash.exe,$(dir $(shell where git)))
  Git_Bash=$(subst \,/,$(subst cmd\,bin\bash.exe,$(dir $(shell where git))))
  INTERNAL_PROTO_FILES=$(shell $(Git_Bash) -c "find internal -name *.proto")
  API_PROTO_FILES=$(shell $(Git_Bash) -c "find api -name *.proto")
else
  INTERNAL_PROTO_FILES=$(shell find internal -name *.proto)
  API_PROTO_FILES=$(shell find api -name *.proto)
endif

.PHONY: init
# init env
init:
  go get -u google.golang.org/protobuf/cmd/protoc-gen-go@latest
  go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
  go get -u github.com/go-kratos/kratos/cmd/kratos/v2@latest
  go get -u github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
  go get -u github.com/google/gnostic/cmd/protoc-gen-openapi@latest
  go get -u github.com/google/wire/cmd/wire@latest
  go get -u github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2

.PHONY: errors
# generate errors code
errors:
  protoc --proto_path=. \
               --proto_path=./third_party \
               --go_out=paths=source_relative:. \
               --go-errors_out=paths=source_relative:. \
               $(API_PROTO_FILES)

.PHONY: validate
# generate validate proto
validate:
  protoc --proto_path=. \
           --proto_path=./third_party \
           --go_out=paths=source_relative:. \
           --validate_out=paths=source_relative,lang=go:. \
           $(API_PROTO_FILES)

.PHONY: config
# generate internal proto
config:
  protoc --proto_path=./internal \
         --proto_path=./third_party \
         --go_out=paths=source_relative:./internal \
         $(INTERNAL_PROTO_FILES)

.PHONY: api
# generate api proto
api:
  protoc --proto_path=./api \
         --proto_path=./third_party \
         --go_out=paths=source_relative:./api \
         --go-http_out=paths=source_relative:./api \
         --go-grpc_out=paths=source_relative:./api \
         --openapi_out=fq_schema_naming=true,default_response=false:. \
         $(API_PROTO_FILES)

.PHONY: build
# build
build:
  mkdir -p bin/ && go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ ./...

.PHONY: generate
# generate
generate:
  go mod tidy
  go get github.com/google/wire/cmd/wire@latest
  go generate ./...

.PHONY: all
# generate all
all:
  make api;
  make config;
  make generate;
  make validate;
  make errors;


# show help
help:
  @echo ''
  @echo 'Usage:'
  @echo ' make [target]'
  @echo ''
  @echo 'Targets:'
  @awk '/^[a-zA-Z\-\_0-9]+:/ { \
  helpMessage = match(lastLine, /^# (.*)/); \
    if (helpMessage) { \
      helpCommand = substr($$1, 0, index($$1, ":")); \
      helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
      printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
    } \
  } \
  { lastLine = $$0 }' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help