SpringBoot實現(xiàn)kafka多源配置的示例代碼
背景
實際開發(fā)中,不同的topic可能來自不同的集群,所以就需要配置不同的kafka數(shù)據(jù)源,基于springboot自動配置的思想,最終通過配置文件的配置,自動生成生產(chǎn)者及消費(fèi)者的配置。
核心配置
自動化配置類
import com.example.kafka.autoconfig.CustomKafkaDataSourceRegister;
import com.example.kafka.autoconfig.kafkaConsumerConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.kafka.annotation.EnableKafka;
@EnableKafka
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnWebApplication
@EnableConfigurationProperties({kafkaConsumerConfig.class})
@Import({CustomKafkaDataSourceRegister.class})
public class MyKafkaAutoConfiguration implements BeanFactoryAware, SmartInstantiationAwareBeanPostProcessor {
public MyKafkaAutoConfiguration() {
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
beanFactory.getBean(CustomKafkaDataSourceRegister.class);
}
}
注冊生產(chǎn)者、消費(fèi)者核心bean到spring
public void afterPropertiesSet() {
Map<String, ConsumerConfigWrapper> factories = kafkaConsumerConfig.getFactories();
if (factories != null && !factories.isEmpty()) {
factories.forEach((factoryName, consumerConfig) -> {
KafkaProperties.Listener listener = consumerConfig.getListener();
Integer concurrency = consumerConfig.getConcurrency();
// 創(chuàng)建監(jiān)聽容器工廠
ConcurrentKafkaListenerContainerFactory<String, String> containerFactory = createKafkaListenerContainerFactory(consumerConfig.buildProperties(), listener, concurrency);
// 注冊到容器
if (!beanFactory.containsBean(factoryName)) {
beanFactory.registerSingleton(factoryName, containerFactory);
}
});
}
Map<String, KafkaProperties.Producer> templates = kafkaProducerConfig.getTemplates();
if (!ObjectUtils.isEmpty(templates)) {
templates.forEach((templateName, producerConfig) -> {
//registerBean(beanFactory, templateName, KafkaTemplate.class, propertyValues);
//注冊spring bean的兩種方式
registerBeanWithConstructor(beanFactory, templateName, KafkaTemplate.class, producerFactoryValues(producerConfig.buildProperties()));
});
}
}
配置spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.kafka.MyKafkaAutoConfiguration
yml配置
spring:
kafka:
multiple:
consumer:
factories:
test-factory:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
bootstrap-servers: 192.168.56.112:9092
group-id: group_a
concurrency: 25
fetch-min-size: 1048576
fetch-max-wait: 3000
listener:
type: batch
properties:
spring-json-trusted-packages: '*'
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
auto-offset-reset: latest
producer:
templates:
test-template:
bootstrap-servers: 192.168.56.112:9092
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
使用


到此這篇關(guān)于SpringBoot實現(xiàn)kafka多源配置的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot kafka多源配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java8中parallelStream性能測試及結(jié)果分析
本篇文章給大家用代碼實例做了segmentfaultjava8中parallelStream性能測試,并對測試結(jié)果做了說明,需要的朋友學(xué)習(xí)下吧。2018-01-01
Java Calendar類常用示例_動力節(jié)點Java學(xué)院整理
從JDK1.1版本開始,在處理日期和時間時,系統(tǒng)推薦使用Calendar類進(jìn)行實現(xiàn)。接下來通過實例代碼給大家詳細(xì)介紹Java Calendar類相關(guān)知識,需要的朋友參考下吧2017-04-04
線上dubbo線程池耗盡CyclicBarrier線程屏障異常解決記錄
系統(tǒng)相關(guān)使用人員反饋系統(tǒng)故障,這篇文章主要介紹了線上dubbo線程池耗盡CyclicBarrier線程屏障異常解決的記錄,有需要的朋友可以借鑒參考下2022-03-03
客戶端Socket與服務(wù)端ServerSocket串聯(lián)實現(xiàn)網(wǎng)絡(luò)通信
這篇文章主要為大家介紹了客戶端Socket與服務(wù)端ServerSocket串聯(lián)實現(xiàn)網(wǎng)絡(luò)通信的內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03
dubbo如何設(shè)置連接zookeeper權(quán)限
這篇文章主要介紹了dubbo如何設(shè)置連接zookeeper權(quán)限問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
在Java8與Java7中HashMap源碼實現(xiàn)的對比
這篇文章主要介紹了在Java8與Java7中HashMap源碼實現(xiàn)的對比,內(nèi)容包括HashMap 的原理簡單介紹、結(jié)合源碼在Java7中是如何解決hash沖突的以及優(yōu)缺點,結(jié)合源碼以及在Java8中如何解決hash沖突,balance tree相關(guān)源碼介紹,需要的朋友可以參考借鑒。2017-01-01
Java實現(xiàn)獲取服務(wù)器資源(內(nèi)存,負(fù)載,磁盤容量)
這篇文章主要為大家詳細(xì)介紹了如何Java實現(xiàn)獲取服務(wù)器資源信息,包括內(nèi)存,負(fù)載,磁盤容量等內(nèi)容,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-07-07

