使用Docker链接时拒绝与RabbitMQ的连接 [英] Refused connection to RabbitMQ when using docker link
问题描述
我有一个微服务应用程序,它有两个服务,还有一个兔子mq用作它们之间进行通信的消息队列.现在,我想将它们部署在docker上.我在docker-compose.yml
文件中有以下代码:
I have a microservices application which has two services and a rabbit mq used as a message queue for communication between them. Now, I want to deploy them on docker. I have the following code in the docker-compose.yml
file:
版本:"3" 服务:
rabbitmq:
build: ./Rabbit
hostname: "rabbitmq"
container_name: "rabbitmq"
environment:
RABBITMQ_ERLANG_COOKIE: "cookie"
RABBITMQ_DEFAULT_USER: "user"
RABBITMQ_DEFAULT_PASS: "pass"
RABBITMQ_DEFAULT_VHOST: "/"
ports:
- "15672:15672"
- "5672:5672"
# labels:
# NAME: "rabbit1"
volumes:
- "/opt/rabbitmq:/var/lib/rabbitmq"
service1:
build: ./service1
deploy:
replicas: 5
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.1"
memory: 50M
ports:
- "8181:80"
depends_on:
- rabbitmq
links:
- rabbitmq
networks:
- webnet
因此,这里我在容器中构建RabbitMQ映像,然后将此容器链接到service1
的容器.由于service1
是ASP.NET Core Web API,因此我使用以下设置连接到消息队列:
So, here I build the RabbitMQ image in a container and then link this container to the container of service1
. Since service1
one is an ASP.NET Core Web API, I use the following setup to connect to the message queue:
//Establish the connection
var factory = new ConnectionFactory
{
HostName = "rabbitmq",
Port = 5672,
UserName = "user",
Password = "pass",
VirtualHost = "/",
AutomaticRecoveryEnabled = true,
NetworkRecoveryInterval = TimeSpan.FromSeconds(15)
};
但是当我尝试运行docker-compose up
时,收到以下错误消息:
But when I try to run docker-compose up
, I receive the following error message:
未处理的异常: RabbitMQ.Client.Exceptions.BrokerUnreachableException:无 指定的端点可以访问---> RabbitMQ.Client.Exceptions.ConnectFailureException:连接失败 ---> System.Net.Internals.SocketExceptionFactory + ExtendedSocketException: 没有这样的设备或地址
Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
也许我在HostName
中有一个错误,但是我不确定如何纠正它.
Maybe I have a mistake in the HostName
but I am not sure how to correct it.
推荐答案
有两个问题需要解决:
-
这两个服务不属于同一网络,因此您需要将
rabbitmq
服务添加到webnet
网络或为这两个服务创建新的网络
the two services do not belong to the same network so you need to add the
rabbitmq
service to thewebnet
network or create a new network for the two services
rabbitmq
可能需要一些时间才能完全可用(即监听5672
端口),因此您需要使service1
服务等待rabbitmq
服务;有关此问题,请参见此问题.
rabbitmq
may take some time to become fully available (i.e. to listen to the 5672
port) so you need to make the service1
service to wait for the rabbitmq
service; see this question about this issue.
这篇关于使用Docker链接时拒绝与RabbitMQ的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!