如何知道GRPC服务器是否可用

如何知道GRPC服务器是否可用

问题描述:

如果gRPC客户端在服务器之前启动,则它将退出,并出现异常.但是,我正在处理的应用程序同时启动了一堆服务器和客户端(附近),并且我必须在执行顺序方面保持稳健.如果客户端在服务器之前启动,我希望它等待服务器出现.

If a gRPC client starts before the server, it exits with an exception. The application I am working on starts a bunch of servers and clients (near) simultaneously, however, and I have to be robust with respect to execution order. If the client starts before the server, I want it to wait until the server shows up.

我修改了HelloWorld python客户端示例,如下所示:

I modified the HelloWorld python client example as follows:

done = False
while not done:
    try:
        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), timeout=1)
        print("Greeter client received: " + response.message)
        done = True
    except:
        print('Waiting for server connect...')
        time.sleep(1)

所以现在,如果我在服务器之前启动客户端,则会收到正在等待服务器连接..."消息,按预期方式在终端窗口中向上滚动.然后,我启动服务器,然后服务器连接起来……最终.实际上,您好"消息出现之前大约需要十秒钟,这是令人惊讶的长.为什么会这么慢,并且有一种健壮的方法来检查服务器连接?

so now if I start the client before the server, I get the 'Waiting for server connect...' message scrolling up my terminal window as expected. Then I start the server, and it connects... eventually. In fact it takes about ten seconds before the 'Hello you' messages appears, which is surprisingly long. Why might it be so slow, and is there a robust way to check for server connection?

看看

Take a look at ChannelConnectivity - I am a go programmer but it should be the same in python. What I do is create while/for loop and when the "ChannelConnectivity" is set to "READY" I create the client from the connection and then continue.