简体   繁体   English

Spring Kafka - 多个 model 类的消费者工厂

[英]Spring Kafka - Consumer Factory for multiple model classes

I have recently started learning Kafka and I'm trying to make basic communication between two springboot microservices.我最近开始学习 Kafka,我正在尝试在两个 springboot 微服务之间进行基本通信。

Currently I have a model class User.目前我有一个 model class 用户。 I am sending the User object from one Producer service and receiving the same on separate Consumer service via Kafka.我从一个生产者服务发送用户 object,并通过 Kafka 在单独的消费者服务上接收相同的信息。 I have configured the ConsumerFactory in the following way:我按以下方式配置了 ConsumerFactory:

public class User{
    Integer id;
    String name;
    String place;
   // getters, setters
}

Consumer Config:消费者配置:

@Configuration
public class ConsumerConfig{

    @Bean
    public ConsumerFactory<String, User> consumerFactory(){
        Map<String, Object> config = new HashMap<>();
        config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
        config.put(ConsumerConfig.GROUP_ID_CONFIG,"group1");
        config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
        return new DefaultKafkaConsumerFactory<>(config, new StringSerializer(), new JsonDeserializer<>(User.class));
   }

   @Bean
    public ConcurrentKafkaListenerContainerFactory<String, User> 
      kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, User> factory =
          new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }

This is working properly and I am able to deserialize the User object in the consumer service.这工作正常,我能够在消费者服务中反序列化用户 object。

Now suppose if I have 20 more such model classes such as Product, Order, Payment etc. Do I need to create ConsumerFactory for each of model classes as JsonDeserializer constructor takes the target class type as parameter in return new DefaultKafkaConsumerFactory<>(config, new StringSerializer(), new JsonDeserializer<>(User.class));现在假设我还有 20 个这样的 model 类,例如产品、订单、付款等。我是否需要为每个 model 类创建 ConsumerFactory,因为 JsonDeserializer 构造函数将目标 class 类型作为参数return new DefaultKafkaConsumerFactory<>(config, new StringSerializer(), new JsonDeserializer<>(User.class));

Is there any generic way of doing this for all model classes?对于所有 model 类,是否有任何通用的方法来执行此操作?

Appreciate all of your suggestions.感谢您的所有建议。

Since the sender is also Spring, the JsonSerializer will add type information in headers that the deserializer uses to deserialize (by default);由于发件人也是 Spring,因此JsonSerializer将在反序列化程序用于反序列化的标头中添加类型信息(默认情况下); the generic parameter is only used when no type information is available in the headers (or using type headers is disabled).通用参数仅在标头中没有类型信息可用时使用(或禁用类型标头)。

If the model is in different packages on the producer and consumer, then you can add type mapping configuration to map the consumer type to the producer type.如果 model 在生产者和消费者的不同包中,那么可以添加类型映射配置 map 消费者类型到生产者类型。

See https://docs.spring.io/spring-kafka/docs/current/reference/html/#json-serde参见https://docs.spring.io/spring-kafka/docs/current/reference/html/#json-serde

And https://docs.spring.io/spring-kafka/docs/current/reference/html/#serdes-mapping-typeshttps://docs.spring.io/spring-kafka/docs/current/reference/html/#serdes-mapping-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM