gRPC超時(shí)攔截器實(shí)現(xiàn)示例
介紹
本文介紹如何通過 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
快速開始
使用 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ì)超過 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)文章
Go?Excelize?API源碼解析GetSheetFormatPr使用示例
這篇文章主要為大家介紹了Go?Excelize?API源碼解析GetSheetFormatPr使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Go語言實(shí)現(xiàn)服務(wù)端消息接收和發(fā)送
這篇文章主要為大家詳細(xì)介紹了Go語言實(shí)現(xiàn)服務(wù)端消息接收和發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Go語言循環(huán)遍歷含有中文的字符串的方法小結(jié)
這篇文章主要介紹了Go語言循環(huán)遍歷含有中文的字符串的幾種方法,文章通過代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來看看吧2023-07-07golang時(shí)間字符串和時(shí)間戳轉(zhuǎn)換的案例
這篇文章主要介紹了golang時(shí)間字符串和時(shí)間戳轉(zhuǎn)換的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12Golang實(shí)現(xiàn)不被復(fù)制的結(jié)構(gòu)體的方法
sync包中的許多結(jié)構(gòu)都是不允許拷貝的,因?yàn)樗鼈冏陨泶鎯?chǔ)了一些狀態(tài)(比如等待者的數(shù)量),如果你嘗試復(fù)制這些結(jié)構(gòu)體,就會(huì)在你的?IDE中看到警告,那這是怎么實(shí)現(xiàn)的呢,下文就來和大家詳細(xì)講講2023-03-03Go設(shè)計(jì)模式原型模式考查點(diǎn)及使用詳解
這篇文章主要為大家介紹了Go設(shè)計(jì)模式原型模式考查點(diǎn)及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12go語言簡(jiǎn)單的處理http請(qǐng)求的函數(shù)實(shí)例
這篇文章主要介紹了go語言簡(jiǎn)單的處理http請(qǐng)求的函數(shù),實(shí)例分析了Go語言處理http請(qǐng)求的技巧,需要的朋友可以參考下2015-03-03