美文网首页
gRPC 掉线重连

gRPC 掉线重连

作者: Feng_Sir | 来源:发表于2021-04-26 18:01 被阅读0次

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 掉线重连

      本文链接:https://www.haomeiwen.com/subject/mhberltx.html