Answer a question

In my spring boot kafka publisher application, I want to provide support for publishing messages both in String(json) or in byte format, because I want to provide support for both json and avro. But Kafka template in spring boot let us define only either one of the templates. Is there a way to use both templates or any other way to provide support for both json and avro?

KafkaTemplate<String, String> works just for string, but i also want to publish avro which should be something like KafkaTemplate<String, byte[]>

Answers

You could try to create KafkaTemplate with different configs:

@Bean
public ProducerFactory<String, String> producerFactoryString() {
    Map<String, Object> configProps = new HashMap<>();
    //additional config parameters .... 
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}

@Bean
public ProducerFactory<String, byte[]> producerFactoryByte() {
    Map<String, Object> configProps = new HashMap<>();
    //additional config parameters ....
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, BytesSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}

@Bean
public KafkaTemplate<String, String> kafkaTemplateString() {
    return new KafkaTemplate<>(producerFactoryString());
}

@Bean
public KafkaTemplate<String, byte[]> kafkaTemplateByte() {
    return new KafkaTemplate<>(producerFactoryByte());
}
Logo

华为、百度、京东云现已入驻,来创建你的专属开发者社区吧!

更多推荐