使用MassTransit的RabbitMQ集群连接提供了BrokerUnreachableException

时间:2019-05-10 08:58:08

标签: c# rabbitmq masstransit

我遇到了与这个人完全相同的问题:https://github.com/MassTransit/MassTransit/issues/975

出于测试目的,我的开发机上有一个3节点集群,节点使用了以下端口:

amqp rabbit 5672
http rabbit 15672

amqp rabbit1 6001
http rabbit1 15674

amqp rabbit2 6002
http rabbit2 15673


我尝试如下连接:

var rabbitMqHost = busFactoryConfig.Host(new Uri("rabbitmq://localhost:6002"), hostConfig =>
{
  hostConfig.Username("username");
  hostConfig.Password("password");
  hostConfig.UseCluster(c =>
  {
    c.ClusterMembers = new[]
    {
      "rabbitmq://localhost:6001",
      "rabbitmq://localhost:6002",
      "rabbitmq://localhost:5672"
    };
  });
});

但我不断得到:

  

连接失败:admin @ localhost:6002 /

     

BrokerUnreachableException:指定的端点都不可达

尝试各种可能性,没有任何效果。如果我删除UseCluster,那么同一个host:port会起作用,这很奇怪。

使用群集名称作为主机名(例如:busFactoryConfig.Host("rabbit2@{hostname-fqdn}", "/", ...)会出现以下错误:

  

EndpointNotFoundException:找不到指定地址的主机:rabbitmq:// rabbit2 @ {hostname-fqdn} / bus- {hostname} -RabbitMQConsumer-rnuoyyrtr44jcxutbdmpkkikdu?durable = false&autodelete = true

RabbitMQ v3.7.14,Erlang v21.3

3 个答案:

答案 0 :(得分:0)

我认为经过1000次尝试后我解决了问题,如下所示:

var rabbitMqHost = busFactoryConfig.Host(new Uri("rabbitmq://rabbit2/"), hostConfig =>
{
    hostConfig.Username("username");
    hostConfig.Password("password");
    hostConfig.UseCluster(c =>
    {
        c.Node("rabbit");
        c.Node("rabbit1");
        c.Node("rabbit2");
    });
});

然后我在主机文件中添加了以下内容:

127.0.0.1 rabbit2

但是它仍然弄乱了端口(因为所有节点都是本地主机)...所以仍然没有成功。

答案 1 :(得分:0)

尝试在CreateConnection上指定AddressFamily属性。

 var factory = new RabbitMQ.Client.ConnectionFactory
{
    UserName = "username",
    Password = "password"
};
var conn  = factory.CreateConnection(new [] { new AmqpTcpEndpoint("hostname") {AddressFamily = AddressFamily.InterNetwork}});

答案 2 :(得分:0)

好吧,我明白了。

在创建RabbitMqHost之后尝试一下

var rabbitMqHost = busFactoryConfig.Host(new Uri("rabbitmq://rabbit2/"), hostConfig =>
{
    hostConfig.Username("username");
    hostConfig.Password("password");
    hostConfig.UseCluster(c =>
    {
        c.Node("rabbit");
        c.Node("rabbit1");
        c.Node("rabbit2");
    });
});

rabbitMqHost.Settings.GetConnectionFactory().Endpoint.AddressFamily = AddressFamily.InterNetwork;