测试用例记录将 spring-kafka 升级到 3.0.10 后,kafka 客户端连接问题数量增加

问题描述 投票:0回答:1

我们有一个 Spring 测试,我们创建一个 Kafka 模板的 @MockBean,以便上下文在启动过程中不会初始化失败。这只是一个基类,其中没有任何测试,其他测试类扩展了它。

@ContextConfiguration(classes = ApplicationTest.class)
@SpringBootTest(classes = Application.class,
                webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestExecutionListeners(value = { DependencyInjectionTestExecutionListener.class,
        FlywayTestExecutionListener.class },
                        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
@AutoConfigureMockMvc
@ActiveProfiles("int")
public class IntegrationTestBase {

    @Autowired
    MockMvc mockMvc;

    @Autowired
    ObjectMapper objectMapper;

    @MockBean
    User user;

    @MockBean
    private KafkaTemplate<UUID, SpecificRecord> avroTemplate;
}

因此,当我尝试启动/运行测试类时,它会初始化上下文并开始给出错误:

2023-11-27T17:50:30.154+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:30.271+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:30.272+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:30.380+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:30.380+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:30.600+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:30.601+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:31.040+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:31.041+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:31.909+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:31.909+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:33.110+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:33.110+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:34.315+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:34.315+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:35.409+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:35.410+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:36.285+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:36.286+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:37.156+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:37.157+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:38.360+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:38.360+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:39.236+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:39.237+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:40.440+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:40.440+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:41.639+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:41.639+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:42.731+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:42.733+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:43.715+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:43.715+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:44.589+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:44.589+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:45.796+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:45.796+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:46.891+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:46.891+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:48.097+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:48.098+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:49.191+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:49.192+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:50.168+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:50.169+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:51.255+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:51.256+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:52.457+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:52.458+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:53.662+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:53.662+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:54.648+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:54.649+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:55.745+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:55.745+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:56.729+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:56.729+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:57.707+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:57.708+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:58.583+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:58.584+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-11-27T17:50:59.558+05:30  INFO 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Node -1 disconnected.
2023-11-27T17:50:59.559+05:30  WARN 5064 --- [ my-service] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=my-service] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.

在以前的 spring-kafka 版本(2.7.14)中,我们没有收到这么多日志,这也增加了执行这些测试所需的时间,我们只看到该日志在日志中只打印一次。

spring apache-kafka integration-testing spring-kafka
1个回答
0
投票

之后,查看日志,我们发现问题是由于 kafka 多次重试连接到不存在的队列而引起的。 它正在寻找队列,因为我们设置了一个 kafka 模板模拟 bean,这是正确初始化上下文所必需的。

我们通过为测试环境设置此属性解决了该问题:

spring.kafka.admin.auto-create=false

© www.soinside.com 2019 - 2024. All rights reserved.