3

I am trying to connect my Spring-Boot application to RabbitMQ which is hosted as a AWS service.I am getting the following exception when starting the application.

I am including the java exception, configurations of my spring boot and AWS as well

2021-05-15 09:31:50.651[0;39m [32m INFO[0;39m [35m12988[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter       [0;39m [2m:[0;39m Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure
[2m2021-05-15 09:31:50.655[0;39m [32m INFO[0;39m [35m12988[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter       [0;39m [2m:[0;39m Located managed bean 'rabbitConnectionFactory': registering with JMX server as MBean [org.springframework.amqp.rabbit.connection:name=rabbitConnectionFactory,type=CachingConnectionFactory]
[2m2021-05-15 09:31:50.678[0;39m [32m INFO[0;39m [35m12988[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.c.support.DefaultLifecycleProcessor [0;39m [2m:[0;39m Starting beans in phase 0
[2m2021-05-15 09:31:50.678[0;39m [32m INFO[0;39m [35m12988[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.c.support.DefaultLifecycleProcessor [0;39m [2m:[0;39m Starting beans in phase 2147483647
[2m2021-05-15 09:31:50.680[0;39m [32m INFO[0;39m [35m12988[0;39m [2m---[0;39m [2m[           main][0;39m [36md.s.w.p.DocumentationPluginsBootstrapper[0;39m [2m:[0;39m Context refreshed
[2m2021-05-15 09:31:50.715[0;39m [32m INFO[0;39m [35m12988[0;39m [2m---[0;39m [2m[           main][0;39m [36md.s.w.p.DocumentationPluginsBootstrapper[0;39m [2m:[0;39m Found 1 custom documentation plugin(s)
[2m2021-05-15 09:31:50.794[0;39m [32m INFO[0;39m [35m12988[0;39m [2m---[0;39m [2m[           main][0;39m [36ms.d.s.w.s.ApiListingReferenceScanner    [0;39m [2m:[0;39m Scanning for api listing references
[2m2021-05-15 09:31:51.241[0;39m [32m INFO[0;39m [35m12988[0;39m [2m---[0;39m [2m[cTaskExecutor-1][0;39m [36mo.s.a.r.c.CachingConnectionFactory      [0;39m [2m:[0;39m Attempting to connect to: [b-********-9c**-4b44-97c4-a3526fa7d4ee.mq.**-****east-1.amazonaws.com:5672]
[2m2021-05-15 09:32:12.283[0;39m [31mERROR[0;39m [35m12988[0;39m [2m---[0;39m [2m[cTaskExecutor-1][0;39m [36mo.s.a.r.l.SimpleMessageListenerContainer[0;39m [2m:[0;39m Failed to check/redeclare auto-delete queue(s).

org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection timed out: connect
    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:476) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:614) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:240) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1797) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1771) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1752) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:338) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1604) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:963) [spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_261]
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_261]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:81) ~[na:1.8.0_261]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476) ~[na:1.8.0_261]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218) ~[na:1.8.0_261]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200) ~[na:1.8.0_261]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162) ~[na:1.8.0_261]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394) ~[na:1.8.0_261]
    at java.net.Socket.connect(Socket.java:606) ~[na:1.8.0_261]
    at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.1.2.jar:5.1.2]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:955) ~[amqp-client-5.1.2.jar:5.1.2]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:907) ~[amqp-client-5.1.2.jar:5.1.2]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:847) ~[amqp-client-5.1.2.jar:5.1.2]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:449) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    ... 9 common frames omitted

below mentioned is configuration of the application.yml and pom.xml file

# RabbitMQ configuration 
spring.rabbitmq.host=https://b-********-9c**-4b44-97c4-a3526fa7d4ee.mq.**-****east-1.amazonaws.com
#spring.rabbitmq.host=amqps://b-********-9c**-4b44-97c4-a3526fa7d4ee.mq.**-****east-1.amazonaws.com
#spring.rabbitmq.host=b-********-9c**-4b44-97c4-a3526fa7d4ee.mq.**-****east-1.amazonaws.com
spring.rabbitmq.port=5672
spring.rabbitmq.username=<username>
spring.rabbitmq.password=<password>

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>2.4.2</version>
</dependency>

aws inbound VPC configuration

enter image description here

Any Help is Appreciated, Thanks

k.explorer
  • 291
  • 6
  • 19

4 Answers4

6

#spring.rabbitmq.host=amqps://b--9c-4b44-97c4-a3526fa7d4ee.mq.-east-1.amazonaws.com

Taking hint from the above commented host property, it seems you have SSL enabled.

You can explicitly tell Spring to use SSL with spring.rabbitmq.ssl.enabled property. This is how the entire set of properties would look like

spring.rabbitmq.ssl.enabled=true
spring.rabbitmq.host=b-XXXX-27f68f8379d3.mq.aws-region.amazonaws.com
spring.rabbitmq.port=5671
spring.rabbitmq.username=<YourUserName>
spring.rabbitmq.password=<YourPassword>

Or you can provide the addresses along with the username and password and Spring Boot would figure out that it needs to use amqps and port 5671

spring.rabbitmq.addresses=amqps://b-XXXX-27f68f8379d3.mq.aws-region.amazonaws.com:5671
spring.rabbitmq.username=<YourUserName>
spring.rabbitmq.password=<YourPassword>
GSSwain
  • 5,787
  • 2
  • 19
  • 24
  • tried enabled "spring.rabbitmq.ssl.enabled=true", but getting following exception " org.springframework.amqp.AmqpIOException: javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:71) ~[spring-rabbit-2.0.3.RELEASE.jar:2.0.3.RELEASE]" – k.explorer May 15 '21 at 18:08
  • New errors are better. Can you provide more details about your runtime like Java version and distribution, running from within AWS or outside. How are you running it, Docker or Java process. Any Command line arguments for the Java process. RabbitMQ single instance deployment or cluster mode. – GSSwain May 16 '21 at 00:38
  • 1
    Just checked the managed RabbitMq from AWS supports `Protocol : TLSv1.2`. Make sure your Java client also supports TLSv1.2. – GSSwain May 16 '21 at 01:35
  • java version : 8, Rabbit MQ is a aws service,and my java application is running in local currently as a standalone spring application, RabbitMQ is a single instance deployment, Rabbit MQ is not exposed publicly and has a VPC connection enabled, i guess it could be an issue with port access from my local m/c(not sure though), also i have enabled the 5672 & 5671 inbound ports in security group, not sure what i am missing still – k.explorer May 16 '21 at 06:04
  • If you need to connect from Local, either the service should be public(not limited to VPC based access) or you need to use Client VPN. https://docs.aws.amazon.com/vpn/latest/clientvpn-admin/what-is.html If you are trying it for a POC or learning, you can consider using a public/internet based one or deploy your code on the AWS in the correct VPC and check the connectivity. – GSSwain May 16 '21 at 12:38
  • thanks, i was able to connect to RabbitMQ based on your answer by exclusively configuring ssl as true, but strangely passing the property through application.yml didn't work, i had to use java configuration to set that property – k.explorer May 18 '21 at 16:17
1

I was able to solve the same issue by adding following config

    spring:
      rabbitmq:
        host: YOUR_HOST_NAME
        port: 5671
        username: YOUR_USER_NAME
        password: YOUR_PASSWORD
          ssl:
            enabled: true
            algorithm: TLSv1.2

Last part algorithm:TSLv1.2 is important. Without it I was getting

org.springframework.amqp.AmqpIOException: javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version at 
Nullish Byte
  • 354
  • 2
  • 10
0

i have to set ssl property to true to get connected to AWS Rabbitmq, but setting the property "spring.rabbitmq.ssl.enabled=true" through application.yml didnt work in my case, i had to create a bean in a configuration class to set it, the below is the code which worked for me

@Bean 
public CachingConnectionFactory rabbitConnectionFactory() throws Exception 
{
  ConnectionFactory factory = new ConnectionFactory(); 
  factory.setHost(host);
  factory.setUsername(username); 
  factory.setPassword(password);
  factory.setPort(5671);
  **factory.useSslProtocol();**  
  CachingConnectionFactory connectionFactory = new CachingConnectionFactory(factory); 
  return connectionFactory; 
}
k.explorer
  • 291
  • 6
  • 19
  • are you sure this is a working solution. I getting: 'ConnectionFactory' is abstract; cannot be instantiate – Dmitry Oct 24 '22 at 21:42
0

Bellow, mine configuration, that works well with SpringBoot 2.4.5

  @Value("${spring.rabbitmq.addresses}")
    private String addresses;
    @Value("${spring.rabbitmq.username}")
    private String rabbitmqUsername;
    @Value("${spring.rabbitmq.password}")
    private String rabbitmqPassword;
    @Value("${spring.rabbitmq.virtual-host}")
    private String rabbitmqVirtualHost;
    @Bean
    public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory =new CachingConnectionFactory();
    connectionFactory.setUri(addresses);
    connectionFactory.setUsername(rabbitmqUsername);
    connectionFactory.setPassword(rabbitmqPassword);
    connectionFactory.setVirtualHost(rabbitmqVirtualHost);
    return connectionFactory;
    }
Dmitry
  • 186
  • 3
  • 4