Unable to connect Spring AMQP / Rabbit MQ org.springframework.amqp.AmqpConnectException java.net.ConnectException Connection refused connect
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
org.springframework.amqp.AmqpConnectException wrapped around java.net.ConnectException: Connection refused means the application could not open a TCP connection to RabbitMQ at the configured host and port. In other words, the failure usually happens before authentication, queue declaration, or message publishing logic even begins.
That makes the debugging order straightforward: verify the broker is up, verify the address is correct, verify the port is reachable, and only then look at higher-level Spring configuration.
Start with the Broker Itself
Make sure RabbitMQ is actually running.
If RabbitMQ is running in Docker:
If the broker is not listening on the expected port, Spring cannot possibly connect.
The default AMQP port is usually 5672.
Verify the Spring Connection Settings
Typical Spring Boot configuration looks like this:
The most common configuration mistakes are:
- wrong host name
- wrong port
- assuming
localhostfrom inside a container means the host machine - using
guestremotely where the broker policy only allows it locally
That last case is especially common. RabbitMQ often restricts the guest user to loopback connections by default.
Test Raw TCP Reachability
Before digging deeper into Spring, test the port directly.
If that fails with connection refused, the issue is not Spring AMQP. It is network reachability or broker availability.
In containerized setups, also check whether the port is published correctly and whether the application should use a container service name rather than localhost.
Common Docker and Compose Mistake
If your Spring app runs in Docker Compose and RabbitMQ runs in another Compose service, localhost is wrong.
Inside the app container, localhost refers to the app container itself, not the RabbitMQ container.
In that case, use the RabbitMQ service name instead:
This single mistake causes a large share of connection-refused errors in development environments.
Distinguish Refused vs. Auth Failure
A refused connection means the TCP socket could not be opened. That is different from:
- authentication failure
- virtual host permission failure
- channel or exchange errors after connect
This distinction is useful because it narrows the search dramatically. If the error is connection refused, focus on process state, host, port, networking, and port publishing first.
A Minimal Spring Configuration Example
If this fails with connection refused, the message send code is not the interesting part yet. The infrastructure connection must work first.
Common Pitfalls
The biggest mistake is troubleshooting exchanges, queues, and listeners before confirming the broker port is reachable at all.
Another common issue is using localhost from inside a container or VM and expecting it to refer to RabbitMQ running somewhere else.
People also forget the guest remote access restriction and spend time debugging passwords when the real problem is policy.
Finally, do not rely only on application stack traces. Check RabbitMQ status, container logs, and a raw TCP connection test.
Summary
- '
Connection refusedmeans the TCP connection itself failed.' - Check that RabbitMQ is running and listening on the expected port.
- Verify
spring.rabbitmq.hostandspring.rabbitmq.portcarefully. - In container environments, avoid assuming
localhostpoints to the broker. - Use a raw network test such as
ncto separate infrastructure problems from Spring problems. - Investigate authentication only after basic connectivity is working.

