spring 启动未连接到 AWS 中的 RabbitMQ

spring boot not getting connected to RabbitMQ in AWS

我正在尝试将我的 Spring-Boot 应用程序连接到作为 AWS 托管的 RabbitMQ service.I 启动应用程序时出现以下异常。

我 我包括 java 异常,我的 spring 启动和 AWS 的配置

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

下面提到的是application.yml和pom.xml文件的配置

# 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 入站 VPC 配置

感谢任何帮助,谢谢

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

从上面评论的主机 属性 得到提示,您似乎启用了 SSL。

您可以明确告诉 Spring 将 SSL 与 spring.rabbitmq.ssl.enabled 属性 一起使用。这就是整个属性集的样子

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>

或者您可以提供 addresses 以及 usernamepassword 以及 Spring Boot 会发现它需要使用 amqpsport5671

spring.rabbitmq.addresses=amqps://b-XXXX-27f68f8379d3.mq.aws-region.amazonaws.com:5671
spring.rabbitmq.username=<YourUserName>
spring.rabbitmq.password=<YourPassword>

我必须将 ssl 属性 设置为 true 才能连接到 AWS Rabbitmq,但是在我的情况下设置 属性 "spring.rabbitmq.ssl.enabled=true" 到 application.yml 没有用,我必须在配置 class 中创建一个 bean 来设置它,下面是对我有用的代码

@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; 
}

我通过添加以下配置解决了同样的问题

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

最后一部分 algorithm:TSLv1.2 很重要。没有它我得到

org.springframework.amqp.AmqpIOException: javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version at