我有一个微服务应用程序,它有两个服务,还有一个兔子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 the webnet 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的连接
发布评论