如何使用spring在kafka中创建两个生产者kafka模板,spring接受不同的数据类型一个是jsonnode,另一个是avro?

13z8s7eq  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(332)

我正在尝试开发两个具有不同序列化类型的producer配置类(jsonnode、avro),但是在运行时,我只能示例化一个,另一个不起作用。
头等舱:

@Configuration
@EnableKafka
public class KafkaProducerConfig extends SomeClassConfig{
@Autowired
    private SomeClassProps someClassProps ;

@Bean
    public ProducerFactory<JsonNode, JsonNode> eventProducerFactory() throws UnknownHostException{
    return new DefaultKafkaProducerFactory<JsonNode, JsonNode>(producerConfigs(someClassProps ));
}

@Bean
public Map<String, Object> producerConfigs(SomeClassProps someClassProps ) throws UnknownHostException{
    Properties props = this.initProps(someClassProps );
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,***.getBootstrapServers());      
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.connect.json.JsonSerializer");
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.connect.json.JsonSerializer");
    Map<String, Object> map = new HashMap<>();
    for (final String name: props.stringPropertyNames()) {
        map.put(name, props.getProperty(name));         
    }

    return map;
}

@Bean(name="eventProducerKafkaTemplate")
public KafkaTemplate<JsonNode,JsonNode> eventProducerKafkaTemplate() throws UnknownHostException{       
    return new KafkaTemplate<JsonNode,JsonNode>(eventProducerFactory());
}

}
二等舱:

@Configuration("avroKafkaProducerConfig")
@EnableKafka
public class AvroKafkaProducerConfig extends SomeClassConfig{

    //private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(KafkaProducerConfig.class);
        @Autowired
private SomeClassProps someClassProps ;

    @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
    public StreamsConfig kafkaStreamsConfig(SomeClassProps someClassProps ) throws UnknownHostException {
        Map<String, Object> props = producerConfigs(someClassProps );
        return new StreamsConfig(props);
    }

    @Bean
    public ProducerFactory<SpecificRecord, SpecificRecord> eventProducerFactory() throws UnknownHostException{
        return new DefaultKafkaProducerFactory<SpecificRecord, SpecificRecord>(producerConfigs(someClassProps ));
    }

    @Bean
    public Map<String, Object> producerConfigs(SomeClassProps someClassProps ) throws UnknownHostException{
        Properties props = this.initProps(someClassProps );
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,***.getBootstrapServers());      
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, io.confluent.kafka.serializers.KafkaAvroSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
        Map<String, Object> map = (Map) props;
        System.out.println("Avro"+map.values());

        return map;
    }

    @Bean(name="eventAvroProducerKafkaTemplate")
    public KafkaTemplate<SpecificRecord,SpecificRecord> eventProducerKafkaTemplate() throws UnknownHostException{       
        return new KafkaTemplate<SpecificRecord,SpecificRecord>(eventProducerFactory());
    }

}
当我试图在控制器类中使用这些kafka模板时,它只是初始化json序列化程序,而不是avro序列化程序。
这就是我在其他类中使用这些Kafka模板的方式

@Autowired
    @Qualifier("eventProducerKafkaTemplate")
    private KafkaTemplate<JsonNode, JsonNode> eventProducerKafkaTemplate;

    @Autowired
     @Qualifier("eventAvroProducerKafkaTemplate")
    private KafkaTemplate<SpecificRecord,SpecificRecord> eventAvroProducerKafkaTemplate;

public ReturnTYpe methodName() {

eventProducerKafkaTemplate.send(****,****,****);
eventAvroProducerKafkaTemplate.send(****,****,****);
}
y3bcpkx1

y3bcpkx11#

你必须给我2 producerConfigs 和2 eventProducerFactory bean不同的方法(bean)名称。否则,一个将覆盖另一个。

相关问题