在docker container grpc客户端内部既不接收响应也不接收状态

问题描述 投票:0回答:1

在我的项目中,我在Docker容器内的两个微服务之间进行了一次grpc通信。当客户端是本地客户端且服务器位于容器中时,客户端将请求发送到服务器并接收响应。当我将客户端放在一个容器中而服务器放在另一个容器中时,客户端发出请求,既不接收响应也不接收状态。

这是我的客户:

class RoomClient:
    def __init__(self, host='', port=50051):
        conn_str = '{}:{}'.format(host, port)
        self.channel = grpc.insecure_channel(conn_str)
        self.stub = booking_pb2_grpc.BookingStub(self.channel)

    # Login call this method
    def rpc_run_get_all(self, request):
        number = booking_pb2.AddRequest(value=request)
        response = self.stub.sendAll(number)
        return response

我的服务器:

class BookingServicer(booking_pb2_grpc.BookingServicer):

    def sendAll(self, request, context):
        response = booking_pb2.AddReply()
        #response.value = send_all(request.value)
        print ("chegou")
        response.value = str(json_util.dumps({'response': "hello"}))
        return response


def serve():

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    booking_pb2_grpc.add_BookingServicer_to_server(BookingServicer(), server)

    print('Starting server. Listening on port 50051.')
    server.add_insecure_port('[::]:50051')
    server.start()

    try:
        while True:
            time.sleep(86400)
    except KeyboardInterrupt:
        server.stop(0)

if __name__ == '__main__':
  serve()
python-3.x docker grpc
1个回答
2
投票

您缺少服务器的主机名或IP。如果两个容器在同一主机上运行或将它们放在同一网络中并使用dns记录进行发现,请使用docker inspect <containerid>获取容器IP。

这是GRPC下面的层,因此任何网络调试步骤都会对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.