在C#程序中(使用RabbitMQ.Client)无法连接到以Docker启动的RabbitMQ

时间:2019-05-03 16:01:16

标签: c# docker .net-core rabbitmq

我根据以下文章启动了RabbitMQ容器:https://docs.docker.com/samples/library/rabbitmq使用包含管理工具的图像

docker run -d --hostname my-rabbit --name some-rabbit -p 8080:15672 rabbitmq:3-management
docker container ls --all
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS
         NAMES
8a42bb749074        rabbitmq:3-management   "docker-entrypoint.s…"   8 hours ago         Up 8 hours          4369/tcp, 5671-5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:8080->15672/tcp   some-rabbit

我可以通过http://localhost:8080

访问管理工具

然后我创建了that article之后最基本的C#项目,以与我的RabbitMQ本地实例进行通信:

using System.Globalization;
using RabbitMQ.Client;

namespace RabbitCSharp
{
    public static class Program
    {
        public static void Main(params string[] args)
        {
            var factory = new ConnectionFactory
            {
                HostName = "localhost"
            };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                }
            }
        }
    }
}

但不幸的是出现了以下错误:

Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> System.AggregateException
: One or more errors occurred. (Connection failed) ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.S
ocketExceptionFactory+ExtendedSocketException: No connection could be made because the target machine actively refused it 127.0.0.1:5672
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.<>c.<ConnectAsync>b__272_0(IAsyncResult iar)
--- End of stack trace from previous location where exception was thrown ---
   at RabbitMQ.Client.TcpClientAdapter.ConnectAsync(String host, Int32 port)
   at RabbitMQ.Client.Impl.TaskExtensions.TimeoutAfter(Task task, Int32 millisecondsTimeout)
   at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, Int32 timeout)
   --- End of inner exception stack trace ---
   at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingAddressFamily(AmqpTcpEndpoint endpoint, Func`2 socketFactory, Int32 timeout, AddressFamily fami
ly)
   at RabbitMQ.Client.Impl.SocketFrameHandler..ctor(AmqpTcpEndpoint endpoint, Func`2 socketFactory, Int32 connectionTimeout, Int32 readTimeout, Int32 wri
teTimeout)
   at RabbitMQ.Client.ConnectionFactory.CreateFrameHandler(AmqpTcpEndpoint endpoint)
   at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
   --- End of inner exception stack trace ---
   at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
   at RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(IEndpointResolver endpoints)
   at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
   --- End of inner exception stack trace ---
   at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
   at RabbitCSharp.Program.Main(String[] args) in C:\Users\eperret\Desktop\RabbitCSharp\RabbitCSharp\RabbitCSharp\Program.cs:line 14

Process finished with exit code -532,462,766.

不太确定为什么RabbitClient无法通过127.0.0.1:5672

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

似乎我的容器端口5672没有映射到本地端口5672。

我刚刚创建了另一个包含端口映射的容器,如this answer所示:

docker stop some-rabbit
docker commit some-rabbit some-rabbit-right
docker run -p 5672:5672 -p 8080:15672 -td some-rabbit-right

或者直接运行新实例:

docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 8080:15672 rabbitmq:3-management