java实现kafka支持多种序列化值

1、kafkaconfig

package com.catl.tools.kafka.config;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.LoggingProducerListener;
import org.springframework.kafka.support.ProducerListener;
import org.springframework.kafka.support.converter.RecordMessageConverter;

import java.util.Map;

/**
* kafkaTemplate多实例使用
* 例如当key胡序列化类不同的时候,这个时候就需要适配性的进行序列化
*/
@EnableKafka
@Configuration
public class KafkaTemplateConfig {
@Autowired
private KafkaProperties kafkaProperties;

@Bean(name="defaultKafkaTemplate")
public KafkaTemplate<?,?> kafkaTemplate(@Qualifier("defaultKafkaProducerFactory")ProducerFactory<Object,Object> kafkaProducerFactory,
ProducerListener<Object,Object> kafkaProducerListener,
ObjectProvider<RecordMessageConverter> messageConverters){
KafkaTemplate<Object, Object> kafkaTemplate = new KafkaTemplate<>(kafkaProducerFactory);
messageConverters.ifUnique(kafkaTemplate::setMessageConverter);
kafkaTemplate.setProducerListener(kafkaProducerListener);
kafkaTemplate.setDefaultTopic(kafkaProperties.getTemplate().getDefaultTopic());
return kafkaTemplate;
}
@Bean
public ProducerListener<Object,Object> kafkaProducerListener(){
return new LoggingProducerListener<>();
}
@Bean(name="defaultKafkaProducerFactory")
public ProducerFactory<Object,Object> kafkaProducerFactory(){
DefaultKafkaProducerFactory factory = new DefaultKafkaProducerFactory<>(kafkaProperties.buildProducerProperties());
String transactionIdPrefix = kafkaProperties.getProducer().getTransactionIdPrefix();
if (transactionIdPrefix!=null){
factory.setTransactionIdPrefix(transactionIdPrefix);
}
return factory;
}
@Bean(name="newKafkaProducerFactory")
public ProducerFactory<Object,Object> newProducerFactory(){
Map<String, Object> producerProperties = kafkaProperties.buildProducerProperties();
//修改参数名称
//producerProperties.put(ProducerConfig.ACKS_CONFIG,"all");
producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
DefaultKafkaProducerFactory<Object, Object> factory = new DefaultKafkaProducerFactory<>(producerProperties);
String transactionIdPrefix = kafkaProperties.getProducer().getTransactionIdPrefix();
if (transactionIdPrefix!=null){
factory.setTransactionIdPrefix(transactionIdPrefix);
}
return factory;
}
@Bean(name="newKafkaTemplate")
public KafkaTemplate<?,?> newKakfaTemplate(@Qualifier("newKafkaProducerFactory") ProducerFactory<Object,Object> kafkaProducerFactory,
ProducerListener<Object,Object> kafkaProducerListener,
ObjectProvider<RecordMessageConverter> messageConverters){
KafkaTemplate<Object, Object> kafkaTemplate = new KafkaTemplate<>(kafkaProducerFactory);
messageConverters.ifUnique(kafkaTemplate::setMessageConverter);
kafkaTemplate.setProducerListener(kafkaProducerListener);
kafkaTemplate.setDefaultTopic(kafkaProperties.getTemplate().getDefaultTopic());
return kafkaTemplate;
}


}

2、kafkaUtils

package com.catl.tools.kafka.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
@Component
public class BaseKafkaUtils {
@Autowired
@Qualifier("defaultKafkaTemplate")
private KafkaTemplate defaultKafkaTemplate;
private static KafkaTemplate defaultKafkaTemplateStatic;

@Autowired
@Qualifier("newKafkaTemplate")
private KafkaTemplate<Object,Object> newKafkaTemplate;
private static KafkaTemplate<Object,Object> newKafkaTemplateStatic;

@PostConstruct
public void init(){
defaultKafkaTemplateStatic=defaultKafkaTemplate;
newKafkaTemplateStatic=newKafkaTemplate;
}

/**
* Kafka发送消息
*/
public static void sendMessage(String topic,Object content){
defaultKafkaTemplateStatic.send(topic,content);
}

public static void newKafkaTempalteSend(String topic,Object content){
newKafkaTemplateStatic.send(topic,content);
}

}

3、使用示例

public static void main(String[] args) {
String message="测试数据";
   //支持StringSerialize
BaseKafkaUtils.newKafkaTempalteSend(FlinkConstant.CAR_DATA_TOPIC, message);
  //BuyteArray
BaseKafkaUtils.sendMessage("CAR_DATA_TOPIC", message.getBytes(StandardCharsets.UTF_8));
}

posted on 2022-07-12 10:44  程序狗狗  阅读(511)  评论(0编辑  收藏  举报

导航