无法连接到在 C# 程序中使用 Docker 启动的 RabbitMQ(使用 RabbitMQ.Client)
Posted
技术标签:
【中文标题】无法连接到在 C# 程序中使用 Docker 启动的 RabbitMQ(使用 RabbitMQ.Client)【英文标题】:Unable to connect to RabbitMQ started with Docker in a C# program (with RabbitMQ.Client) 【发布时间】:2019-09-22 05:11:24 【问题描述】:我按照文章启动了一个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访问管理工具
然后,我创建了有史以来最基本的 C# 项目,以 that article 与我的本地 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】:我的容器端口 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
【讨论】:
以上是关于无法连接到在 C# 程序中使用 Docker 启动的 RabbitMQ(使用 RabbitMQ.Client)的主要内容,如果未能解决你的问题,请参考以下文章
Docker:Springboot 容器无法连接到 PostgreSql 容器
AIR NativeProcess standardInput无法连接到应用。
Spring Boot 应用程序无法连接到 Docker 中的 Redis 副本