当前位置:网站首页>Gin framework: automatically add requestid
Gin framework: automatically add requestid
2022-06-24 01:05:00 【Trespass 】
Introduce
Through a complete example , stay Gin In the frame , For every one API Automatic addition RequestId .
We will use rk-boot To start up Gin Framework microservices .
Please visit the following address for a complete tutorial :
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-gin
Quick start
Open the meta After middleware , Each request will automatically contain the following values .
Header key | details |
|---|---|
X-Request-Id | The middleware will automatically generate the request ID. |
X-Prefix-App | The service name . |
X-Prefix-App-Version | Service version . |
X-Prefix-App-Unix-Time | Current service Unix Time . |
X-Prefix-Request-Received-Time | Timestamp of the request received . |
1. establish boot.yaml
In order to verify , We started commonService,commonService Contains a series of commonly used API, for example /rk/v1/healthy.
---
gin:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
commonService:
enabled: true # Optional, enable common service
interceptors:
meta:
enabled: true # Optional, enable meta middleware2. establish main.go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-gin/boot"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}3. start-up main.go
$ go run main.go
4. verification
$ curl -vs -X GET localhost:8080/rk/v1/healthy
...
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< X-Request-Id: 78c25a06-3b34-4ecb-b9dd-7197078873c7
< X-Rk-App-Name: rk-demo
< X-Rk-App-Unix-Time: 2021-11-21T00:24:49.662023+08:00
< X-Rk-App-Version: master-2c9c6fd
< X-Rk-Received-Time: 2021-11-21T00:24:49.662023+08:00
...
{"healthy":true}Cover requestId
If we want to customize requestId, Need to add a Header.
func Greeter(ctx *gin.Context) {
// Override request id
rkginctx.SetHeaderToClient(ctx, rkginctx.RequestIdKey, "request-id-override")
// We expect new request id attached to logger
rkginctx.GetLogger(ctx).Info("Received request")
ctx.JSON(http.StatusOK, &GreeterResponse{
Message: fmt.Sprintf("Hello %s!", ctx.Query("name")),
})
}
// Response.
type GreeterResponse struct {
Message string
}RequestId Will be covered .
$ curl -vs -X GET "localhost:8080/v1/greeter?name=rk-dev"
...
< X-Request-Id: request-id-override
< X-Rk-App-Name: rk-demo
< X-Rk-App-Unix-Time: 2021-11-21T00:37:16.514685+08:00
< X-Rk-App-Version: master-2c9c6fd
< X-Rk-Received-Time: 2021-11-21T00:37:16.514685+08:00
...
{"Message":"Hello rk-dev!"}If we start the logging middleware , Then we will see the following log .
2021-11-21T00:39:04.605+0800 INFO basic/main.go:54 Received request {"requestId": "request-id-override"}------------------------------------------------------------------------
endTime=2021-11-21T00:39:04.605647+08:00
startTime=2021-11-21T00:39:04.605483+08:00
elapsedNano=164096
timezone=CST
ids={"eventId":"request-id-override","requestId":"request-id-override"}
app={"appName":"rk-demo","appVersion":"master-2c9c6fd","entryName":"greeter","entryType":"GinEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"192.168.101.5","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/greeter","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:61967
operation=/v1/greeter
resCode=200
eventStatus=Ended
EOECover header Prefix
---
gin:
- name: greeter
...
interceptors:
meta:
enabled: true # Enable meta middleware
prefix: "Override" # Override prefix which formed as X-[Prefix]-xxx$ curl -vs -X GET localhost:8080/rk/v1/healthy
...
< X-Override-App-Name: rk-demo
< X-Override-App-Unix-Time: 2021-11-21T00:40:15.761993+08:00
< X-Override-App-Version: master-2c9c6fd
< X-Override-Received-Time: 2021-11-21T00:40:15.761993+08:00
< X-Request-Id: 1449deb5-464d-4b65-8430-985413c2671b
...
{"healthy":true}边栏推荐
- C语言:利用自定义函数排序
- Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks
- 【ICCV Workshop 2021】基于密度图的小目标检测:Coarse-grained Density Map Guided Object Detection in Aerial Images
- 同行评议论文怎么写
- Architecture solutions
- 现在网上开股票账户安全吗?选择国有券商,最快8分钟开户成功
- C language: recursively implementing factorial of n
- How many of the 36 difficult points of activity do you know?, Android interview 2020
- Common core resource objects of kubernetes
- Pad User Guide
猜你喜欢
![[redis advanced ziplist] if someone asks you what is a compressed list? Please dump this article directly to him.](/img/3f/988ed31a3f1cdc92deed121ed8d4ec.png)
[redis advanced ziplist] if someone asks you what is a compressed list? Please dump this article directly to him.
Shengdun technology joined dragon lizard community to build a new open source ecosystem

【Redis进阶之ZipList】如果再有人问你什么是压缩列表?请把这篇文章直接甩给他。

【小程序】实现双列商品效果

GNN upper edge distributor! Instead of trying to refine pills, you might as well give your GNN some tricks

【ICPR 2021】遥感图中的密集小目标检测:Tiny Object Detection in Aerial Images

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

Isn't this another go bug?

Installation and use of winscp and putty

分别用SVM、贝叶斯分类、二叉树、CNN实现手写数字识别
随机推荐
智能制造时代下,MES管理系统需要解决哪些问题
C language: on the right shift of matrix
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
LSF opens job idle information to view the CPU time/elapse time usage of the job
Experience summary of 9 Android interviews, bytes received, Ali, advanced Android interview answer
Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)
【CVPR 2022】高分辨率小目标检测:Cascaded Sparse Query for Accelerating High-Resolution Smal Object Detection
[applet] when compiling the preview applet, a -80063 error prompt appears
DML operation
钟珊珊:被爆锤后的工程师会起飞|OneFlow U
LSF打开Job idle information以看job的cpu time/elapse time使用情况
Architecture solutions
Graduation project - thesis writing notes [design topic type, thesis writing details, design materials]
CVPR2022 | 可精简域适应
C语言:结构体数组实现找出最低分学生记录
【ICCV Workshop 2021】基于密度图的小目标检测:Coarse-grained Density Map Guided Object Detection in Aerial Images
Talk to Wu Jiesheng, head of Alibaba cloud storage: my 20 years of data storage (unlimited growth)
Thread safety and lock optimization
Arm learning (7) symbol table and debugging
DML操作