如何使用RabbitMQ主机列表连接参数 [英] How to use RabbitMQ list of hosts connection parameter

查看:326
本文介绍了如何使用RabbitMQ主机列表连接参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#rabbitMQ库中创建连接时,我试图使用IList<string>参数:

I'm trying to use the IList<string> parameter when creating a connection in the C# rabbitMQ library:

我的代码如下:

    private IConnection CreateConnection()
    {
        var connectionFactory = new ConnectionFactory
        {
            UserName = _userName,
            Password = _password,
            VirtualHost = _vhost,
            AutomaticRecoveryEnabled = DEFAULT_AUTO_RECOVER,
            RequestedHeartbeat = HEARTBEAT_TIMEOUT_SECONDS,
            Port = AmqpTcpEndpoint.UseDefaultPort,
        };
        // _hosts contains valid IPs "###.###.###.###"
        return connectionFactory.CreateConnection(_hosts);
    }

但是不管我为hosts参数设置什么,它似乎都没有连接(我得到指定的端点都不可达")

But regardless of what I suppose for the hosts parameter it doesn't seem to connect (I get "None of the specified endpoints were reachable")

即使我的列表仅包含一个元素.

Even if my list contains only one element.

现在,如果我使用这样的单个主机实现,它将可以正常运行:

Now, if I use the single host implementation like this, it works correctly:

    private IConnection CreateConnection()
    {
        var connectionFactory = new ConnectionFactory
        {
            UserName = _userName,
            Password = _password,
            VirtualHost = _vhost,
            AutomaticRecoveryEnabled = DEFAULT_AUTO_RECOVER,
            RequestedHeartbeat = HEARTBEAT_TIMEOUT_SECONDS,
            Port = AmqpTcpEndpoint.UseDefaultPort,
            HostName = _hosts.First() // or just one string
        };
        return connectionFactory.CreateConnection();
    }

我认识到RabbitMQ建议不要在客户端上存储主机集,但我只是想使它们提供的方法起作用.

I recognize that RabbitMQ suggests not storing the set of hosts on the client but I'm just trying to get their provided method to work.

推荐答案

我认为您可能需要为连接工厂的HostnameSelector属性设置一个值

I think you might need to set a value for the HostnameSelector property of the connection factory

private IConnection CreateConnection()
{
    var connectionFactory = new ConnectionFactory
    {
        UserName = _userName,
        Password = _password,
        VirtualHost = _vhost,
        AutomaticRecoveryEnabled = DEFAULT_AUTO_RECOVER,
        RequestedHeartbeat = HEARTBEAT_TIMEOUT_SECONDS,
        Port = AmqpTcpEndpoint.UseDefaultPort,
        HostnameSelector = new RandomHostnameSelector()
    };
    // _hosts contains valid IPs "###.###.###.###"
    return connectionFactory.CreateConnection(_hosts);
}

RabbitMQ提供了RandomHostnameSelector

RabbitMQ provides a RandomHostnameSelector

class RandomHostnameSelector : IHostnameSelector
{
    string IHostnameSelector.NextFrom(IList<string> options)
    {
        return options.RandomItem();
    }
}

或者您可以创建自己的IHostnameSelector实现,以拥有自己的主机选择策略.

Or you could create your own implementation of IHostnameSelector to have your own host selection strategy.

这篇关于如何使用RabbitMQ主机列表连接参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆