Spring Boot RabbitMQ测试容器在测试开始时断开连接

omhiaaxx  于 2022-11-23  发布在  Spring
关注(0)|答案(1)|浏览(166)

我遇到了与this question相同的问题,rabbitmq在开始连接的同一秒就断开了连接。我在github上有一个最小的项目。
这是一个Spring Boot项目,其中运行了两个Docker容器。一个使用RMQ,一个使用psql。在运行项目时使用容器没有问题,只是rabbitmq测试容器似乎不稳定。我确实尝试将容器 Package 在一个通用容器中,结果相同。
第一个测试,看看容器是否启动和运行通过,所以它似乎是一些与amqp连接只。
设定类别:

@SpringBootTest(classes = TestContainersDemoApplication.class)
@Testcontainers
@AutoConfigureMockMvc
@ExtendWith(SpringExtension.class)
public abstract class TestContainersConfig  {

    @Autowired
    public MockMvc mockMvc;

    @Container
    public static final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:3.8-management-alpine");

    @Container
    public static PostgreSQLContainer sqlContainer = new PostgreSQLContainer("postgres:latest")
                .withDatabaseName("demo")
                .withUsername("postgres")
                .withPassword("postgres");

    @DynamicPropertySource
    static void registerProperties(DynamicPropertyRegistry dynamicPropertyRegistry) {
        dynamicPropertyRegistry.add("spring.datasource.url",  () -> sqlContainer.getJdbcUrl());
        dynamicPropertyRegistry.add("spring.datasource.username", () -> sqlContainer.getUsername());
        dynamicPropertyRegistry.add("spring.datasource.password", () -> sqlContainer.getPassword());
        dynamicPropertyRegistry.add("spring.rabbitmq.host", rabbitMQContainer::getHost);
        dynamicPropertyRegistry.add("spring.rabbitmq.port", rabbitMQContainer::getAmqpPort);
    }

    static  {
        Startables.deepStart(Stream.of(rabbitMQContainer, sqlContainer)).join();
    }
}

堆栈跟踪似乎与链接的问题相同:

2022-11-18 21:00:23.555  INFO 441007 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.io.IOException
2022-11-18 21:00:23.559  INFO 441007 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:49596]
2022-11-18 21:00:23.770  WARN 441007 --- [127.0.0.1:49596] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occurred (Exception message: Socket closed)
2022-11-18 21:00:23.773 ERROR 441007 --- [ntContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.AmqpIOException: java.io.IOException
    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:70) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:602) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:725) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:252) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2180) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2153) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2133) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:463) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:447) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1930) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1911) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1377) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1223) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.io.IOException: null
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:129) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:125) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:396) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1225) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1173) ~[amqp-client-5.14.2.jar:5.14.2]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connectAddresses(AbstractConnectionFactory.java:640) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:615) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:565) ~[spring-rabbit-2.4.7.jar:2.4.7]
    ... 12 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:502) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:326) ~[amqp-client-5.14.2.jar:5.14.2]
    ... 17 common frames omitted
Caused by: java.io.EOFException: null
    at java.base/java.io.DataInputStream.readUnsignedByte(DataInputStream.java:296) ~[na:na]
    at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:184) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:665) ~[amqp-client-5.14.2.jar:5.14.2]
    ... 1 common frames omitted
2022-11-18 21:00:23.774  INFO 441007 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:49596]
2022-11-18 21:00:23.978  WARN 441007 --- [127.0.0.1:49596] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occurred (Exception message: Socket closed)
2022-11-18 21:00:24.022  INFO 441007 --- [           main] message.MessageControllerTest            : Started MessageControllerTest in 4.155 seconds (JVM running for 14.327)
2022-11-18 21:00:24.378  INFO 441007 --- [           main] message.MessageControllerTest            : sqlcontianers are working
2022-11-18 21:00:24.390  INFO 441007 --- [           main] message.MessageControllerTest            : InsertNewMessage
2022-11-18 21:00:24.447  INFO 441007 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:49596]```
r7knjye2

r7knjye21#

您手动管理容器生命周期,这是一种很好的方法:

Startables.deepStart(Stream.of(rabbitMQContainer, sqlContainer)).join();

在这种情况下,删除干扰测试生命周期的@Container@Testcontainers注解。
您的测试失败,原因是:

Caused by: com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.

RabbitMQContainer中的默认管理员用户名和密码应该是guest。在application.yml中相应地更改它可以使测试通过。一旦更改,它也可以在仍然使用@Testcontainers扩展时工作(尽管在这种情况下不推荐)。
顺便说一句,谢谢你分享复制器,它使调试变得非常容易。

相关问题