網(wǎng)站首頁 編程語言 正文
介紹
本文介紹如何通過 rk-boot 快速搭建 gRPC 超時攔截器。
什么是 gRPC 超時攔截器?
攔截器會攔截 gRPC 請求,并根據(jù)策略返回超時錯誤。
安裝
go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-grpc
快速開始
使用 rk-boot 啟動的 gRPC 服務。
支持全局超時和 API 超時設定。
1.創(chuàng)建 boot.yaml
boot.yaml 文件告訴 rk-boot 如何啟動 gRPC 服務。
為了驗證,我們啟動了 commonService,commonService 里包含了一系列常用 API,例如 /rk/v1/gc。
設定全局超時為 5秒,讓 GC 的超時時間定位 1 毫秒,GC 一般會超過 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.啟動 main.go
$ go run main.go
4.驗證
發(fā)送 GC 請求。
$ 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!" } ] } }
原文鏈接:https://juejin.cn/post/7030478524523741221
相關推薦
- 2022-11-05 Android實現(xiàn)雙曲線折線圖_Android
- 2024-01-28 使用element-ui代碼沒有提示
- 2022-06-18 C#如何綁定多個按鈕到同一個事件_C#教程
- 2023-01-13 Python?CNN卷積神經(jīng)網(wǎng)絡實戰(zhàn)教程深入講解_python
- 2022-04-08 IIS部署ASP.NET?Core項目及常見問題總結(jié)_基礎應用
- 2023-01-01 c語言中實現(xiàn)數(shù)組幾個數(shù)求次大值_C 語言
- 2022-05-28 python爬蟲框架scrapy下載中間件的編寫方法_python
- 2022-06-01 C語言?深入淺出講解指針的使用_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支