gRPC 掉线重连

2个grpc server服务
1个grpc client服务

假设一个服务挂掉.png

处理方式
详见看源码demo中keepalivlie.go中可以设置

// Package keepalive defines configurable parameters for point-to-point
// healthcheck.
package keepalive
// ClientParameters is used to set keepalive parameters on the client-side.
// These configure how the client will actively probe to notice when a
// connection is broken and send pings so intermediaries will be aware of the
// liveness of the connection. Make sure these parameters are set in
// coordination with the keepalive policy on the server, as incompatible
// settings can result in closing of connection.
type ClientParameters struct {
    // After a duration of this time if the client doesn't see any activity it
    // pings the server to see if the transport is still alive.
    // If set below 10s, a minimum value of 10s will be used instead.
    Time time.Duration // The current default value is infinity.
    // After having pinged for keepalive check, the client waits for a duration
    // of Timeout and if no activity is seen even after that the connection is
    // closed.
    Timeout time.Duration // The current default value is 20 seconds.
    // If true, client sends keepalive pings even with no active RPCs. If false,
    // when there are no active RPCs, Time and Timeout will be ignored and no
    // keepalive pings will be sent.
    PermitWithoutStream bool // false by default.
}

代码添加grpc.WithKeepaliveParams,重启服务

    conn, err := grpc.DialContext(ctx, addr,
            grpc.WithInsecure(),
            grpc.WithBlock(),
            grpc.WithKeepaliveParams(keepalive.ClientParameters{
                Time:                10 * time.Second,
                Timeout:             100 * time.Millisecond,
                PermitWithoutStream: true}),
        )

重新测试,自动重连,问题解决了


问题解决了
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近遇到 grpc 客户端报错 rpc error: code = Unavailable desc = tran...
    hatlonely阅读 2,076评论 0 1
  • golang 的grpc库是 https://github.com/grpc/grpc-go grpc serve...
    cc180912阅读 2,442评论 0 1
  • 本文转载自用Golang构建gRPC服务[https://zhuanlan.zhihu.com/p/8550838...
    雪域迷影阅读 1,976评论 0 0
  • gRPC是由Google主导开发的RPC框架,使用HTTP/2协议并用ProtoBuf作为序列化工具。其客户端提供...
    CZ_Golang阅读 82,190评论 9 71
  • 原文出处:gRPC gRPC分享 概述 gRPC 一开始由 google 开发,是一款语言中立、平台中立、开源的远...
    小波同学阅读 7,279评论 0 18