When using a Producer and Consumer in an application, attempting to send a message to RabbitMQ via the Spring-Cloud-Stream-Binder-Rabbit library results in a 'ConnectionRefused' error when connecting to the RabbitMQ server.
Error Message:
2017-12-19T17:59:54.045+03:00 [APP/PROC/WEB/0] [OUT] 2017-12-19 14:59:54.044 ERROR 14 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/postgresql-test-2] threw exception [Request processing failed; nested exception is org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint@23f6f56f]; nested exception is org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused), failedMessage=GenericMessage [payload=byte[37], headers={id=123456789-998e-1ccc-40b5-a34c73fe823, contentType=text/plain, originalContentType=application/json;charset=UTF-8, timestamp=1513695594020}]] with root cause 2017-12-19T17:59:54.045+03:00 [APP/PROC/WEB/0] [OUT] java.net.ConnectException: Connection refused (Connection refused) 2017-12-19T17:59:54.045+03:00 [APP/PROC/WEB/0] [OUT] at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_152] 2017-12-19T17:59:54.045+03:00 [APP/PROC/WEB/0] [OUT] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_152] 2017-12-19T17:59:54.045+03:00 [APP/PROC/WEB/0] [OUT] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_152]
The error is caused by the addition of a separate ConnectionFactory for Producers and Consumers (added in spring-cloud-stream-binder-rabbit v1.3.1).
Although its necessary to prevent blocking of the single connection by producers, the bug indicates that the connector's properties were not properly copied to the second connection factory, and thus the connection is not initialized properly.
1. One of the workaround is to set the Spring Boot property: 'spring.rabbitmq.addresses'. The purpose of this property is to provide a list of addresses to which the client should connect. The value of this should be set to '${vcap.services.<rabbit-service-name>.credentials.protocols.amqp.uri}', where <rabbit-service-name> is the name of the RabbitMQ service instance. This effectively provides the application's ConnectionFactories with the explicit URL of the RabbitMQ server.
2. Alternatively, the Spring Cloud version can be set to the previous release, Dalston.RELEASE, in which the Spring-Cloud-Stream-Binder-Rabbit library does not have this issue.