01 go-zero入门--单体服务demo

官方文档:

https://go-zero.dev/cn/docs/introduction

视频地址:

https://space.bilibili.com/387126464/channel/series

系统环境:

linux debain

配置环境:

1. golang安装

配置go环境变量:

vim /home/haima/.bashrc

export GOROOT="/usr/local/go" #go源码包
export GOPATH=/home/go # go工作路径
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

source /home/haima/.bashrc

2. go module配置

    go env -w GO111MODULE="on"
    go env -w GOPROXY=https://goproxy.cn
    go env -w GOMODCACHE=$GOPATH/pkg/mod

3. goctl安装

参考:
https://go-zero.dev/cn/docs/goctl/installation

goctl一键安装

Go 1.16 及以后版本

安装(mac&linux)

GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest
查看版本
haima@haima-PC:~$ goctl -v
goctl version 1.3.4 linux/amd64

安装(windows)

$ go install github.com/zeromicro/go-zero/tools/goctl@latest
$ goctl -v
goctl version 1.4.0 windows/amd64

4.protoc,protoc-gen-go,protoc-gen-go-grpc安装

方式一:
如果goctl安装的版本是>=1.3.3版本的,执行以命令就可以自动安装protoc,protoc-gen-go,protoc-gen-go-grpc三个依赖

haima@haima-PC:~/Desktop$ goctl -v
goctl version 1.3.3 linux/amd64

$ goctl env check -i -f --verbose                                 
[goctl-env]: preparing to check env

[goctl-env]: looking up "protoc"
[goctl-env]: "protoc" is not found in PATH
[goctl-env]: preparing to install "protoc"
"protoc" installed from cache
[goctl-env]: "protoc" is already installed in "/Users/keson/go/bin/protoc"

[goctl-env]: looking up "protoc-gen-go"
[goctl-env]: "protoc-gen-go" is not found in PATH
[goctl-env]: preparing to install "protoc-gen-go"
"protoc-gen-go" installed from cache
[goctl-env]: "protoc-gen-go" is already installed in "/Users/keson/go/bin/protoc-gen-go"

[goctl-env]: looking up "protoc-gen-go-grpc"
[goctl-env]: "protoc-gen-go-grpc" is not found in PATH
[goctl-env]: preparing to install "protoc-gen-go-grpc"
"protoc-gen-go-grpc" installed from cache
[goctl-env]: "protoc-gen-go-grpc" is already installed in "/Users/keson/go/bin/protoc-gen-go-grpc"

[goctl-env]: congratulations! your goctl environment is ready!

方式二: 源文件安装

goctl版本小于 1.3.3 参考以下面源文件安装,单个手动安装protoc,protoc-gen-go,protoc-gen-go-grpc三个依赖

1. protoc&protoc-gen-go安装

1.1 protoc安装

查看版本

haima@haima-PC:~$ protoc --version
libprotoc 3.19.4

1.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 下载包

查看版本

haima@haima-PC:~/Desktop$ protoc-gen-go --version
protoc-gen-go v1.27.1

1.3 protoc-gen-go-grpc 安装

go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

查看版本

haima@haima-PC:~$ protoc-gen-go-grpc -version
protoc-gen-go-grpc 1.2.0

单体服务

创建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

image

修改GET入参

去除options限制的入参值

  1. 修改greet/greet.api 文件
type Request {
	Name string `path:"name,options=you|me"`
}

修改为

type Request {
	Name string `path:"name"`
}
  1. 重新生成代码
  $ cd ~/go-zero-single_demo/greet
  $ goctl api go -api greet.api -dir . -style gozero

文件greet/internal/types/types.go 会被重新生成

  1. 重启服务
  $ 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请求

  1. 修改 greet/greet.api 文件

type RequestJson {
    Name string `json:"name"`
}

service greet-api {
	......
	
	@handler GreetPostHandler
	post /from/:name(RequestJson) returns (Response)
}
  1. 重新生成代码
  $ 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"`
}
  1. 修改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
}
  1. 重启服务
  $ cd ~/go-zero-single_demo/greet
  $ go run greet.go -f etc/greet-api.yaml
  1. 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"
}
posted @ 2022-02-01 21:56  HaimaBlog  阅读(1830)  评论(0编辑  收藏  举报