How to configure RabbitMQ connection with spring-rabbit?

kenshinji picture kenshinji · Feb 13, 2017 · Viewed 39.1k times · Source

I'm following this guide to learn how to use spring-rabbit with RabbitMQ. However in this guide, the RabbitMQ configuration is as default(localhost server and with credential as guest/guest). What should I do if I want to connect to an remote RabbitMQ with ip address and credential? I don't know where to set these information in my application.

Answer

Gary Russell picture Gary Russell · Feb 13, 2017

The application for that guide is a Spring Boot Application.

Add a file application.properties to src/main/resources.

You can then configure rabbitmq properties according to the Spring Boot Documentation - scroll down to the rabbitmq properties...

...
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.
...

To connect to a cluster, use

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

e.g. server1:5672,server2:5672.

If you don't want to use boot auto configuration, declare a CachingConnectionFactory @Bean yourself and configure it as desired.