gRPC超時(shí)攔截器實(shí)現(xiàn)示例
介紹
本文介紹如何通過(guò) rk-boot 快速搭建 gRPC 超時(shí)攔截器。
什么是 gRPC 超時(shí)攔截器?
攔截器會(huì)攔截 gRPC 請(qǐng)求,并根據(jù)策略返回超時(shí)錯(cuò)誤。
安裝
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-grpc
快速開(kāi)始
使用 rk-boot 啟動(dòng)的 gRPC 服務(wù)。
支持全局超時(shí)和 API 超時(shí)設(shè)定。
1.創(chuàng)建 boot.yaml
boot.yaml 文件告訴 rk-boot 如何啟動(dòng) gRPC 服務(wù)。
為了驗(yàn)證,我們啟動(dòng)了 commonService,commonService 里包含了一系列常用 API,例如 /rk/v1/gc。
設(shè)定全局超時(shí)為 5秒,讓 GC 的超時(shí)時(shí)間定位 1 毫秒,GC 一般會(huì)超過(guò) 1 毫秒。
---
grpc:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
commonService:
enabled: true # Optional, Enable common service for testing
interceptors:
timeout:
enabled: true # Optional, default: false
timeoutMs: 5000 # Optional, default: 5000
paths:
- path: "/rk.api.v1.RkCommonService/Gc" # Optional, default: ""
timeoutMs: 1 # Optional, default: 5000
2.創(chuàng)建 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-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.啟動(dòng) main.go
$ go run main.go
4.驗(yàn)證
發(fā)送 GC 請(qǐng)求。
$ grpcurl -plaintext localhost:8080 rk.api.v1.RkCommonService.Gc
ERROR:
Code: Canceled
Message: Request timed out!
Details:
1) {"@type":"type.googleapis.com/rk.api.v1.ErrorDetail","code":1,"message":"[from-grpc] Request timed out!","status":"Canceled"}
$ curl -X GET localhost:8080/rk/v1/gc
{
"error":{
"code":408,
"status":"Request Timeout",
"message":"Request timed out!",
"details":[
{
"code":1,
"status":"Canceled",
"message":"[from-grpc] Request timed out!"
}
]
}
}以上就是gRPC超時(shí)攔截器實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于gRPC超時(shí)攔截器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
golang遍歷時(shí)修改被遍歷對(duì)象的示例詳解
這篇文章主要介紹了golang遍歷時(shí)修改被遍歷對(duì)象的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Golang實(shí)現(xiàn)自己的Redis(有序集合跳表)實(shí)例探究
這篇文章主要為大家介紹了Golang實(shí)現(xiàn)自己的Redis(有序集合跳表)實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Golang語(yǔ)言JSON解碼函數(shù)Unmarshal的使用
本文主要介紹了Golang語(yǔ)言JSON解碼函數(shù)Unmarshal的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Golang并發(fā)繞不開(kāi)的重要組件之Goroutine詳解
Goroutine、Channel、Context、Sync都是Golang并發(fā)編程中的幾個(gè)重要組件,這篇文中主要為大家介紹了Goroutine的相關(guān)知識(shí),需要的可以參考一下2023-06-06
Go語(yǔ)言如何輕松編寫(xiě)高效可靠的并發(fā)程序
這篇文章主要為大家介紹了Go語(yǔ)言輕松編寫(xiě)高效可靠的并發(fā)程序?qū)崿F(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
golang?gorm學(xué)習(xí)之如何指定數(shù)據(jù)表
在sql中首先要指定是從哪張表中查詢(xún),所以這篇文章小編就來(lái)帶大家一起看一下gorm是如何根據(jù)model來(lái)自動(dòng)解析表名的,感興趣的小伙伴可以了解下2023-08-08
Golang使用反射的動(dòng)態(tài)方法調(diào)用詳解
Go是一種靜態(tài)類(lèi)型的語(yǔ)言,提供了大量的安全性和性能。這篇文章主要和大家介紹一下Golang使用反射的動(dòng)態(tài)方法調(diào)用,感興趣的小伙伴可以了解一下2023-03-03

