您好,登錄后才能下訂單哦!
這篇文章主要介紹springboot 1.5.2 集成kafka的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
具體如下:
隨著spring boot 1.5版本的發(fā)布,在spring項(xiàng)目中與kafka集成更為簡(jiǎn)便。
添加依賴
compile("org.springframework.kafka:spring-kafka:1.1.2.RELEASE")
添加application.properties
#kafka # 指定kafka 代理地址,可以多個(gè) spring.kafka.bootstrap-servers=192.168.59.130:9092,192.168.59.131:9092,192.168.59.132:9092 # 指定默認(rèn)消費(fèi)者group id spring.kafka.consumer.group-id=myGroup # 指定默認(rèn)topic id spring.kafka.template.default-topic= my-replicated-topic # 指定listener 容器中的線程數(shù),用于提高并發(fā)量 spring.kafka.listener.concurrency= 3 # 每次批量發(fā)送消息的數(shù)量 spring.kafka.producer.batch-size= 1000
configuration 啟用kafka
package cn.xiaojf.today.data.kafka.configuration; import org.springframework.context.annotation.Configuration; import org.springframework.kafka.annotation.EnableKafka; /** * kafka 配置 * @author xiaojf 2017/3/24 14:09 */ @Configuration @EnableKafka public class KafkaConfiguration { }
消息生產(chǎn)者
package cn.xiaojf.today.data.kafka.producer; import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.clients.producer.RecordMetadata; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaOperations; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.support.ProducerListener; import org.springframework.stereotype.Component; /** * 消息生產(chǎn)者 * @author xiaojf 2017/3/24 14:36 */ @Component public class MsgProducer { @Autowired private KafkaTemplate<String,String> kafkaTemplate; public void send() { kafkaTemplate.send("my-replicated-topic","xiaojf"); kafkaTemplate.send("my-replicated-topic","xiaojf"); kafkaTemplate.metrics(); kafkaTemplate.execute(new KafkaOperations.ProducerCallback<String, String, Object>() { @Override public Object doInKafka(Producer<String, String> producer) { //這里可以編寫(xiě)kafka原生的api操作 return null; } }); //消息發(fā)送的監(jiān)聽(tīng)器,用于回調(diào)返回信息 kafkaTemplate.setProducerListener(new ProducerListener<String, String>() { @Override public void onSuccess(String topic, Integer partition, String key, String value, RecordMetadata recordMetadata) { } @Override public void onError(String topic, Integer partition, String key, String value, Exception exception) { } @Override public boolean isInterestedInSuccess() { return false; } }); } }
消息消費(fèi)者
package cn.xiaojf.today.data.kafka.consumer; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; /** * 消息消費(fèi)者 * @author xiaojf 2017/3/24 14:36 */ @Component public class MsgConsumer { @KafkaListener(topics = {"my-replicated-topic","my-replicated-topic2"}) public void processMessage(String content) { System.out.println(content); } }
以上是“springboot 1.5.2 集成kafka的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。