簡體   English   中英

我可以在運行時向 spring-kafka 中的生產者和消費者提供配置詳細信息嗎?

[英]Any possibility that I can provide configuration details at run time to producer and consumer in spring-kafka?

我們正在使用 spring 引導將我們的項目遷移到微服務。 我有一個要求,我應該能夠使用用戶在運行時提供的配置詳細信息(例如引導服務器和鍵值 serdes)來運行生產者和消費者。

I was able to do this using java Apache Kafka API in the earlier project but I don't see any way to do this using spring-kafka API as it allows to define the producer or consumer related configuration only via Spring java configuration class or application 。特性。

您可以在運行時為消費者和生產者覆蓋您想要的任何屬性。

使用ConcurrentKafkaListenerContainerFactory創建監聽器容器:

@Autowired
ConcurrentKafkaListenerContainerFactory<String, String> factory;

...

    ConcurrentMessageListenerContainer<String, String> container = factory.createContainer("someTopic");
    container.getContainerProperties()
            .getKafkaConsumerProperties()
            .setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "...");
    ...
    container.getContainerProperties().setMessageListener(someListener);
    container.start();

在生產者端,使用這個構造函數:

/**
 * Create an instance using the supplied producer factory and properties, with
 * autoFlush false. If the configOverrides is not null or empty, a new
 * {@link DefaultKafkaProducerFactory} will be created with merged producer properties
 * with the overrides being applied after the supplied factory's properties.
 * @param producerFactory the producer factory.
 * @param configOverrides producer configuration properties to override.
 * @since 2.5
 */
public KafkaTemplate(ProducerFactory<K, V> producerFactory, @Nullable Map<String, Object> configOverrides) {
    this(producerFactory, false, configOverrides);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM