gpt4 book ai didi

java - 如何在 Spring 测试中创建嵌入式 Kafka 时修复 'Invalid URL'

转载 作者:行者123 更新时间:2023-11-30 01:48:08 26 4
gpt4 key购买 nike

我正在尝试为一段将数据发送到 Kafka 主题的代码构建一个测试,我一直在阅读一个看起来非常简单的示例,但是当我尝试运行测试时,它无法抛出此错误: 'bootstrap.servers 中的 URL 无效:spring.embedded.kafka.brokers'

URL“spring.embedded.kafka.brokers”我从文档中获得了它,但我现在找不到链接源。这是我到目前为止所尝试过的:

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@DirtiesContext
@TestPropertySource({"classpath:application-test.properties"})
public class PublishCustomerServiceImplTest {

private String bootstrapServers = "spring.embedded.kafka.brokers";

private static final String TOPIC = "TopicName";

public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.RETRIES_CONFIG, 3);
props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, "org.apache.kafka.clients.producer.internals.DefaultPartitioner");
return props;
}

public ProducerFactory<String, CustomerPublishRequest> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}

public KafkaTemplate<String, CustomerPublishRequest> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}

@ClassRule
public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, TOPIC);

@Test
public void publishCustomerTest() throws Exception {

KafkaTemplate<String, CustomerPublishRequest> kafkaTemplate = kafkaTemplate();

ListenableFuture<SendResult<String, CustomerPublishRequest>> future = kafkaTemplate.send(TOPIC, CustomerRequestDummy.getCustomer());
SendResult<String, CustomerPublishRequest> sendResult = future.get();
long offset = sendResult.getRecordMetadata().offset();

}
}

任何帮助将不胜感激。

最佳答案

错误消息很清楚,这是 spring.embedded.kafka.brokers 无效的 bootstrap-server URL,从 EmbeddedKafkaRule 获取它

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@DirtiesContext
@TestPropertySource({"classpath:application-test.properties"})
public class PublishCustomerServiceImplTest {

private static final String TOPIC = "TopicName";

@ClassRule
public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, TOPIC);

public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, embeddedKafka.getEmbeddedKafka().getBrokersAsString());
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.RETRIES_CONFIG, 3);
props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, "org.apache.kafka.clients.producer.internals.DefaultPartitioner");
return props;
}

public ProducerFactory<String, CustomerPublishRequest> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}

public KafkaTemplate<String, CustomerPublishRequest> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}

@Test
public void publishCustomerTest() throws Exception {

KafkaTemplate<String, CustomerPublishRequest> kafkaTemplate = kafkaTemplate();

ListenableFuture<SendResult<String, CustomerPublishRequest>> future = kafkaTemplate.send(TOPIC, CustomerRequestDummy.getCustomer());
SendResult<String, CustomerPublishRequest> sendResult = future.get();
long offset = sendResult.getRecordMetadata().offset();

}
}

关于java - 如何在 Spring 测试中创建嵌入式 Kafka 时修复 'Invalid URL',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57173115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com