简体   繁体   English

使用 SpringBoot 外化 Spring Kafka 配置

[英]Externalizing Spring Kafka configuration with SpringBoot

I have this kafka config class我有这个卡夫卡配置 class

@Configuration
public class KafkaConfiguration {
    @Value("${KAFKA_SERVERS}")
    private String kafkaServers;


    @Bean
    ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(this.consumerFactory());
        factory.setMessageConverter(new StringJsonMessageConverter());
        return factory;
    }

    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(this.consumerConfigs());
    }

    public Map<String, Object> consumerConfigs() {

        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServers);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "foo-group-id");
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
        props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");
        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return props;
    }

    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServers);
        props.put(ProducerConfig.RETRIES_CONFIG, 0);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
        props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
        return props;
    }

    @Bean
    public KafkaProperties.Listener listener() {
        return new KafkaProperties.Listener();
    }

    @Bean
    public KafkaTemplate<String, Map<String, Object>> kafkaTemplateHistory() {
        return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(this.producerConfigs()));
    }
}

And I want to use spring boot auto configuration so to get ride of the config class above, so I set almost all the properties in appliction.yml我想使用 spring 引导自动配置,以便获得上面的配置 class ,所以我在appliction.yml中设置了几乎所有属性

but for these two properties:但是对于这两个属性:

ProducerConfig.LINGER_MS_CONFIG
ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG

spring.kafka.consumer doesn't propose me anything spring.kafka.consumer没有向我提出任何建议

  1. So how can I set them in appliction.yml ?那么如何在appliction.yml中设置它们?
  2. Also how to externalize this setting line factory.setMessageConverter(new StringJsonMessageConverter());还有如何外部化这个设置行factory.setMessageConverter(new StringJsonMessageConverter()); ? ?

you can set those properties by using spring.kafka.producer.properties.*您可以使用spring.kafka.producer.properties 设置这些属性。*

Additional producer-specific properties used to configure the client.用于配置客户端的其他特定于生产者的属性。

spring:
   kafka:
     producer:
       properties:
         linger.ms: 10

For consumer additional properties you can use spring.kafka.consumer.properties.* with trusted package so you don't need the converter对于消费者附加属性,您可以将spring.kafka.consumer.properties.*与受信任的 package 一起使用,因此您不需要转换器

spring:
   kafka:
     consumer:
       properties:
         sesssion.timeout.ms: 60000

And for consumer desrializer you can use this spring.kafka.consumer.value-deserializer对于消费者解串器,您可以使用此spring.kafka.consumer.value-deserializer

Deserializer class for values.解串器 class 用于值。

 spring:
   kafka:
    consumer:
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
      properties:
        spring:
          json:
            trusted:
              packages: 'learn.kafka.model'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM