Kotlin / Java grpc 检查连接状态

Kotlin / Java grpc check connection status

我正在用 Kotlin 编写一个应用程序来连接到 grpc 服务器并获取数据。

下面是我的代码片段:

channel = ManagedChannelBuilder.forTarget("localhost:25001")
                .usePlaintext()
                .build()

stub = hello.HelloGrpc.newBlockingStub(channel)

我的 GRPC 服务器 down,但我可以看到两个变量的值如下:

for channel = channelManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=1, target=localhost:25001}}
for stub = channelhello.HelloGrpc$HelloBlockingStub@19b843ba

我很困惑,如何检查连接是否处于活动状态。

目前,我在获取值时添加了 try catch 语句,这会引发错误

UNAVAILABLE: io exception

只是想知道,有什么方法可以检查我的连接状态并尝试重新连接断开的连接。

gRPC automatically attempts re-connection,使用指数退避。只要继续使用频道,问题就会在问题解决后消失。

如果您对频道当前的连接状态感兴趣,可以使用ManagedChannel.getState()