发呆的花生
1 年前 |
我正在开发
Spring Boot + Apache Kafka
示例。当把
TOPIC
发送给卡夫卡的时候,出错了。
错误:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.demo.kafka.SpringBootKafkaJsonApplication.main(SpringBootKafkaJsonApplication.java:17) [classes/:na]
Caused by: org.apache.kafka.common.KafkaException: Failed to construct kafka producer
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:457) ~[kafka-clients-2.0.1.jar:na]
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:289) ~[kafka-clients-2.0.1.jar:na]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.createKafkaProducer(DefaultKafkaProducerFactory.java:318) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.createProducer(DefaultKafkaProducerFactory.java:305) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.getTheProducer(KafkaTemplate.java:437) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:367) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:184) ~[spring-kafka-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at com.demo.kafka.config.producer.Sender.send(Sender.java:28) ~[classes/:na]
at com.demo.kafka.SpringBootKafkaJsonApplication.run(SpringBootKafkaJsonApplication.java:22) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
... 5 common frames omitted
Caused by: org.apache.kafka.common.KafkaException: com.fasterxml.jackson.databind.ser.std.StringSerializer is not an instance of org.apache.kafka.common.serialization.Serializer
at org.apache.kafka.common.config.AbstractConfig.getConfiguredInstance(AbstractConfig.java:304) ~[kafka-clients-2.0.1.jar:na]
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:362) ~[kafka-clients-2.0.1.jar:na]
... 14 common frames omitted
Sender.java
@Data
@NoArgsConstructor
public class Sender {
private static final Logger LOGGER = LoggerFactory.getLogger(Sender.class);
@Value("${kafka.topic-json}")
private String jsonTopic;
@Autowired
private KafkaTemplate<String, Car> kafkaTemplate;
public void send(Car car) {
LOGGER.info("sending car='{}'", car.toString());
kafkaTemplate.send(jsonTopic, car);
}
SenderConfig.java
@Configuration
public class SenderConfig {
@Value("${kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
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, JsonSerializer.class);
return props;
@Bean
public ProducerFactory<String, Car> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
@Bean
public KafkaTemplate<String, Car> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
@Bean
public Sender sender() {
return new Sender();
}
Receiver.java
public class Receiver {
private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);
private CountDownLatch latch = new CountDownLatch(1);
public CountDownLatch getLatch() {
return latch;
@Value("${kafka.topic-json}")
private String jsonTopic;
@KafkaListener(topics = "jsont")
public void receive(Car car) {
LOGGER.info("received car='{}'", car.toString());
latch.countDown();
}
ReceiverConfig.java
@Configuration
@EnableKafka
public class ReceiverConfig {
@Value("${kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "json");
return props;
@Bean
public ConsumerFactory<String, Car> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
new JsonDeserializer<>(Car.class));
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Car> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, Car> factory =new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
@Bean
public Receiver receiver() {
return new Receiver();
}
MainJava
@SpringBootApplication
public class SpringBootKafkaJsonApplication implements CommandLineRunner {
@Autowired
private Sender sender;
public static void main(String[] args) {
SpringApplication.run(SpringBootKafkaJsonApplication.class, args);
@Override
public void run(String... args) throws Exception {
sender.send(Car.builder().id("1").make("ALTO").manufacturer("Maruti Suzuki").build());
}
application.yml
kafka:
topic-json: jsont
bootstrap-servers: localhost:9092
发布于 2018-12-16 11:16:58
试着使用
org.apache.kafka.common.serialization.StringSerializer
从…
Org.apache.kafka:kafka-客户端jar
而不是
com.fasterxml.jackson.databind.ser.std.StringSerializer
发布于 2020-03-06 17:37:45
// 1,需要更正props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,JsonSerializer.class);
//2如果像Employee这样的特定类
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,Employee.class.getName();
//3如果通用案例创建一个接口而不是雇员类,并允许每个类扩展该接口并在配置中使用该接口
//Employee类示例
package com.start.kafka;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Serializer;
import java.io.Serializable;
import java.util.Map;
//Developed by Arun Singh
public class Employee implements Serializable, Serializer {
Integer empId;
String empName;
Address add;
public Employee() {
public Employee(Integer empId, String empName, Address add) {
this.empId = empId;
this.empName = empName;
this.add = add;
public Integer getEmpId() {
return empId;
public String getEmpName() {
return empName;
public Address getAdd() {
return add;
public void setEmpId(Integer empId) {
this.empId = empId;
public void setEmpName(String empName) {
this.empName = empName;
public void setAdd(Address add) {
this.add = add;
public void configure(Map configs, boolean isKey) {
public byte[] serialize(String s, Object o) {
return new byte[0];
public byte[] serialize(String topic, Headers headers, Object data) {
return new byte[0];
独立的南瓜 · 开发好能重构的代码,都是这么干的- 华为云开发者联盟 2 周前 |