如何配置 RabbitMQ 与 spring-rabbit 的连接?

How to configure RabbitMQ connection with spring-rabbit?

我正在关注 this guide 以了解如何将 spring-rabbit 与 RabbitMQ 一起使用。但是在本指南中,RabbitMQ 配置是默认配置(本地主机服务器和凭据为 guest/guest)。想用ip地址和凭据连接远程RabbitMQ怎么办?我不知道在我的申请中在哪里设置这些信息。

该指南的应用程序是 Spring 启动应用程序。

将文件 application.properties 添加到 src/main/resources

然后您可以根据 Spring Boot Documentation 配置 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.
...

要连接到集群,请使用

spring.rabbitmq.addresses= # Comma-separated list of addresses to which the client should connect.

例如server1:5672,server2:5672.

如果您不想使用开机自动配置,请自行声明一个 CachingConnectionFactory @Bean 并根据需要进行配置。