go-zero 使用
go-zero框架概述
goctl工具使用
1. goctl 安装
- 升级goctl 和 protoc,protoc-gen-go,protoc-gen-go-grpc
GO111MODULE=on
go install github.com/zeromicro/go-zero/tools/goctl@latest
goctl env check --install --verbose --force
- 安装protoc-gen-go (建议使用goctl安装即可)
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
2. goctl 创建一个go-zero demo仓库
# 生成 snake case 文件和目录示例
goctl api new blog --style go_zero
goctl rpc new blog --style go_zero
3. goctl 高级用法
1. 替换代码模板
goctl template init
编辑 ~/.goctl/${goctl版本号}/api/handler.tpl
2. 生成api代码
goctl api go -api blog.api -dir ../ --style go_zero
3. 格式化api代码
goctl api format --dir blog.api
4. 生成api.ts代码
goctl api ts -api blog.api -dir ../ts
4. goctl goctl-swagger生成文档
https://github.com/zeromicro/goctl-swagger
1. 编译goctl-swagger插件
GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/goctl-swagger@latest
2. 使用goctl-swagger插件
goctl api plugin -plugin goctl-swagger="swagger -filename user.json" -api user.api -dir .
项目启动
1.本地启动项目
启动etcd(可选,使用直连rpc模式时不需要)
etcd
启动rpc服务
go run service/blog/rpc/blog.go -f service/blog/rpc/etc/blog-rpc.yaml
启动api服务
go run service/blog/api/blog.go -f service/blog/api/etc/blog-api.yaml
2.使用nacos配置启动
go run service/blog/rpc/blog.go
go run service/blog/api/blog.go
3.使用docker启动
构造镜像
goctl docker --go service/blog/rpc/blog.go --exe blog