参考文献

https://go-kratos.dev/
Docs

环境说明

  • kratos version v2.0.0-beta4
  • go version go1.16.2 darwin/amd64

步骤说明

环境准备

参考:快速开始

安装命令工具

# 安装 kratos 命令工具
go get -u github.com/go-kratos/kratos/cmd/kratos/v2@latest
# 可能不是最新的项目,可以执行
kratos upgrade

我这里kratos 还是0.5.0版本,所以我这使用的是kratos upgrade

创建项目

kratos new <project-name>

# axin @ AxindeMacBook-Pro in ~/workspace/goProject [10:55:35]
$ kratos new axin
🚀 Creating service axin, layout repo is https://github.com/go-kratos/kratos-layout.git, please wait a moment.

Cloning into '/Users/axin/.kratos/repo/github.com/go-kratos/kratos-layout'...
remote: Enumerating objects: 540, done.
remote: Counting objects: 100% (540/540), done.
remote: Compressing objects: 100% (231/231), done.
remote: Total 540 (delta 278), reused 486 (delta 240), pack-reused 0
Receiving objects: 100% (540/540), 129.59 KiB | 100.00 KiB/s, done.
Resolving deltas: 100% (278/278), done.
CREATED axin/.gitignore (528 bytes)
CREATED axin/Dockerfile (458 bytes)
CREATED axin/LICENSE (1066 bytes)
CREATED axin/Makefile (2422 bytes)
CREATED axin/README.md (798 bytes)
CREATED axin/api/helloworld/v1/error_reason.pb.go (5345 bytes)
CREATED axin/api/helloworld/v1/error_reason.proto (314 bytes)
CREATED axin/api/helloworld/v1/error_reason.swagger.json (841 bytes)
CREATED axin/api/helloworld/v1/greeter.pb.go (8230 bytes)
CREATED axin/api/helloworld/v1/greeter.proto (688 bytes)
CREATED axin/api/helloworld/v1/greeter.swagger.json (1845 bytes)
CREATED axin/api/helloworld/v1/greeter_grpc.pb.go (3449 bytes)
CREATED axin/api/helloworld/v1/greeter_http.pb.go (2203 bytes)
CREATED axin/cmd/axin/main.go (1527 bytes)
CREATED axin/cmd/axin/wire.go (549 bytes)
CREATED axin/cmd/axin/wire_gen.go (1017 bytes)
CREATED axin/configs/config.yaml (266 bytes)
CREATED axin/generate.go (54 bytes)
CREATED axin/go.mod (725 bytes)
CREATED axin/go.sum (18353 bytes)
CREATED axin/internal/biz/README.md (6 bytes)
CREATED axin/internal/biz/biz.go (128 bytes)
CREATED axin/internal/biz/greeter.go (683 bytes)
CREATED axin/internal/conf/conf.pb.go (21314 bytes)
CREATED axin/internal/conf/conf.proto (754 bytes)
CREATED axin/internal/data/README.md (7 bytes)
CREATED axin/internal/data/data.go (464 bytes)
CREATED axin/internal/data/greeter.go (507 bytes)
CREATED axin/internal/server/grpc.go (1033 bytes)
CREATED axin/internal/server/http.go (1045 bytes)
CREATED axin/internal/server/server.go (150 bytes)
CREATED axin/internal/service/README.md (10 bytes)
CREATED axin/internal/service/greeter.go (888 bytes)
CREATED axin/internal/service/service.go (136 bytes)

🍺 Project creation succeeded axin
💻 Use the following command to start the project 👇:

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

			🤝 Thanks for using Kratos
	📚 Tutorial: https://go-kratos.dev/docs/getting-started/start

拉取依赖

cd <project-name> && go mod download

# axin @ AxindeMacBook-Pro in ~/workspace/goProject [11:03:40]
$ cd ./axin && go mod download

实体

数据访问

# axin @ AxindeMacBook-Pro in ~/workspace/goProject/axin on git:master x [13:38:56] 
$ go get entgo.io/ent/cmd/ent
go: downloading entgo.io/ent v0.8.0
go: downloading github.com/go-bindata/go-bindata v1.0.1-0.20190711162640-ee3c2418e368
go get: added entgo.io/ent v0.8.0

运行

{
    "message": "Hello kratos"
}