完整异常堆栈信息:
org.springframework.kafka.KafkaException: Send failed; nested exception is org.apache.kafka.common.errors.TimeoutException: Topic test not present in metadata after 60000 ms.
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:574)
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:389)
at com.yang.custom.kafka.kafka.ProducerDemo.send(ProducerDemo.java:37)
at com.yang.custom.kafka.CustomKafkaApplicationTests.contextLoads(CustomKafkaApplicationTests.java:23)
at java.util.ArrayList.forEach(ArrayList.java:1255)
at java.util.ArrayList.forEach(ArrayList.java:1255)
Caused by: org.apache.kafka.common.errors.TimeoutException: Topic test not present in metadata after 60000 ms.
问题出现:
向kafka broker 中发送消息
@Autowired private KafkaTemplate kafkaTemplate; public void send() { // 构造方法:ProducerRecord(String topic, Integer partition, Long timestamp, K key, V value, Iterable<Header> headers) ProducerRecord<Integer, String> producerRecord = new ProducerRecord( "test", // topic null, // 不指定partition System.currentTimeMillis(), // 发送消息的事件戳 null, // key不指定 "这是一条带headers的消息", // msg new RecordHeaders().add(new RecordHeader("ttl", "20".getBytes(StandardCharsets.UTF_8))) ); kafkaTemplate.send(producerRecord); }
原因:
Jackson包版本冲突导致
项目Springboot:2.4.2
引入spring-kafka版本:2.6.5
spring-kafka依赖的Jackson版本为 2.11.3
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.3</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.3</version> <scope>compile</scope> <optional>true</optional> </dependency>
springboot自带依赖的Jackson版本:2.11.4
导致 spring-kafka 依赖的 Jackson 2.11.3版本失效,实际依赖的是2.11.4版本。
解决方案:
在项目中添加Jackson依赖,覆盖springboot和spring-kafka的Jackson依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.11.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.3</version> </dependency>
END.