如何用java连接到rabbitmq集群?

时间:2018-01-05 23:24:59

标签: java rabbitmq

我有两个由IBM提供的RabbitMQ实例:

a[n]

连接RabbitMQ有两个选择:

选项1:

String uri1 = "amqps://admin:password@host1:31281/virtualhost";
String uri2 = "amqps://admin:password@host2:31281/virtualhost";

选项2:

ConnectionFactory factory = new ConnectionFactory();
factory.setUri(uri1);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

出于任何原因,我无法使用此选项连接到rabbitmq。此选项会出现此错误:

ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("admin");
factory.setPassword("password");
factory.setHost("host1");
factory.setPort(31281);
factory.setVirtualHost("virtualhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

连接群集rabbitmq的一种方法是:

Exception in thread "main" java.util.concurrent.TimeoutException
    at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77)
    at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:120)
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:372)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:297)
    at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
    at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:99)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:900)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:859)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:817)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:954)

但我不知道如何连接到具有许多URI的集群rabbitmq,因为选项2不起作用。请问,有什么建议吗?

2 个答案:

答案 0 :(得分:0)

尝试使用此未经测试的代码。我使用RabbitMQ Java Client javadocs作为参考:

ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("admin");
factory.setPassword("password");
factory.setVirtualHost("virtualhost");
Address address1 = new Address("host1", 31281);
Address a2 = new Address("host2", 31281);
List<Address> = new List<Address> { a1, a2 }; // I'm sure this is not the right syntax
AddressResolver addressResolver = factory.createAddressResolver(addresses);
Connection connection = factory.newConnection(addressResolver);
Channel channel = connection.createChannel();

答案 1 :(得分:0)

对不起,将rabbitmq与URI连接无关紧要,问题通过&#34; List addressesList = ......;&#34;是因为这个组合的兔子实例使用SSL,没有考虑SSL客户端java得到这个错误:

Exception in thread "main" java.util.concurrent.TimeoutException
    at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77)
    at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:120)
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:372)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:297)
    at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
    at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:99)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:900)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:859)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:817)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:954)

然后我们可以使用信任库或简单地添加:

factory.useSslProtocol("TLSv1.2");