多kafka使用

消费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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值