如何用spring-rabbit配置RabbitMQ连接?

ken*_*nji 19 rabbitmq spring-rabbit spring-amqp spring-boot spring-rabbitmq

我正在按照本指南学习如何使用spring-rabbitRabbitMQ.但是在本指南中,RabbitMQ配置是默认配置(localhost服务器,凭证为guest/guest).如果我想用ip地址和凭证连接到远程RabbitMQ,我该怎么办?我不知道在我的应用程序中将这些信息设置在何处.

Gar*_*ell 46

该指南的应用程序是Spring Boot应用程序.

添加文件application.propertiessrc/main/resources.

然后,您可以根据Spring Boot文档配置rabbitmq属性- 向下滚动到rabbitmq属性...

...
spring.rabbitmq.host=localhost # RabbitMQ host.
...
spring.rabbitmq.password= # Login to authenticate against the broker.
spring.rabbitmq.port=5672 # RabbitMQ port.
...
spring.rabbitmq.username= # Login user to authenticate to the broker.
...
Run Code Online (Sandbox Code Playgroud)

要连接到群集,请使用

spring.rabbitmq.addresses= # Comma-separated list of addresses to which the client should connect.
Run Code Online (Sandbox Code Playgroud)

例如server1:5672,server2:5672.

如果您不想使用引导自动配置,请声明CachingConnectionFactory @Bean自己并根据需要进行配置.