Вот моя текущая настройка: < /p>
Application.propertiesобразноspring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.listener.simple.auto-startup=true
< /code>
RabbitConfig
@Configuration
public class RabbitMqConfig {
// Email OTP queue
public static final String OTP_QUEUE = "otp_email_queue";
public static final String OTP_EXCHANGE = "otp_email_exchange";
public static final String OTP_ROUTING_KEY = "otp_email_routing";
// Registration notification queue
public static final String REGISTRATION_QUEUE = "registration_email_queue";
public static final String REGISTRATION_EXCHANGE = "registration_email_exchange";
public static final String REGISTRATION_ROUTING_KEY = "registration_email_routing";
@Bean
public Queue otpQueue() {
return new Queue(OTP_QUEUE, true);
}
@Bean
public DirectExchange otpExchange() {
return new DirectExchange(OTP_EXCHANGE);
}
@Bean
public Binding otpBinding() {
return BindingBuilder.bind(otpQueue()).to(otpExchange()).with(OTP_ROUTING_KEY);
}
@Bean
public Queue registrationQueue() {
return new Queue(REGISTRATION_QUEUE, true);
}
@Bean
public DirectExchange registrationExchange() {
return new DirectExchange(REGISTRATION_EXCHANGE);
}
@Bean
public Binding registrationBinding() {
return BindingBuilder.bind(registrationQueue()).to(registrationExchange()).with(REGISTRATION_ROUTING_KEY);
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
}
< /code>
Error logs
When I run the application, I keep getting connection errors like this:
20-08-2025 11:22:11.873 [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] INFO org.springframework.amqp.rabbit.connection.CachingConnectionFactory.connectAddresses - Attempting to connect to: [localhost:5672]
20-08-2025 11:22:11.900 [main] INFO org.springframework.amqp.rabbit.connection.CachingConnectionFactory.connectAddresses - Attempting to connect to: [localhost:5672]
20-08-2025 11:22:11.931 [main] INFO org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.checkMismatchedQueues - Broker not available; cannot force queue declarations during start: java.net.ConnectException: Connection refused: getsockopt
20-08-2025 11:22:11.932 [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#1-1] INFO org.springframework.amqp.rabbit.connection.CachingConnectionFactory.connectAddresses - Attempting to connect to: [localhost:5672]
20-08-2025 11:22:11.964 [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#1-1] ERROR org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.redeclareElementsIfNecessary - Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: getsockopt
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61)
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:632)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:726)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:257)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2262)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2235)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2215)
at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:467)
WARN org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.logConsumerException - Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: getsockopt
20-08-2025 11:22:16.970 [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] INFO org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.killOrRestart - Restarting Consumer@7c654cfc: tags=[[]], channel=null, acknowledgeMode=AUTO local queue size=0
20-08-2025 11:22:16.971 [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] INFO org.springframework.amqp.rabbit.connection.CachingConnectionFactory.connectAddresses - Attempting to connect to: [localhost:5672]
20-08-2025 11:22:17.008 [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] ERROR org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.redeclareElementsIfNecessary - Failed to check/redeclare auto-delete queue(s).
< /code>
What I tried / What I’m confused about
I’m running my Spring Boot application with Docker, and I already exposed ports 5672 and 15672.
However, I can’t access the RabbitMQ management UI (http://localhost:15672), it doesn’t show up.
The Spring Boot app keeps retrying to connect but always fails with Connection refused.
Question
Can someone explain clearly why Spring can’t connect to RabbitMQ in this setup and what I need to fix (either in Docker, properties, or config) so that the application can connect successfully?
Thanks in advance!
Подробнее здесь: https://stackoverflow.com/questions/797 ... getsockotp