使用Docker链接时拒绝与RabbitMQ的连接

编程入门 行业动态 更新时间:2024-10-12 01:32:48
本文介绍了使用Docker链接时拒绝与RabbitMQ的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

    本文发布于:2023-11-16 14:50:52,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1605267.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:链接   Docker   RabbitMQ

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!