准备工作
官网:https://go-zero.dev/cn/docs/introduction
环境安装:
- Go:https://go-zero.dev/cn/docs/prepare/golang-install
- Go Module:https://go-zero.dev/cn/docs/prepare/gomod-config
- Goctl:
go install github.com/tal-tech/go-zero/tools/zeromicro@latest
- protoc & protoc-gen-go安装:
goctl env check -i -f
- etcd :https://github.com/etcd-io/etcd/releases
- mysql + redis + nginx : laragon https://laragon.org/download/index.html
仓库准备:
https://gitee.com/HappyTeemo/go-zero-demo
单体
$ mkdir go-zero-demo
$ cd go-zero-demo
$ go mod init go-zero-demo
$ goctl api new greet
$ go mod tidy
Done.
$ vim greet/internal/logic/greetlogic.go
func (l *GreetLogic) Greet(req *types.Request) (*types.Response, error) {
return &types.Response{
Message: "Hello go-zero",
}, nil
}
$ cd greet
$ go run greet.go -f etc/greet-api.yaml
微服务
https://go-zero.dev/cn/docs/quick-start/micro-service
对着写即可,唯一的坑是
$ cd mall/user/rpc
$ goctl rpc protoc user.proto --go_out=./types --go-grpc_out=./types --zrpc_out=.
Done.
这里可能会卡住不动,多执行几次即可。
还有一点,每次user服务修改了结构,调用者的引用会发生变化,如果是考虑版本兼容或者多仓库的思路,可以考虑将这个文件拷贝到公共目录下。
标签:demo,greet,zero,https,go,types From: https://www.cnblogs.com/HappyTeemo/p/17101582.html