- 【kratos入门实战教程】0-商城项目介绍
- 【kratos入门实战教程】1-kratos项目搭建和开发环境配置
- 【kratos入门实战教程】2-实现注册登陆业务
2、概览
经过上一篇的说明,本篇教程正式开始搭建项目。深入解析工程项目的目录结构和介绍一些开发需要使用的工具。如果读者没有阅读过上一篇教程,可以点击这里进行跳转。
3、搭建项目 3.1、安装kratos脚手架注意:读者需要提前配置好本地的GO环境[version>=1.16]
打开命令行执行命令:
GOPROXY=https://goproxy.io,direct go install github.com/go-kratos/kratos/cmd/kratos/v2@latest && kratos upgrade
成功的结果会看到:
❯ GOPROXY=https://goproxy.io,direct go install github.com/go-kratos/kratos/cmd/kratos/v2@latest && kratos upgrade go: downloading github.com/go-kratos/kratos/cmd/kratos/v2 v2.0.0-20220714125901-1ab3d8f02840 go install github.com/go-kratos/kratos/cmd/kratos/v2@latest go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest go: downloading github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2 v2.0.0-20220714125901-1ab3d8f02840 go install github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2@latest go: downloading github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2 v2.0.0-20220714125901-1ab3d8f02840 go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest go: downloading google.golang.org/grpc v1.48.0 go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest3.2、创建项目
还是在命令行里(win用户可能会吐槽了),进入一个自己的工作目录(随意一个就行),执行创建项目的命令:
kratos new kratos-shop
然后就能看到如下输出:
❯ kratos new kratos-shop Creating service kratos-shop, layout repo is https://github.com/go-kratos/kratos-layout.git, please wait a moment. 已经是最新的。 CREATED kratos-shop/.gitignore (528 bytes) CREATED kratos-shop/Dockerfile (459 bytes) CREATED kratos-shop/LICENSE (1066 bytes) CREATED kratos-shop/Makefile (2399 bytes) CREATED kratos-shop/README.md (1062 bytes) CREATED kratos-shop/api/helloworld/v1/error_reason.pb.go (4991 bytes) CREATED kratos-shop/api/helloworld/v1/error_reason.proto (290 bytes) CREATED kratos-shop/api/helloworld/v1/greeter.pb.go (8074 bytes) CREATED kratos-shop/api/helloworld/v1/greeter.proto (679 bytes) CREATED kratos-shop/api/helloworld/v1/greeter_grpc.pb.go (3560 bytes) CREATED kratos-shop/api/helloworld/v1/greeter_http.pb.go (2139 bytes) CREATED kratos-shop/cmd/kratos-shop/main.go (1713 bytes) CREATED kratos-shop/cmd/kratos-shop/wire.go (589 bytes) CREATED kratos-shop/cmd/kratos-shop/wire_gen.go (1073 bytes) CREATED kratos-shop/configs/config.yaml (266 bytes) CREATED kratos-shop/go.mod (991 bytes) CREATED kratos-shop/go.sum (18962 bytes) CREATED kratos-shop/internal/biz/README.md (6 bytes) CREATED kratos-shop/internal/biz/biz.go (128 bytes) CREATED kratos-shop/internal/biz/greeter.go (1236 bytes) CREATED kratos-shop/internal/conf/conf.pb.go (20782 bytes) CREATED kratos-shop/internal/conf/conf.proto (762 bytes) CREATED kratos-shop/internal/data/README.md (7 bytes) CREATED kratos-shop/internal/data/data.go (473 bytes) CREATED kratos-shop/internal/data/greeter.go (835 bytes) CREATED kratos-shop/internal/server/grpc.go (828 bytes) CREATED kratos-shop/internal/server/http.go (832 bytes) CREATED kratos-shop/internal/server/server.go (150 bytes) CREATED kratos-shop/internal/service/README.md (10 bytes) CREATED kratos-shop/internal/service/greeter.go (690 bytes) CREATED kratos-shop/internal/service/service.go (136 bytes) CREATED kratos-shop/openapi.yaml (1130 bytes) CREATED kratos-shop/third_party/README.md (14 bytes) CREATED kratos-shop/third_party/errors/errors.proto (411 bytes) CREATED kratos-shop/third_party/google/api/annotations.proto (1051 bytes) CREATED kratos-shop/third_party/google/api/client.proto (3395 bytes) CREATED kratos-shop/third_party/google/api/field_behavior.proto (3011 bytes) CREATED kratos-shop/third_party/google/api/http.proto (15140 bytes) CREATED kratos-shop/third_party/google/api/httpbody.proto (2671 bytes) CREATED kratos-shop/third_party/google/protobuf/any.proto (5909 bytes) CREATED kratos-shop/third_party/google/protobuf/api.proto (7734 bytes) CREATED kratos-shop/third_party/google/protobuf/compiler/plugin.proto (8754 bytes) CREATED kratos-shop/third_party/google/protobuf/descriptor.proto (38497 bytes) CREATED kratos-shop/third_party/google/protobuf/duration.proto (4895 bytes) CREATED kratos-shop/third_party/google/protobuf/empty.proto (2429 bytes) CREATED kratos-shop/third_party/google/protobuf/field_mask.proto (8185 bytes) CREATED kratos-shop/third_party/google/protobuf/source_context.proto (2341 bytes) CREATED kratos-shop/third_party/google/protobuf/struct.proto (3779 bytes) CREATED kratos-shop/third_party/google/protobuf/timestamp.proto (6459 bytes) CREATED kratos-shop/third_party/google/protobuf/type.proto (6126 bytes) CREATED kratos-shop/third_party/google/protobuf/wrappers.proto (4042 bytes) CREATED kratos-shop/third_party/openapi/v3/annotations.proto (2196 bytes) CREATED kratos-shop/third_party/openapi/v3/openapi.proto (22082 bytes) CREATED kratos-shop/third_party/validate/README.md (81 bytes) CREATED kratos-shop/third_party/validate/validate.proto (31270 bytes) Project creation succeeded kratos-shop Use the following command to start the project : $ cd kratos-shop $ go generate ./... $ go build -o ./bin/ ./... $ ./bin/kratos-shop -conf ./configs 欄 Thanks for using Kratos Tutorial: https://go-kratos.dev/docs/getting-started/start
至此,一个项目就创建完了,是不是很简单?没错!kratos就是这么简单。
4、目录结构解析使用IDE打开刚刚创建的项目(goland、vscode或者vim),看到项目的整体目录大致如下:
├── api
│ └── helloworld
├── cmd
│ └── kratos-shop
├── configs
├── internal
│ ├── biz
│ ├── conf
│ ├── data
│ ├── server
│ └── service
└── third_party
├── errors
├── google
├── openapi
└── validate
目录层级折射出的架构如下图:
是不是很熟悉?是的,和整洁架构几乎是一样的。kratos-layout的目录架构是参考整洁架构和DDD的设计思想的。
api目录用于存放对外暴露的接口的源文件,包括定义用的protobuf文件和各种插件生成的源码.go文件。kratos的api风格是使用protobuf定义接口以及接口返回的错误,然后使用插件生成相应的代码。
4.2、cmdcmd目录是应用的main入口目录。例如项目是kratos-shop,那么服务应用的main就在kratos-shop目录下。如果是定时任务应用,那么使用者可以自行添加job目录和main入口,构建的时候会一并构建出来的。
4.3、configsconfigs目录是存放应用的静态配置的。kratos的配置默认使用yaml格式,同时也支持json格式。自定义格式的配置可以自行添加解析器,这个特性在后面的教程再张开介绍。
4.4、internalinternal目录是存放业务代码的。整个项目中,这个目录尤其重要,几乎所有的业务逻辑的实现源码都是存在在此目录中。下面再就子目录展开说明:
4.4.1、confconf目录定义应用的配置。和api一样,kratos的风格是使用统一的DL语言,即protobuf,定义服务的配置,然后使用protoc以及相关的插件生成go源码,供业务代码读取需要的配置项。
4.4.2、serverserver目录是定义应用使用的应用层协议。kratos提供了grpc和http两种。
4.4.3、serviceservice目录实现了api接口。api的实现(implement)是在service这一层做的。
4.4.4、bizbiz目录是业务逻辑目录。所有的业务逻辑都是在这一层目录实现的。
4.4.5、datadata目录是数据层目录。数据层包含了一切提供数据的外部数据源,包括数据库、缓存甚至下游服务的rpc接口。
4.5、third_partythird_party目录存放一些用到的proto文件。一般开发日常不会动这个目录的。
5、常用工具如下是常用工具清单:
- make
- wire
- docker && docker-compose
这些工具需要读者自定安装配置好。
6、配置开发环境项目用到了db、redis和api文档都是通过docker容器在本地跑起的,我们可以使用docker-compose工具,一次性拉起所需要的中间件。如何使用docker和docker-compose不在本系列的教程中,所以这里直接给出docker-compose配置文件和配置常用的make命令。
docker-compose.yaml:
version: "3"
services:
db:
image: mariadb
restart: always
ports:
- "3306:3306"
environment:
MARIADB_ROOT_PASSWORD: root
MARIADB_DATABASE: test
redis:
image: redis
restart: always
ports:
- "6379:6379"
openapi:
ports:
- "8080:8080"
image: swaggerapi/swagger-ui
volumes:
- ${PWD}:/data
environment:
SWAGGER_JSON: /data/openapi.yaml
在Makefile后面追加如下内容:
.PHONY: set-env set-env: docker-compose -f docker-compose.yaml up -d .PHONY: clean-env clean-env: docker-compose -f docker-compose.yaml down



