首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何用java连接到rabbitmq集群?

如何用java连接到rabbitmq集群?
EN

Stack Overflow用户
提问于 2018-01-05 23:24:59
回答 2查看 3.6K关注 0票数 2

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

代码语言:javascript
复制
String uri1 = "amqps://admin:password@host1:31281/virtualhost";
String uri2 = "amqps://admin:password@host2:31281/virtualhost";

要连接RabbitMQ,有两个选项:

备选案文1:

代码语言:javascript
复制
ConnectionFactory factory = new ConnectionFactory();
factory.setUri(uri1);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

备选案文2:

代码语言:javascript
复制
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。此选项将获得以下错误:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
List<AddressType> addressesList = ......;//Read from xml and load
Connection connection = factory.newConnection(addressesList);

但是我不知道如何用许多URI连接到一个集群rabbitmq,因为选项2不起作用。拜托,有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-09 15:28:09

对不起,将rabbitmq与URI连接起来并不重要,问题是通过"List addressesList =.“连接;是因为兔子的这个组合实例使用SSL,而不考虑SSL,客户端java得到了以下错误:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
factory.useSslProtocol("TLSv1.2");
票数 1
EN

Stack Overflow用户

发布于 2018-01-06 15:40:54

让这个未经测试的代码试一试。我使用RabbitMQ Java客户端javadocs作为我的参考:

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48122685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档