GRPC的ConnectivityState状态不正常工作?

huangapple go评论82阅读模式
英文:

GRPC ConnectivityState is not working true?

问题

ManagedChannel managedChannel = ManagedChannelBuilder
                        .forAddress("localhost", 9090)
                        .usePlaintext().build();

System.out.println("State: " + managedChannel.getState(false)) // State: IDLE

Grpc.Stub stub = Grpc.newStub(managedChannel);

System.out.println("State: " + managedChannel.getState(false)) // State: IDLE

stub.callMethod();

System.out.println("State: " + managedChannel.getState(false)) // State: IDLE

以上代码是 GRPC 客户端代码。它与 GRPC 服务器建立连接。但每次通道状态都是 IDLE?什么时候是 CONNECTING 和 READY 状态?有什么问题吗?

英文:
ManagedChannel managedChannel = ManagedChannelBuilder
                    .forAddress("localhost", 9090)
                    .usePlaintext().build();

System.out.println("State: " + managedChannel .getState(false)) // State: IDLE

Grpc.Stub stub = Grpc.newStub(managedChannel);

System.out.println("State: " + managedChannel .getState(false)) // State: IDLE

stub.callMethod();

System.out.println("State: " + managedChannel .getState(false)) // State: IDLE

Above code is GRPC client. It get connection with grpc server. But every time the channel state is IDLE? When is CONNECTING and READY state? Whats wrong?

答案1

得分: 1

需要等待另一种状态。
当服务器未启动时,以下代码会打印connection state: TRANSIENT_FAILURE,当服务器健康时会打印connection state: READY
gRPC版本:1.24.0

ManagedChannel managedChannel = ManagedChannelBuilder
    .forAddress("localhost", 9090)
    .usePlaintext().build();
 
ConnectivityState state = channel.getState(true);
while (state == ConnectivityState.IDLE || state == ConnectivityState.CONNECTING) {
    state = channel.getState(true);
}

System.out.println("connection state: " + state.toString());
英文:

It is required to wait for another state.
The following code prints connection state: TRANSIENT_FAILURE when server is not started and connection state: READY when it is healthy.
gRPC version: 1.24.0

ManagedChannel managedChannel = ManagedChannelBuilder
    .forAddress("localhost", 9090)
    .usePlaintext().build();
 
ConnectivityState state = channel.getState(true);
while (state == ConnectivityState.IDLE || state == ConnectivityState.CONNECTING) {
    state = channel.getState(true);
}

System.out.println("connection state: " + state.toString());

huangapple
  • 本文由 发表于 2020年10月4日 20:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64194562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定