在亚马逊EC2上构建gRPC服务器

5

当我尝试在Amazon EC2实例上构建gRPC服务器/客户端时,遇到了问题。

我有一个实例A(私有IP为1.2.3.4)。服务器代码如下:

from concurrent import futures
import time
import math

import grpc

import helloworld_pb2

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

class Greeter(helloworld_pb2.GreeterServicer):

  def SayHello(self, request, context):
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


def serve():
  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
  helloworld_pb2.add_GreeterServicer_to_server(Greeter(), server)
  server.add_insecure_port('1.2.3.4:50051')

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

if __name__ == '__main__':
  serve()

另一方面,实例B具有私有IP 2.3.4.5,我希望在其上运行客户端脚本。

from __future__ import print_function

import grpc

import helloworld_pb2


def run():
  channel = grpc.insecure_channel('1.2.3.4:50051')
  stub = helloworld_pb2.GreeterStub(channel)
  response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
  print("Greeter client received: " + response.message)


if __name__ == '__main__':
  run()

客户端和服务器代码在本地机器上运行良好。但是,当我尝试在EC2集群上运行它们时,客户端无法找到服务器。

Traceback (most recent call last):
  File "helloworld_client.py", line 47, in <module>
    run()
  File "helloworld_client.py", line 42, in run
    response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
  File "/usr/local/lib/python3.4/dist-packages/grpc/_channel.py", line 481, in __call__
    return _end_unary_response_blocking(state, False, deadline)
  File "/usr/local/lib/python3.4/dist-packages/grpc/_channel.py", line 432, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, )>

我应该怎么做才能使脚本运行?

谢谢。

1个回答

1
我找到了问题所在。通过设置安全组 - 输入 - 类型 - 所有流量,服务器和客户端之间的连接可以正常工作。

这里 gRPC 使用的不是特定的端口吗? - Shih-Min Lee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接