JsonSerializer无法正常运行卡夫卡

时间:2019-11-18 10:54:13

标签: apache-kafka spring-kafka

我正在使用kafka,我有一个Notification类,正在使用spring-kafka进行序列化。

package com.code2hack.notification;

public class Notification {
    private Object message;
    private NotificationType type;

    public static Notification create(NotificationType type, Object message){
        return new Notification(message,type);
    }

    public Notification(){

    }
    public Notification(Object message, NotificationType type){
        this.message = message;
        this.type = type;
    }

    @Override
    public String toString() {
        return "Notification{" +
                "message=" + message +
                ", type=" + type +
                '}';
    }

    public <T> T getMessage(Class<T> type){
        return (T)this.message;
    }
    public NotificationType getType(){
        return this.type;
    }
    public void setType(NotificationType type){
        this.type = type;
    }
    public void setMessage(Object message){
        this.message = message;
    }


}

这是我的配置

spring:
  kafka:
    producer:
      bootstrap-servers: localhost:9092
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.springframework.kafka.support.serializer.JsonSerializer

当我尝试使用来自消费者的通知时,通知中缺少我的消息部分,我可以接收类型。

我什至尝试了kafka console-consumer,那里也只打印了我的通知消息中的type字段。

我不知道我在想什么。

我的消费者配置为

package com.code2hack.booking;

import com.code2hack.notification.Notification;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.support.serializer.JsonDeserializer;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableKafka
public class KafkaConfiguration {
    @Value("${spring.kafka.consumer.bootstrap-servers}")
    private String address;

    @Bean
    public ConsumerFactory<String, Notification> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(
                ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
                address);
        props.put(
                ConsumerConfig.GROUP_ID_CONFIG,
                "booking");
        JsonDeserializer<Notification> ds = new JsonDeserializer<>();
        ds.addTrustedPackages("*");
        return new DefaultKafkaConsumerFactory<>(props,
                new StringDeserializer(),
                ds);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, Notification>
    kafkaListenerContainerFactory() {

        ConcurrentKafkaListenerContainerFactory<String, Notification> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }
}

这是我的消费者

@KafkaListener(topics = "sql-insert",groupId = "booking")
    public void onNotification(@Payload Notification notification){
        handleNotification(notification);
    }

请帮助我。

注意:实际上问题出在Kafka中的JsonSerializer。我尝试使用以下代码,但未正确序列化对象。

public static void main(String[] args) {

        //SpringApplication.run(BookingServiceApplication.class, args);
        Notification notification = Notification.create(NotificationType.NEW_SCHEDULED,"Hellow how are you");
        byte[] serialize = new JsonSerializer<Notification>().serialize("sql-insert", notification);
        System.out.println(new String(serialize));
    }

它给了我输出。

{"type":"NEW_SCHEDULED"}

有什么办法可以解决它。

1 个答案:

答案 0 :(得分:1)

除非创建自定义序列化程序,否则Jackson仅适用于JavaBean语义; message没有吸气剂;您需要为message属性添加一个简单的getter。