官方文档:
https://go-zero.dev/cn/monolithic-service.html
系统环境:
linux debain
配置环境:
1. golang安装
2. go module配置
go env -w GO111MODULE="on"
go env -w GOPROXY=https://goproxy.cn
go env -w GOMODCACHE=$GOPATH/pkg/mod
3. goctl安装
Go 1.16 及以后版本
GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest
goctl -v
参考: https://go-zero.dev/cn/goctl-install.html
4. protoc&protoc-gen-go安装
4.1 protoc安装
4.2 protoc-gen-go 安装
如果goctl 版本已经是1.2.1以后了,可以忽略此步骤。
GOPROXY=https://goproxy.cn/,direct go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
查看$GOPATH/bin下是否有protoc-gen-go即可
【注】:如果后续在使用goctl生成代码时候,遇到以下问题
protoc --proto_path=/Users/seven/Developer/goenv/go-zero-looklook/app/usercenter/cmd/rpc/pb usercenter.proto --go_out=plugins=grpc:/Users/seven/Developer/goenv/go-zero-looklook/app/usercenter/cmd/rpc --go_opt=Musercenter.proto=././pb
goctl: generation error: unsupported plugin protoc-gen-go which installed from the following source:
google.golang.org/protobuf/cmd/protoc-gen-go,
github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;
Please replace it by the following command, we recommend to use version before v1.3.5:
go get -u github.com/golang/protobuf/protoc-gen-go
goctl version: 1.3.0 darwin/amd64
直接执行
$ GOPROXY=https://goproxy.cn/,direct go get -u github.com/golang/protobuf/protoc-gen-go 下载包
protoc-gen-go --version 查看版本
4.2 protoc-gen-go-grpc 安装
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
单体服务
创建greet服务
$ cd ~/go-zero-single_demo
$ go mod init go-zero-single_demo
$ goctl api new greet
$ go mod tidy
目录结构
cd ~/go-zero-single_demo
tree
服务的目录结构:
.
├── go.mod
├── go.sum
├── etc
│ └── greet-api.yaml
├── greet.api
├── greet.go
└── internal
├── config
│ └── config.go
├── handler
│ ├── greethandler.go
│ └── routes.go
├── logic
│ └── greetlogic.go
├── svc
│ └── servicecontext.go
└── types
└── types.go
编写逻辑
vim go-zero-single_demo/greet/internal/logic/greetlogic.go
func (l *GreetLogic) Greet(req types.Request) (*types.Response, error) {
return &types.Response{
Message: "Hello go-zero""+req.Name,
}, nil
}
启动并访问服务
$ cd ~/go-zero-single_demo/greet
$ go run greet.go -f etc/greet-api.yaml
$ curl -i -X GET \
http://localhost:8888/from/you
返回
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 07 Feb 2021 04:31:25 GMT
Content-Length: 27
{"message":"Hello go-zero name:you"}
goland启动配置:
greet.go -f etc/greet-api.yaml
修改GET入参
去除options限制的入参值
- 修改greet/greet.api 文件
type Request {
Name string `path:"name,options=you|me"`
}
修改为
type Request {
Name string `path:"name"`
}
- 重新生成代码
$ cd ~/go-zero-single_demo/greet
$ goctl api go -api greet.api -dir . -style gozero
文件greet/internal/types/types.go 会被重新生成
- 重启服务
$ cd ~/go-zero-single_demo/greet
$ go run greet.go -f etc/greet-api.yaml
$ curl -i -X GET \
http://localhost:8888/from/haima
返回
{
"message": "Hello go-zero name:haima"
}
添加post请求
- 修改
greet/greet.api 文件
type RequestJson {
Name string `json:"name"`
}
service greet-api {
......
@handler GreetPostHandler
post /from/:name(RequestJson) returns (Response)
}
- 重新生成代码
$ cd ~/go-zero-single_demo/greet
$ goctl api go -api greet.api -dir . -style gozero
会成生以下两个文件
- greet/internal/handler/greetposthandler.go
- greet/internal/logic/greetpostlogic.go
文件greet/internal/types/types.go里会自动添加如下代码
type RequestJson struct {
Name string `json:"name"`
}
- 修改
greet/internal/logic/greetpostlogic.go 文件
func (l *GreetPostLogic) GreetPost(req types.RequestJson) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
return &types.Response{
Message: "Hello go-zero name:"+req.Name,
}, nil
}
- 重启服务
$ cd ~/go-zero-single_demo/greet
$ go run greet.go -f etc/greet-api.yaml
- post请求
新建test.http 请求文件
###
GET http://127.0.0.1:8888/from/haima HTTP/1.1
###
POST http://127.0.0.1:8888/from/haima HTTP/1.1
Content-Type: application/json
{
"name":"post haima"
}
返回
{
"message": "Hello go-zero name:post haima"
}
|
请发表评论