[英]How to fix 'Invalid URL' while creating embedded Kafka in Spring test
I am trying to build a test for a piece of code that send data to a Kafka topic, I've been reading an example which looks pretty straightforward, however when I try to run the test, it fails throwing this error: 'Invalid url in bootstrap.servers: spring.embedded.kafka.brokers'我正在尝试为将数据发送到 Kafka 主题的一段代码构建一个测试,我一直在阅读一个看起来非常简单的示例,但是当我尝试运行测试时,它无法抛出此错误:'无效的 url在 bootstrap.servers 中:spring.embedded.kafka.brokers'
The URL 'spring.embedded.kafka.brokers' I've got it from the docs, I can't find right now the link source though. URL 'spring.embedded.kafka.brokers' 我从文档中得到它,但我现在找不到链接源。 This is what I have tried so far:这是我到目前为止所尝试的:
@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();
}
}
Any help will be appreciated.任何帮助将不胜感激。
The error message is clear this is spring.embedded.kafka.brokers
invalid bootstrap-server URL, get it from EmbeddedKafkaRule
错误消息很清楚,这是spring.embedded.kafka.brokers
无效的引导服务器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();
}
}
Faced the same problem and then fixed it by overriding two following properties,面临同样的问题,然后通过覆盖以下两个属性来修复它,
@TestPropertySource(properties = {"spring.kafka.producer.bootstrap-servers=${spring.embedded.kafka.brokers}",
"spring.kafka.admin.properties.bootstrap.servers=${spring.embedded.kafka.brokers}"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.