当前位置:网站首页>Grp: how to automatically add requestid in GRP service?
Grp: how to automatically add requestid in GRP service?
2022-06-24 03:07:00 【Trespass 】
Introduce
This article will show you how to gRPC In microservice , For every one API Automatic addition RequestId .
We will use rk-boot To start up gRPC service .
Please visit the following address for a complete tutorial :https://rkdev.info/cnhttps://rkdocs.netlify.app/cn ( spare )
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-grpc
Quick start
For detailed documents, please refer to :
- Official documents
- perhaps ,Github
Open the meta After the interceptor , Each request will automatically contain the following values .
Header key | details |
|---|---|
X-Request-Id | The interceptor 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.
---
grpc:
- name: greeter # Name of grpc entry
port: 8080 # Port of grpc entry
enabled: true # Enable grpc entry
commonService:
enabled: true # Enable common service for testing
interceptors:
meta:
enabled: true2. establish main.go
package main
import (
"context"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-grpc/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
Above boot.yaml in , We didn't start enableRkGwOption, therefore , Return to the middle of the header , There will be Grpc-Metadata Prefix .
$ curl -vs -X GET localhost:8080/rk/v1/healthy
...
< Grpc-Metadata-Content-Type: application/grpc
< Grpc-Metadata-X-Request-Id: cb8c0c77-39cd-4e15-89d2-66ba30fa7a46
< Grpc-Metadata-X-Rk-App-Name: rk-demo
< Grpc-Metadata-X-Rk-App-Unix-Time: 2021-07-10T00:19:58.170222+08:00
< Grpc-Metadata-X-Rk-App-Version: master-f414049
< Grpc-Metadata-X-Rk-Received-Time: 2021-07-10T00:19:58.170222+08:00
...
{"healthy":true}If we start enableRkGwOption, Grpc-Metadata Will disappear !
--- grpc: ... enableRkGwOption: true
$ curl -vs -X GET localhost:8080/rk/v1/healthy
...
< X-Request-Id: 2ae18df6-d132-42ce-841c-571f19a88787
< X-Rk-App-Name: rk-demo
< X-Rk-App-Unix-Time: 2021-07-10T00:24:10.281868+08:00
< X-Rk-App-Version: master-f414049
< X-Rk-Received-Time: 2021-07-10T00:24:10.281868+08:00
...
{"healthy":true}Cover requestId
If we want to customize requestId, Need to add a Header.
func (server *GreeterServer) Greeter(ctx context.Context, request *greeter.GreeterRequest) (*greeter.GreeterResponse, error) {
// Override request id
rkgrpcctx.AddHeaderToClient(ctx, rkginctx.RequestIdKey, "request-id-override")
// We expect new request id attached to logger
rkgrpcctx.GetLogger(ctx).Info("Received request")
return &greeter.GreeterResponse{
Message: fmt.Sprintf("Hello %s!", request.Name),
}, nil
}In the returned header , There are two X-Request-Id, This is because , For the same Key Set it up ,GRPC Will fuse these Value, Not replacement . However , The client , The last value is extracted .
$ curl -vs -X GET "localhost:8080/v1/greeter?name=rk-dev"
...
< X-Request-Id: 4e858644-f2e2-48b2-adec-a1d329497abf
< X-Request-Id: request-id-override
< X-Rk-App-Name: rk-demo
< X-Rk-App-Unix-Time: 2021-07-10T00:29:19.030555+08:00
< X-Rk-App-Version: master-f414049
< X-Rk-Received-Time: 2021-07-10T00:29:19.030555+08:00
...
{"Message":"Hello rk-dev!"}If we start the log interceptor , Then we will see the following log .
2021-07-10T00:29:19.030+0800 INFO basic/main.go:40 Received request {"requestId": "request-id-override"}------------------------------------------------------------------------
endTime=2021-07-10T00:29:19.030673+08:00
startTime=2021-07-10T00:29:19.030535+08:00
elapsedNano=138133
timezone=CST
ids={"eventId":"request-id-override","requestId":"request-id-override"}
app={"appName":"rk-demo","appVersion":"master-f414049","entryName":"greeter","entryType":"GrpcEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"grpcMethod":"Greeter","grpcService":"api.v1.Greeter","grpcType":"unaryServer","gwMethod":"GET","gwPath":"/v1/greeter","gwScheme":"http","gwUserAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:58601
operation=/api.v1.Greeter/Greeter
resCode=OK
eventStatus=Ended
EOECover header Prefix
---
grpc:
- name: greeter
...
interceptors:
meta:
enabled: true # Enable meta interceptor/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-07-10T00:34:59.974205+08:00
< X-Override-App-Version: master-f414049
< X-Override-Received-Time: 2021-07-10T00:34:59.974205+08:00
< X-Request-Id: 4deded12-2d39-42a5-b740-b4f74dd73c90
...
{"healthy":true}边栏推荐
- [51nod] 3216 Awards
- What is the use of cloud desktop security server configuration? What should I do?
- How to pair cloud game servers? Is the cloud game server expensive?
- Grand summary of boutique idea plug-ins! Worth collecting
- How does easydss handle the problem that the sharing page cannot be opened due to cache problems?
- What are the performance characteristics of cloud desktop? How to choose the most cost-effective cloud desktop server?
- [51nod] 3047 displacement operation
- 2022-2028 global marine wet exhaust hose industry research and trend analysis report
- Sinclair radio stopped broadcasting many TV stations, suspected of being attacked by blackmail software
- RI Geng series: tricks of using function pointers
猜你喜欢

2022-2028 global indoor pressure monitor and environmental monitor industry research and trend analysis report

What is etcd and its application scenarios
![[51nod] 2653 section XOR](/img/2d/cb4bf4e14939ce432cac6d35b6a41b.jpg)
[51nod] 2653 section XOR

The cost of on-site development of software talent outsourcing is higher than that of software project outsourcing. Why

2022-2028 global medical coating materials industry research and trend analysis report

2022-2028 global marine clutch industry research and trend analysis report

Simple and beautiful weather code

2022-2028 global genome editing mutation detection kit industry survey and trend analysis report

2022-2028 global cell-based seafood industry research and trend analysis report

2022-2028 Global Industry Survey and trend analysis report on portable pressure monitors for wards
随机推荐
How to install the cloud desktop security server certificate? What can cloud desktops do?
2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report
Industry ranks first in blackmail attacks, hacker organizations attack Afghanistan and India | global network security hotspot
Concise and practical time code
How does the fortress machine connect to the server? Which is easy to use, fortress machine or firewall?
I have a server. What can I do?
How to handle the occasional address request failure in easygbs live video playback?
Grpc: how do I start multiple ports?
What is the case when easynvr non administrator logs in to view the empty video list?
[51nod] 3216 Awards
2022-2028 global genome editing mutation detection kit industry survey and trend analysis report
Why should I change my PC to a cloud desktop server? What are the characteristics of this server?
Tencent dongzhiqiang: network security construction needs to change from product driven to service driven
SAP retail characteristic profile I
Live broadcast Reservation: a guide to using the "cloud call" capability of wechat cloud hosting
Velocitytracker use
How much does it cost to rent a cloud game server? Which cloud game server is more reliable?
JD Logistics: from giant baby to mainstay
October 27, 2021: curriculum. You must take numcourses this semester
Instructions for performance pressure test tool