網站首頁 編程語言 正文
介紹
本文介紹如何通過 rk-boot 快速搭建 gRPC 超時攔截器。
什么是 gRPC 超時攔截器?
攔截器會攔截 gRPC 請求,并根據策略返回超時錯誤。
安裝
go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-grpc
快速開始
使用 rk-boot 啟動的 gRPC 服務。
支持全局超時和 API 超時設定。
1.創建 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.創建 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.驗證
發送 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-09-14 Go語言中序列化與反序列化示例詳解_Golang
- 2022-04-10 用python實現文件備份_python
- 2022-04-20 Httprunner簡介、安裝及基本使用教程_python
- 2022-08-04 Go?slice切片make生成append追加copy復制示例_Golang
- 2022-10-15 python-yml文件讀寫與xml文件讀寫_python
- 2022-03-15 使用axios出現跨域問題Response to preflight request doesn‘t
- 2023-07-05 React通過useContext特性實現組件數據傳遞
- 2022-08-23 構建?Python?命令行參數的?4?種常見方式_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支