【问题标题】:Slow startup on spring integration test. Cause? Can't disable RabbitMQspring 集成测试启动缓慢。原因?无法禁用 RabbitMQ
【发布时间】:2018-02-09 16:44:21
【问题描述】:

我的集成测试在启动时遇到性能问题。

我正在尝试模拟系统的消息传递。为此,我基本上在网关上使用@MockBean 并使用@EnableAutoConfiguration(exclude = {RabbitAutoConfiguration.class})。示例:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
@WebAppConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@ActiveProfiles("test")
@EnableAutoConfiguration(exclude = {RabbitAutoConfiguration.class})
public class MyTestIT {

这两个配置做得很好,我的测试运行没有问题,并且与一些外部 RabbitMQ 没有任何依赖关系。

但是spring boot的启动时间非常非常慢。配置SimpleMessageListenerContainerAmqpInboundChannelAdapterEventDrivenConsumerRabbitExchangeQueueProvisioner等这部分只需要两分钟左右。

日志中有一些关于问题所在的提示(我剪掉了很多消息,这是一个示例):

2018-02-09 14:26:37.784  INFO [ms-project-name-service,,,] 13804 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'ms-project-name-service:test:-1.channel2-output' has 1 subscriber(s).

2018-02-09 14:26:54.110  INFO [ms-project-name-service,,,] 13804 --- [           main] c.s.b.r.p.RabbitExchangeQueueProvisioner : declaring queue for inbound: channel1-input.anonymous.417FtxKTTce7-_IR0tGuNA, bound to: channel1-input

2018-02-09 14:27:00.147  INFO [ms-project-name-service,,,] 13804 --- [           main] o.s.c.stream.binder.BinderErrorChannel   : Channel 'ms-project-name-service:test:-1.channel1-input.anonymous.417FtxKTTce7-_IR0tGuNA.errors' has 2 subscriber(s).

2018-02-09 14:27:09.186  INFO [ms-project-name-service,,,] 13804 --- [ce7-_IR0tGuNA-1] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@4f0ea6f8: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0

最奇怪的是这个:

2018-02-09 14:58:42.783  WARN [ms-project-name-service,,,] 208 --- [geGeQP_9Li3Jg-1] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect

最后一个似乎还在尝试连接本地的 RabbitMQ。

日志中有很多这样的消息。我不明白为什么即使禁用了 RabbitMQAutoConfiguration 仍然会发生这种情况。如果没有 RabbitMQ 连接,spring 是如何订阅频道的也是一个谜。

【问题讨论】:

  • 添加了几个标签,希望有使用spring boot经验的人可以提供帮助。编程方面不会有太多帮助,因为问题实际上可能出现在代码中的任何地方。你做了什么调试(我假设可以调试测试的启动)?

标签: spring spring-boot rabbitmq integration-testing spring-boot-test


【解决方案1】:

我们这里有类似的问题,通过更换跑步者解决了:

@RunWith(SpringRunner.class)

@RunWith(SpringJUnit4ClassRunner.class)

它们在文档中看起来是一样的,但确实启动了我们的测试性能。 让我知道它是否有效,我仍在查看文档以获取更多详细信息。

【讨论】:

  • 这对我来说没有意义,但是...有效!在 Spring 文档中,他们说 SpringRunner 和 SpringJUnit4ClassRunner 是一回事,但事实并非如此。谢谢!
  • 不敢相信是这样,+1。
  • @MinjunYu 看到我的补充答案。问题不仅如此!
【解决方案2】:

@Marco 响应后,我删除 pom.xml 的 follow 依赖后问题又回来了(例如,这是我对<dependencies/> 的第一个依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-test-support</artifactId>
    <scope>test</scope>
</dependency>

返回依赖后,问题解决。我改为SpringRunner.class,测试继续很快。我删除了依赖,测试又变慢了……

我认为这是一个与 Spring 寻找类路径来进行自动配置相关的错误。

顺便说一句,日志中的一些消息会继续发生,但不会花费这么长时间。

其他有帮助的事情是使用此配置进行测试

@Configuration
public class RabbitMqConfiguration {

    @Bean
    ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory();
    }

}

【讨论】:

    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    相关资源
    最近更新 更多