我有一个flask应用程序,无论何时遇到端点,该应用程序都需要与SQS服务进行交互。我正在使用sukumarporeddy/sqs:fp基本镜像为https://github.com/vsouza/docker- SQS-local的 docker镜像在本地模拟SQS服务,并在配置中添加了两个队列。
sukumarporeddy/sqs:fp
我需要从另一个以 app_service* 运行的 应用程序 访问此服务。这两个服务使用 docker-compose.yml 文件运行,其中我提到了两个服务。 ***
app_service
sqs_service
在构建 应用程序 的形象,我设置环境变量来访问 sqs_service 作为QUEUE_ENDPOINT=http://sqs_service:9324。但是,当我尝试访问该应用程序的 sqs_service时 ,它说的是无效的队列端点。
QUEUE_ENDPOINT=http://sqs_service:9324
我正在使用 boto3 连接到本地 sqs_service 。
boto3.client('sqs', endpoint_url=os.getenv("QUEUE_ENDPOINT"), region_name='default')
这是 docker-compose.yml 文件。
app_service: container_name: app_container restart: always image: app build: context: ./dsdp dockerfile: Dockerfile.app.local ports: - "5000:5000" env_file: - ./local_secrets.env command: flask run --host=0.0.0.0 --port 5000 sqs_service: container_name: sqs_container image: sukumarporeddy/sqs:fp ports: - "9324:9324"
local_secrets.env:
QUEUE_ENDPOINT=https://sqs_service:9324 FEEDER_QUEUE_URL=https://sqs_service:9324/queue/feeder PREDICTION_QUEUE_URL=https://sqs_service:9324/queue/prediction AWS_ACCESS_KEY_ID='' AWS_SECRET_ACCESS_KEY=''
尝试将消息发送到本地运行的SQS服务时遇到的错误。
ValueError ValueError:无效的终结点:https:// sqs_service:9324
ValueError
ValueError:无效的终结点:https:// sqs_service:9324
我在哪里犯错?
不知何故,我无法使用环境变量中提到的服务名称来使用SQS队列。这是我所做的。
从环境变量中获取服务名称,使用python中的 套接字 库获取服务的IP地址,并使用该IP地址来格式化和创建QUEUE端点url。
import socket queue_endpoint_service = os.getenv("QUEUE_ENDPOINT_SERVICE") queue_endpoint_port = os.getenv("QUEUE_ENDPOINT_PORT") feeder_queue = os.getenv("FEEDER_QUEUE") prediction_queue = os.getenv("PREDICTION_QUEUE") queue_endpoint_ip = socket.gethostbyname(queue_endpoint_service) queue_endpoint = f"http://{queue_endpoint_ip}:{queue_endpoint_port}" mc = boto3.client('sqs', endpoint_url=queue_endpoint, region_name='default') feeder_queue_url = f"{queue_endpoint}/queue/{feeder_queue}" prediction_queue_url = f"{queue_endpoint}/queue/{prediction_queue}"
我现在可以通过点击flask应用程序中的端点来发送消息。
注意: 还使用了 Adiii 提到的新 docker 映像。不再使用旧图像。