Kafka TestContainer 尝试连接到错误的地址
Kafka TestContainer tries to connect to wrong address
我正在学习如何测试 Spring 使用 TestContainers 启动 Kafka 应用程序。测试通过。但是,一开始有很多这样的信息:
2021-04-05 09:00:13.927 WARN 1864 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
之后,生产者连接到有效的 bootstrap 服务器:
bootstrap.servers = [PLAINTEXT://localhost:55015]
如何避免此类错误?他们增加了测试时间。
我遇到了同样的问题,解决了创建和应用程序-test.yml的问题:
spring:
kafka:
bootstrap-servers: fake:1234
KafkaServerTestProvider.java:
@ActiveProfiles("test")
@Testcontainers
@Slf4j
public class KafkaServerTestProvider {
public static final KafkaContainer KAFKA_CONTAINER =
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));
public static class KafkaServerInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(final ConfigurableApplicationContext applicationContext) {
KAFKA_CONTAINER.start();
TestPropertyValues.of(
"spring.kafka.bootstrap-servers=" + KAFKA_CONTAINER.getBootstrapServers())
.applyTo(applicationContext.getEnvironment());
log.info("Kafka for testing: {}", KAFKA_CONTAINER.getBootstrapServers());
}
}
}
和测试:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
@ContextConfiguration(
initializers = {KafkaServerTestProvider.KafkaServerInitializer.class},
classes = Application.class)
class SampleServiceTestIT {
@Autowired SampleService sampleService;
@Test
void sendMessageTest() {
sampleService.sendMessage(
"sampletopic",
SampleRequest.builder().email("user@email.com").name("name").surname("surname").build());
}
}
我正在学习如何测试 Spring 使用 TestContainers 启动 Kafka 应用程序。测试通过。但是,一开始有很多这样的信息:
2021-04-05 09:00:13.927 WARN 1864 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
之后,生产者连接到有效的 bootstrap 服务器:
bootstrap.servers = [PLAINTEXT://localhost:55015]
如何避免此类错误?他们增加了测试时间。
我遇到了同样的问题,解决了创建和应用程序-test.yml的问题:
spring:
kafka:
bootstrap-servers: fake:1234
KafkaServerTestProvider.java:
@ActiveProfiles("test")
@Testcontainers
@Slf4j
public class KafkaServerTestProvider {
public static final KafkaContainer KAFKA_CONTAINER =
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"));
public static class KafkaServerInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(final ConfigurableApplicationContext applicationContext) {
KAFKA_CONTAINER.start();
TestPropertyValues.of(
"spring.kafka.bootstrap-servers=" + KAFKA_CONTAINER.getBootstrapServers())
.applyTo(applicationContext.getEnvironment());
log.info("Kafka for testing: {}", KAFKA_CONTAINER.getBootstrapServers());
}
}
}
和测试:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
@ContextConfiguration(
initializers = {KafkaServerTestProvider.KafkaServerInitializer.class},
classes = Application.class)
class SampleServiceTestIT {
@Autowired SampleService sampleService;
@Test
void sendMessageTest() {
sampleService.sendMessage(
"sampletopic",
SampleRequest.builder().email("user@email.com").name("name").surname("surname").build());
}
}