我已经使用this docker image在我的开发机器上设置了RabbitMQ服务器。

我已经使用下面的命令来设置我的容器
docker run -d --name my-rabbit -p 5672:15672 rabbitmq:3-management
下面是docker ps命令输出
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa40704b7f3a4 rabbitmq:3-management "docker-entrypoint.s…" 13 minutes ago Up 12 minutes 4369/tcp, 5671-5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:5672->15672/tcp my-rabbit
可通过http://localhost:5672访问管理控制台,我可以使用默认的用户名和密码(guest / guest)登录

以下是我的.Net Core代码

public RabbitMQMnager()
        {
            var factory = new ConnectionFactory();
            factory.Port = 5672;
            Uri uri = new Uri("amqp://guest:guest@localhost:5672/");

            var connection = factory.CreateConnection();

            //Below are values of different connection string parameters
            factory.HostName = "localhost";
            factory.UserName = "guest";
            factory.Password = "guest";
            factory.VirtualHost = "/";
            factory.Port = 5672;

            var channel = connection.CreateModel(); //<- Exception here
        }

执行以上代码后,我将获得以下异常。
RabbitMQ.Client.Exceptions.BrokerUnreachableException: 'None of the specified endpoints were reachable'

堆栈跟踪
This exception was originally thrown at this call stack: RabbitMQ.Client.Framing.Impl.Connection.StartAndTune() RabbitMQ.Client.Framing.Impl.Connection.Open(bool) RabbitMQ.Client.Framing.Impl.Connection.Connection(RabbitMQ.Client.IConnectionFactory, bool, RabbitMQ.Client.Impl.IFrameHandler, string) RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(RabbitMQ.Client.Impl.IFrameHandler) RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(RabbitMQ.Client.IEndpointResolver) RabbitMQ.Client.ConnectionFactory.CreateConnection(RabbitMQ.Client.IEndpointResolver, string)
我发现了类似的问题,但提到的解决方案对我来说还不够。似乎与docker和docker创建的网络有关。

Refused connection to RabbitMQ when using docker link

其他详情
  • .Net Core 3.1应用程序
  • RabbitMQ.Client-> 5.1.2

    更新1

  • 根据@ThisIsNoZaku的答案,暴露了其他端口解决了我的问题。
    docker run -d --hostname my-rabbit --name my-rabbit -p 15672:15672 -p  5672:5672 rabbitmq:3-management
    

    最佳答案

    使用AMQP客户端连接到RabbitMQ实例以及管理控制台UI(容器中的15762,您已将其映射到主机上的5762)的端口是不同的。当应用程序尝试将AMQP客户端指向管理端口时,它会失败because it's not supported:



    您的RabbitMQ实例应公开并连接到correct ports for AMQP:

    关于docker - Docker-RabbitMQ.Client.Exceptions.BrokerUnreachableException: 'None of the specified endpoints were reachable',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61092733/

    10-16 12:18