简体   繁体   中英

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.

Currently I have a model class User. I am sending the User object from one Producer service and receiving the same on separate Consumer service via Kafka. I have configured the ConsumerFactory in the following way:

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.

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));

Is there any generic way of doing this for all model classes?

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); 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.

See 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-types

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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