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"
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM