我对 docker 中的 rabbitmq 队列有疑问。当我通过 dotnet run 正常运行项目时,一切正常,但是当我通过 docker 运行项目时,出现错误:
dateapp-container | Unhandled exception. RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable
dateapp-container | ---> System.AggregateException: One or more errors occurred. (Connection failed)
dateapp-container | ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed
我的配置:
码头工人撰写
version: '3.4'
services:
rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
ports:
- "5672:5672"
- "15672:15672"
volumes:
- ./rabbitmq-data:/var/lib/rabbitmq
api:
image: ${DOCKER_REGISTRY-}dateapp
environment:
- RABBIT_HOSTNAME=host.docker.internal
container_name: dateapp-container
build:
context: .
dockerfile: MicroServices/DateApp/DateApp.Application/Dockerfile
ports:
- 7141:7141
depends_on:
- rabbitmq
接收器
var rabbitHostName = Environment.GetEnvironmentVariable("RABBIT_HOSTNAME");
var factory = new ConnectionFactory
{
HostName = rabbitHostName ?? "localhost",
UserName = "guest",
Password = "guest"
};
目前的方法是可行的,但是在以下情况下会出现上面的错误:
输入 HostName localhost,我想以 docker 身份运行项目
输入主机名 host.docker.internal 并希望通过 dotnet run 运行项目
我的主要问题是除了 HostName = rabbitHostName 之外,是否可以编写代码? “本地主机”,