zeromicro/go-zero
Create zeromicro/go-zero server with rk-boot and rk-gin plugins.
Overview#
We will use rk-boot start zeromicro/go-zero microservice and add /v1/greeter API into it.
Furthermore, we will enable bellow functionalities.
Functionality | Description |
---|---|
Swagger UI | Enable Swagger UI |
API Docs UI | Enable RapiDoc UI |
Prometheus Client | Enable Prometheus Client |
Logging middleware | Automatically record logs for every API calls |
Prometheus middleware | Automatically record prometheus metrics for every API calls |
Meta middleware | Automatically add requestID for every API response |
Install#
go get github.com/rookie-ninja/rk-boot/v2
go get github.com/rookie-ninja/rk-zero
1. Create boot.yaml#
zero:
- name: greeter
port: 8080
enabled: true
sw:
enabled: true # Enable Swagger UI,default path: /sw
docs:
enabled: true # Enable API Doc UI,default path: /docs
prom:
enabled: true # Enable Prometheus Client,default path: /metrics
middleware:
logging:
enabled: true
prom:
enabled: true
meta:
enabled: true
2. Create main.go#
In order to generate swagger.json file, we add comments and generate swagger.json file with swag CLI.
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/rookie-ninja/rk-boot/v2"
"github.com/rookie-ninja/rk-zero/boot"
"github.com/zeromicro/go-zero/rest"
"net/http"
)
// @title Swagger Example API
// @version 1.0
// @description This is a sample rk-demo server.
// @termsOfService http://swagger.io/terms/
// @securityDefinitions.basic BasicAuth
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Register handler
zeroEntry := rkzero.GetZeroEntry("greeter")
zeroEntry.Server.AddRoute(rest.Route{
Method: http.MethodGet,
Path: "/v1/greeter",
Handler: Greeter,
})
// Bootstrap
boot.Bootstrap(context.TODO())
boot.WaitForShutdownSig(context.TODO())
}
// Greeter handler
// @Summary Greeter
// @Id 1
// @Tags Hello
// @version 1.0
// @Param name query string true "name"
// @produce application/json
// @Success 200 {object} GreeterResponse
// @Router /v1/greeter [get]
func Greeter(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
resp := &GreeterResponse{
Message: fmt.Sprintf("Hello %s!", request.URL.Query().Get("name")),
}
bytes, _ := json.Marshal(resp)
writer.Write(bytes)
}
type GreeterResponse struct {
Message string
}
3.Generate swagger.json#
$ swag init
4.Directory hierarchy#
$ tree
.
├── boot.yaml
├── docs
│ ├── swagger.json
│ └── swagger.yaml
├── go.mod
├── go.sum
└── main.go
5.Run main.go#
$ go run main.go
2022-05-09T13:37:24.802+0800 INFO boot/zero_entry.go:761 Bootstrap zeroEntry {"eventId": "c2451ccd-b517-4b37-8691-ff621a9a0223", "entryName": "greeter", "entryType": "ZeroEntry"}
2022-05-09T13:37:24.803+0800 INFO boot/zero_entry.go:519 SwaggerEntry: http://localhost:8080/sw/
2022-05-09T13:37:24.803+0800 INFO boot/zero_entry.go:522 DocsEntry: http://localhost:8080/docs/
2022-05-09T13:37:24.803+0800 INFO boot/zero_entry.go:525 PromEntry: http://localhost:8080/metrics
------------------------------------------------------------------------
endTime=2022-05-09T13:37:24.803892+08:00
startTime=2022-05-09T13:37:24.802285+08:00
elapsedNano=1607377
timezone=CST
ids={"eventId":"c2451ccd-b517-4b37-8691-ff621a9a0223"}
app={"appName":"rk","appVersion":"local","entryName":"greeter","entryType":"ZeroEntry"}
env={"arch":"amd64","domain":"*","hostname":"lark.local","localIP":"192.168.1.104","os":"darwin"}
payloads={"docsEnabled":true,"docsPath":"/docs/","promEnabled":true,"promPath":"/metrics","promPort":8080,"swEnabled":true,"swPath":"/sw/","zeroPort":8080}
counters={}
pairs={}
timing={}
remoteAddr=localhost
operation=Bootstrap
resCode=OK
eventStatus=Ended
EOE
6.Validate#
6.1 Swagger UI#
6.2 API Docs UI#
6.3 Prometheus Client#
6.4 Send request#
$ curl -vs "localhost:8080/v1/greeter?name=rk-dev"
* ...
< X-Request-Id: 7120529c-893b-4caa-a425-bc268de5cbc0
< X-Rk-App-Domain: *
< X-Rk-App-Name: rk
< X-Rk-App-Unix-Time: 2022-04-14T01:18:39.013447+08:00
< X-Rk-App-Version: local
< X-Rk-Received-Time: 2022-04-14T01:18:39.013447+08:00
< ...
{"Message":"Hello rk-dev!"}
6.5 API Log#
rk-boot will use bellow format of logging. JSON is also supported, please see user guide for details.
------------------------------------------------------------------------
endTime=2022-05-09T13:37:47.631765+08:00
startTime=2022-05-09T13:37:47.63152+08:00
elapsedNano=245785
timezone=CST
ids={"eventId":"7d461811-81e6-4b48-ae0a-a03c5ed66b5d","requestId":"7d461811-81e6-4b48-ae0a-a03c5ed66b5d","traceId":"872dfba05620f46e12ad9ba75cd9ada4"}
app={"appName":"rk","appVersion":"local","entryName":"greeter","entryType":"ZeroEntry"}
env={"arch":"amd64","domain":"*","hostname":"lark.local","localIP":"192.168.1.104","os":"darwin"}
payloads={"apiMethod":"GET","apiPath":"/v1/greeter","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
counters={}
pairs={}
timing={}
remoteAddr=localhost:64509
operation=/v1/greeter
resCode=200
eventStatus=Ended
EOE
6.6 Prometheus Metrics#
Access http://localhost:8080/metrics