消费kafka 使用containerFactory调用不同的kafka配置
@KafkaListener(topics = "${problemReceiveTopics}",groupId = "${problemReceiveGroupId}",containerFactory = "kafkaProblemListenerContainerFactory")
public ResponseDTO batchInsertBaseLineDataInfo(ConsumerRecord<String, String> message) {
String value = message.value();
BaseLineDataInfoDTO baseLineDataInfoDTO = com.alibaba.fastjson.JSON.parseObject(value , BaseLineDataInfoDTO.class);
return baseLineDataInfoService.doBaseLineDataInfoInterface(baseLineDataInfoDTO);
}
KafkaCarConsumerConfig 配置
@Configuration
public class KafkaProblemReceiveConfig {
@Value("${spring.kafka.problemReceive.bootstrap-servers}")
private String servers;
@Value("${spring.kafka.problemReceive.enable-auto-commit}")
private boolean enableAutoCommit;
@Value("${spring.kafka.problemReceive.auto-offset-reset}")
private String autoOffsetReset;
@Value("${spring.kafka.problemReceive.properties.security.protocol}")
private String securityProtocol;
@Value("${spring.kafka.problemReceive.properties.sasl.mechanism}")
private String saslMechanism;
@Value("${spring.kafka.problemReceive.properties.sasl.jaas.config}")
private String saslJaasConfig;
/**
* 创建
* @return
*/
@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaProblemListenerContainerFactory() {
//添加工厂
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
//添加工厂类型
factory.setConsumerFactory(problemReceiveFactory());
//设置超时时间
factory.getContainerProperties().setPollTimeout(1500);
//返回数据
return factory;
}
/**
* 创建
* @return
*/
public ConsumerFactory<String, String> problemReceiveFactory() {
//加入工厂
return new DefaultKafkaConsumerFactory<>(problemReceiveConfigs());
}
/**
* 问题
* @return
*/
public Map<String, Object> problemReceiveConfigs() {
//添加kafka配置
Map<String, Object> propsMap = new HashMap<>(16);
//添加BOOTSTRAP_SERVERS_CONFIG
propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
//添加ENABLE_AUTO_COMMIT_CONFIG
propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
//添加KEY_DESERIALIZER_CLASS_CONFIG
propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
//添加VALUE_DESERIALIZER_CLASS_CONFIG
propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
//添加AUTO_OFFSET_RESET_CONFIG
propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
propsMap.put("security.protocol", securityProtocol);
propsMap.put("sasl.mechanism", saslMechanism);
propsMap.put("sasl.jaas.config",saslJaasConfig);
return propsMap;
}
}
nacos 配置
problemReceive:
bootstrap-servers: 0000000:9092,000000:9092,000000:9092
enable-auto-commit: true
auto-offset-reset: latest
properties:
security:
protocol: 1111
sasl:
mechanism: 22222
jaas:
config: