我有一个spring-boot应用程序运行在带有rabbit监听器的嵌入式tomcat上,我这样配置它
@Configuration
public class RabbitConfiguration {
public static final String REQUEST_QUEUE = "from-beeline-req";
public static final String REPLY_QUEUE = "from-beeline-reply";
@Bean
public Queue beelineRpcReqQueue() {
return new Queue(REQUEST_QUEUE);
}
@Bean
public Queue beelineRpcReplyQueue() {
return new Queue(REPLY_QUEUE);
}
@Bean
public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer, ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate();
configurer.configure(template, connectionFactory);
template.setDefaultReceiveQueue(REQUEST_QUEUE);
template.setReplyAddress(REPLY_QUEUE);
template.setUseDirectReplyToContainer(false);
return template;
}
@Bean
public SimpleMessageListenerContainer replyListenerContainer(ConnectionFactory connectionFactory, RabbitTemplate rabbitTemplate) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueues(beelineRpcReplyQueue());
container.setMessageListener(rabbitTemplate);
return container;
}
}
还有我的 application.yml
文件看起来像这样
spring:
main:
banner-mode: LOG
rabbitmq:
host: 172.29.14.45
port: 5672
username: guest
password: guest
template:
reply-timeout: 15000
server:
port: 8888
所以主要的一点是我想连接到兔子服务器位于确切的地址(172.29.14.45)。创建的侦听器容器正在尝试连接到localhost。它也忽略了兔子港的特性。
2021-02-23 23:04:59.715 [replyListenerContainer-1] INFO (AbstractConnectionFactory.java:636) - Attempting to connect to: [localhost:5672]
2021-02-23 23:05:01.721 [replyListenerContainer-1] ERROR (AbstractMessageListenerContainer.java:1877) - Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
然后继续重启消费
2021-02-23 23:17:49.069 [replyListenerContainer-1] INFO (SimpleMessageListenerContainer.java:1428) - Restarting Consumer@2a140ce5: tags=[[]], channel=null, acknowledgeMode=AUTO local queue size=0
2021-02-23 23:17:49.069 [replyListenerContainer-1] DEBUG (BlockingQueueConsumer.java:758) - Closing Rabbit Channel: null
2021-02-23 23:17:49.071 [replyListenerContainer-2] INFO (AbstractConnectionFactory.java:636) - Attempting to connect to: [localhost:5672]
我应该怎么做才能让spring使用我的主机属性而不是 localhost
?
2条答案
按热度按时间rsl1atfo1#
我总是使用application.properties文件中的addresses属性
spring.rabbitmq.addresses地址=amqp://username:password@host:端口/vhost
“虚拟主机”(或vhost)的名称指定协议引用的实体(如交换和队列)的命名空间。注意,这不是http意义上的虚拟主机。
https://www.rabbitmq.com/uri-spec.html
例子:
spring.rabbitmq.addresses地址=amqp://ihrpsvpp:in4etuiikgu7fvbr0tr6wygvgcgyj9ja@lion.rmq.cloudamqp.com/ihrpsvpp
mftmpeh82#
好吧,原来是bean刷新了应用程序的上下文,是什么导致了自动配置失败