spring boot與kafka集成的簡(jiǎn)單實(shí)例
本文介紹了spring boot與kafka集成的簡(jiǎn)單實(shí)例,分享給大家,具體如下:
引入相關(guān)依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>1.1.1.RELEASE</version> </dependency>
從依賴(lài)項(xiàng)的引入即可看出,當(dāng)前spring boot(1.4.2)還不支持完全以配置項(xiàng)的配置來(lái)實(shí)現(xiàn)與kafka的無(wú)縫集成。也就意味著必須通過(guò)java config的方式進(jìn)行手工配置。
定義kafka基礎(chǔ)配置
與redisTemplate及jdbcTemplate等類(lèi)似。spring同樣提供了org.springframework.kafka.core.KafkaTemplate作為kafka相關(guān)api操作的入口。
import java.util.HashMap; import java.util.Map; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.common.serialization.StringSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.kafka.annotation.EnableKafka; import org.springframework.kafka.core.DefaultKafkaProducerFactory; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.ProducerFactory; @Configuration @EnableKafka public class KafkaProducerConfig { public Map<String, Object> producerConfigs() { Map<String, Object> props = new HashMap<>(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.179.200:9092"); props.put(ProducerConfig.RETRIES_CONFIG, 0); props.put(ProducerConfig.BATCH_SIZE_CONFIG, 4096); props.put(ProducerConfig.LINGER_MS_CONFIG, 1); props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 40960); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); return props; } public ProducerFactory<String, String> producerFactory() { return new DefaultKafkaProducerFactory<>(producerConfigs()); } @Bean public KafkaTemplate<String, String> kafkaTemplate() { return new KafkaTemplate<String, String>(producerFactory()); } }
KafkaTemplate依賴(lài)于ProducerFactory,而創(chuàng)建ProducerFactory時(shí)則通過(guò)一個(gè)Map指定kafka相關(guān)配置參數(shù)。通過(guò)KafkaTemplate對(duì)象即可實(shí)現(xiàn)消息發(fā)送。
kafkaTemplate.send("test-topic", "hello"); or kafkaTemplate.send("test-topic", "key-1", "hello");
監(jiān)聽(tīng)消息配置
import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.common.serialization.StringDeserializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.kafka.annotation.EnableKafka; import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; import org.springframework.kafka.config.KafkaListenerContainerFactory; import org.springframework.kafka.core.ConsumerFactory; import org.springframework.kafka.core.DefaultKafkaConsumerFactory; import org.springframework.kafka.listener.ConcurrentMessageListenerContainer; import java.util.HashMap; import java.util.Map; @Configuration @EnableKafka public class KafkaConsumerConfig { @Bean public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() { ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); factory.setConsumerFactory(consumerFactory()); factory.setConcurrency(3); factory.getContainerProperties().setPollTimeout(3000); return factory; } public ConsumerFactory<String, String> consumerFactory() { return new DefaultKafkaConsumerFactory<>(consumerConfigs()); } public Map<String, Object> consumerConfigs() { Map<String, Object> propsMap = new HashMap<>(); propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.179.200:9092"); propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false); propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100"); propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000"); propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group"); propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest"); return propsMap; } @Bean public Listener listener() { return new Listener(); } }
實(shí)現(xiàn)消息監(jiān)聽(tīng)的最終目標(biāo)是得到監(jiān)聽(tīng)器對(duì)象。該監(jiān)聽(tīng)器對(duì)象自行實(shí)現(xiàn)。
import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.annotation.KafkaListener; import java.util.Optional; public class Listener { @KafkaListener(topics = {"test-topic"}) public void listen(ConsumerRecord<?, ?> record) { Optional<?> kafkaMessage = Optional.ofNullable(record.value()); if (kafkaMessage.isPresent()) { Object message = kafkaMessage.get(); System.out.println("listen1 " + message); } } }
只需用@KafkaListener指定哪個(gè)方法處理消息即可。同時(shí)指定該方法用于監(jiān)聽(tīng)kafka中哪些topic。
注意事項(xiàng)
定義監(jiān)聽(tīng)消息配置時(shí),GROUP_ID_CONFIG配置項(xiàng)的值用于指定消費(fèi)者組的名稱(chēng),如果同組中存在多個(gè)監(jiān)聽(tīng)器對(duì)象則只有一個(gè)監(jiān)聽(tīng)器對(duì)象能收到消息。
@KafkaListener中topics屬性用于指定kafka topic名稱(chēng),topic名稱(chēng)由消息生產(chǎn)者指定,也就是由kafkaTemplate在發(fā)送消息時(shí)指定。
KEY_DESERIALIZER_CLASS_CONFIG與VALUE_DESERIALIZER_CLASS_CONFIG指定key和value的編碼、解碼策略。kafka用key值確定value存放在哪個(gè)分區(qū)中。
后記
時(shí)間是解決問(wèn)題的有效手段之一。
在spring boot 1.5版本中即可實(shí)現(xiàn)spring boot與kafka Auto-configuration
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot?整合?clickhouse的實(shí)現(xiàn)示例
本文主要介紹了springboot?整合?clickhouse的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02關(guān)于SpringBoot中的XA事務(wù)詳解
這篇文章主要介紹了關(guān)于SpringBoot中的XA事務(wù)詳解,事務(wù)管理可以確保數(shù)據(jù)的一致性和完整性,同時(shí)也可以避免數(shù)據(jù)丟失和沖突等問(wèn)題。在分布式環(huán)境中,XA?事務(wù)是一種常用的事務(wù)管理方式,需要的朋友可以參考下2023-07-07解決mybatisplus的分頁(yè)插件和條件構(gòu)造器自定義SQL遇到的BUG
這篇文章主要介紹了解決mybatisplus的分頁(yè)插件和條件構(gòu)造器自定義SQL遇到的BUG,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Redis實(shí)現(xiàn)商品秒殺功能頁(yè)面流程
這篇文章主要介紹了Redis實(shí)現(xiàn)商品秒殺功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09Java實(shí)現(xiàn)異步延遲隊(duì)列的方法詳解
目前系統(tǒng)中有很多需要用到延時(shí)處理的功能,本文就為大家介紹了Java實(shí)現(xiàn)異步延遲隊(duì)列的方法,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-03-03Spring+MyBatis實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離方案
本文主要介紹了Spring+MyBatis實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離方案。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01SpringBatch從入門(mén)到精通之StepScope作用域和用法詳解
這篇文章主要介紹了SpringBatch從入門(mén)到精通之StepScope作用域和用法詳解,主要包括IOC容器中幾種bean的作用范圍以及可能遇到的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05maven setting多倉(cāng)庫(kù)配置方式
這篇文章主要介紹了maven setting多倉(cāng)庫(kù)配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05