common.KafkaException:com.fasterxml.jackson.databind.ser.std.StringSerializer不是org.apache.kafka.common.serialization.Serializer的实例

时间:2018-12-16 11:06:39

标签: spring spring-boot apache-kafka

我正在开发Spring Boot + Apache Kafka示例。将TOPIC发送到Kafka时出错。

错误:

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

2 个答案:

答案 0 :(得分:3)

尝试使用

  

org.apache.kafka.common.serialization.StringSerializer

来自

  

org.apache.kafka:kafka-客户端jar

代替

  

com.fasterxml.jackson.databind.ser.std.StringSerializer

答案 1 :(得分:0)

// 1,需要纠正 props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,JsonSerializer.class);

// 2 如果是像Employee这样的特定班级,那么

props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,Employee.class.getName());

// 3 如果是一般情况 创建一个接口而不是Employee类,并允许每个类对其进行扩展并在config中使用该接口

//雇员类示例

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];
    }

    public void close() {

    }
}