在谷歌云上运行Python gRPC服务器

在谷歌云上运行Python gRPC服务器,python,grpc,google-cloud-run,Python,Grpc,Google Cloud Run,我有一个基本的概念验证Python gRPC服务器 当我在本地运行docker容器时,我可以向服务器发出请求,并在公开的端口上接收响应 我可以成功地将服务器部署到Cloud Run,并且我可以看到服务在Cloud Run UI中运行 但是,我无法从客户端访问云运行版本 我正在寻找帮助我访问此服务器的建议,无论是更改客户端还是服务器 客户端代码: 以grpc.unsecure_频道('.-uc.a.run.app:80')作为频道: 存根=税务服务存根(通道) response=stub.GetT

我有一个基本的概念验证Python gRPC服务器

当我在本地运行docker容器时,我可以向服务器发出请求,并在公开的端口上接收响应

我可以成功地将服务器部署到Cloud Run,并且我可以看到服务在Cloud Run UI中运行

但是,我无法从客户端访问云运行版本

我正在寻找帮助我访问此服务器的建议,无论是更改客户端还是服务器

客户端代码:

以grpc.unsecure_频道('.-uc.a.run.app:80')作为频道:
存根=税务服务存根(通道)
response=stub.GetTaxRate(tax\u service\u pb2.GetTaxRate请求(zipcode='12345'))
打印(“税务客户端接收:{}”。格式(响应。税率))
如果我尝试连接到端口80,我会收到以下消息:

    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
    status = StatusCode.UNAVAILABLE
    details = "Trying to connect an http1.x server"
    debug_error_string = "{"created":"@1575613033.176590200","description":"Error received from peer ipv4:216.239.36.53:80","file":"src/core/lib/surface/call.cc","file_line":1055,"grpc_message":"Trying to connect an http1.x server","grpc_status":14}"

我认为您的问题是您试图在SSL enpoint上创建一个不安全的通道。我使用这部分代码创建了我的客户端通道:

with open('certificate.pem', 'rb') as f:
  creds = grpc.ssl_channel_credentials(f.read())
  channel = grpc.secure_channel('predict-<PROJECTHASH>-uc.a.run.app:443',creds)
  stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
  ...
以open('certificate.pem','rb')作为f的
:
creds=grpc.ssl\u通道\u凭证(f.read())
频道=grpc.secure_频道('predict--uc.a.run.app:443',creds)
存根=预测\服务\ pb2\ grpc.预测服务存根(通道)
...

您可以从web浏览器下载证书。

我认为您的问题在于您试图在SSL enpoint上创建一个不安全的通道。我使用这部分代码创建了我的客户端通道:

with open('certificate.pem', 'rb') as f:
  creds = grpc.ssl_channel_credentials(f.read())
  channel = grpc.secure_channel('predict-<PROJECTHASH>-uc.a.run.app:443',creds)
  stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
  ...
以open('certificate.pem','rb')作为f的
:
creds=grpc.ssl\u通道\u凭证(f.read())
频道=grpc.secure_频道('predict--uc.a.run.app:443',creds)
存根=预测\服务\ pb2\ grpc.预测服务存根(通道)
...

您可以从web浏览器下载证书。

服务器端无需更改

Cloud Run使用不安全的端口访问gRPC服务,然后在Google的云边缘和客户端之间添加SSL

在我的客户机上,我需要使用系统根证书才能访问服务

使用grpc.secure_通道('-uc.a.run.app:443',grpc.ssl_通道_凭据())作为通道:
存根=税务服务存根(通道)
response=stub.GetTaxRate(tax\u service\u pb2.GetTaxRate请求(zipcode='12345'))
打印(“税务客户端接收:{}”。格式(响应。税率))

服务器端不需要任何更改

Cloud Run使用不安全的端口访问gRPC服务,然后在Google的云边缘和客户端之间添加SSL

在我的客户机上,我需要使用系统根证书才能访问服务

使用grpc.secure_通道('-uc.a.run.app:443',grpc.ssl_通道_凭据())作为通道:
存根=税务服务存根(通道)
response=stub.GetTaxRate(tax\u service\u pb2.GetTaxRate请求(zipcode='12345'))
打印(“税务客户端接收:{}”。格式(响应。税率))

您是否在Cloud Run提供的
$PORT
中运行服务器?也许可以尝试设置一个只返回200的简单端点,看看是否可以点击该端点并返回响应?

您是否在Cloud Run提供的
$PORT
中运行服务器?也许可以尝试设置一个只返回200的简单端点,看看是否可以点击该端点并返回响应