在 Amazon EC2 上构建 gRPC 服务器

foi*_*ois 5 communication cluster-computing amazon-ec2 server grpc

当我尝试在 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()
Run Code Online (Sandbox Code Playgroud)

另一方面,实例 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()
Run Code Online (Sandbox Code Playgroud)

客户端和服务器代码在本地机器上运行良好。但是,当我尝试在 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, )>
Run Code Online (Sandbox Code Playgroud)

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

谢谢。

foi*_*ois 1

我发现问题出在哪里了。通过设置安全组-输入-类型-所有流量,服务器和客户端之间的连接正常。