gRPC超时拦截器如何实现

发布时间:2022-06-16 10:06:37 作者:iii
来源:亿速云 阅读:193

这篇文章主要介绍“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!"
            }
        ]
    }
}

到此,关于“gRPC超时拦截器如何实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. gRPC HelloWorld测试
  2. gRPC快速入门

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

grpc

上一篇:QT如何利用QProcess获取计算机硬件信息

下一篇:node可不可以开启多线程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》