在flask中使用python客户端时Grpc请求失败
我有一个flask应用程序,当向flask端点发出请求时,它需要向grpc服务器发出请求在flask中使用python客户端时Grpc请求失败,python,flask,grpc,grpc-python,Python,Flask,Grpc,Grpc Python,我有一个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 ha
@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)
但我不断收到一个错误,RPC的InactioverPCError终止为:StatusCode.UNAVAILABLE无法连接到所有地址
只需将客户机代码放在一个普通的.py
文件中就可以了,而将请求放在BloomRPC中也可以了,这样就不会成为服务器问题
这是否与烧瓶的工作方式有关,而我只是错过了一些东西?
我也尝试过这样使用,但没有成功:
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服务引用为核心配置文件:50051
主机名是Compose服务核心概要文件
,因为Python服务也在Compose网络中,所以它必须使用50051
localhost:30001
是如何从Compose主机访问它的您如何运行服务?grpc服务在docker容器中运行,使用与flask服务器相同的docker-Compose.yml文件。在您的问题中包含docker Compose.yml
文件会很有帮助。我怀疑(!?)您试图以localhost
的身份访问gRPC服务器,但Compose DNS(服务名称)给了它一个不同的名称。谢谢Daz,我已经添加了docker Compose.yml
文件。好的,因此您的Python应用程序应该将gRPC服务引用为核心配置文件:50051