在我的项目中,我在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()
您缺少服务器的主机名或IP。如果两个容器在同一主机上运行或将它们放在同一网络中并使用DNS记录进行发现,则使用docker inspect <containerid>
获取容器IP。
这是GRPC下方的一层,因此任何网络调试步骤都会在这里为您提供帮助。