python 客户端在烧瓶中使用时 Grpc 请求失败
Grpc requests failing when python client used inside flask
我有一个 Flask 应用程序,当向 Flask 端点发出请求时,它需要向 grpc 服务器发出请求。
@main.route("/someroute", methods=["POST"])
def some_function():
# Do something here
make_grpc_request(somedata)
return create_response(data=None, message="Something happened")
def make_grpc_request(somedata):
channel = grpc.insecure_channel('localhost:30001')
stub = some_proto_pb2_grpc.SomeServiceStub(channel)
request = some_proto_pb2.SomeRequest(id=1)
response = stub.SomeFunction(request)
logger.info(response)
但我一直收到错误消息InactiveRpcError of RPC that terminated with: StatusCode.UNAVAILABLE failed to connect to all addresses
只需将客户端代码放入一个普通的 .py
文件中就可以了,在 BloomRPC 中发出请求也可以正常工作,所以这不可能是服务器问题。
这与 Flask 的工作原理有关吗?我只是遗漏了什么?
我也试过使用 https://github.com/public/sonora 但没有像这样成功:
with sonora.client.insecure_web_channel("localhost:30001") as channel:
stub = some_proto_pb2_grpc.SomeServiceStub(channel)
request = some_proto_pb2.SomeRequest(id=1)
response = stub.SomeFunction(request)
docker-compose.yml
version: "3.7"
services:
core-profile: #This is where the grpc requests are sent to
container_name: core-profile
build:
context: ./app/profile/
target: local
volumes:
- ./app/profile/:/usr/src/app/
env_file:
- ./app/profile/database.env
- ./app/profile/jwt.env
- ./app/profile/oauth2-dev.env
environment:
- APP_PORT=50051
- PYTHONUNBUFFERED=1
- POSTGRES_HOST=core-profile-db
ports:
- 30001:50051
expose:
- 50051
depends_on:
- core-profile-db
core-profile-db:
image: postgres:10-alpine
expose:
- 5432
ports:
- 54321:5432
env_file:
- ./app/profile/database.env
app-flask-server-db:
image: postgres:10-alpine
expose:
- 5433
ports:
- 54333:5433
env_file:
- ./app/flask-server/.env
flask-server:
build:
context: ./app/flask-server/
dockerfile: Dockerfile-dev
volumes:
- ./app/flask-server:/usr/src/app/
env_file:
- ./app/flask-server/.env
environment:
- FLASK_ENV=docker
ports:
- 5000:5000
depends_on:
- app-flask-server-db
volumes:
app-flask-server-db:
name: app-flask-server-db
您的 Python 应用(服务)应将 gRPC 服务引用为 core-profile:50051
。
主机名是 Compose 服务 core-profile
,因为 Python 服务也在 Compose 网络中,它必须使用 50051
.
localhost:30001
是您从 Compose 主机访问它的方式
我有一个 Flask 应用程序,当向 Flask 端点发出请求时,它需要向 grpc 服务器发出请求。
@main.route("/someroute", methods=["POST"])
def some_function():
# Do something here
make_grpc_request(somedata)
return create_response(data=None, message="Something happened")
def make_grpc_request(somedata):
channel = grpc.insecure_channel('localhost:30001')
stub = some_proto_pb2_grpc.SomeServiceStub(channel)
request = some_proto_pb2.SomeRequest(id=1)
response = stub.SomeFunction(request)
logger.info(response)
但我一直收到错误消息InactiveRpcError of RPC that terminated with: StatusCode.UNAVAILABLE failed to connect to all addresses
只需将客户端代码放入一个普通的 .py
文件中就可以了,在 BloomRPC 中发出请求也可以正常工作,所以这不可能是服务器问题。
这与 Flask 的工作原理有关吗?我只是遗漏了什么?
我也试过使用 https://github.com/public/sonora 但没有像这样成功:
with sonora.client.insecure_web_channel("localhost:30001") as channel:
stub = some_proto_pb2_grpc.SomeServiceStub(channel)
request = some_proto_pb2.SomeRequest(id=1)
response = stub.SomeFunction(request)
docker-compose.yml
version: "3.7"
services:
core-profile: #This is where the grpc requests are sent to
container_name: core-profile
build:
context: ./app/profile/
target: local
volumes:
- ./app/profile/:/usr/src/app/
env_file:
- ./app/profile/database.env
- ./app/profile/jwt.env
- ./app/profile/oauth2-dev.env
environment:
- APP_PORT=50051
- PYTHONUNBUFFERED=1
- POSTGRES_HOST=core-profile-db
ports:
- 30001:50051
expose:
- 50051
depends_on:
- core-profile-db
core-profile-db:
image: postgres:10-alpine
expose:
- 5432
ports:
- 54321:5432
env_file:
- ./app/profile/database.env
app-flask-server-db:
image: postgres:10-alpine
expose:
- 5433
ports:
- 54333:5433
env_file:
- ./app/flask-server/.env
flask-server:
build:
context: ./app/flask-server/
dockerfile: Dockerfile-dev
volumes:
- ./app/flask-server:/usr/src/app/
env_file:
- ./app/flask-server/.env
environment:
- FLASK_ENV=docker
ports:
- 5000:5000
depends_on:
- app-flask-server-db
volumes:
app-flask-server-db:
name: app-flask-server-db
您的 Python 应用(服务)应将 gRPC 服务引用为 core-profile:50051
。
主机名是 Compose 服务 core-profile
,因为 Python 服务也在 Compose 网络中,它必须使用 50051
.
localhost:30001
是您从 Compose 主机访问它的方式