新建spring boot項目
湖濱網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,湖濱網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為湖濱上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的湖濱做網(wǎng)站的公司定做!
這里使用intellij IDEA
添加kafka集成maven
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
項目中application.properties 添加
spring.kafka.bootstrap-servers=vm208:9092,vm:9092,vm50:9092 spring.kafka.consumer.auto-offset-reset=latest spring.kafka.consumer.group-id=local_test spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.acks=1
新建KafkaConsumer消費類
package com.example.demo.consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class KafkaConsumer { private Logger logger = LoggerFactory.getLogger(this.getClass()); @KafkaListener(topics = {"test"}) public void listen(ConsumerRecord<?, ?> record) { System.out.printf("offset = %d,key =%s,value=%s\n", record.offset(), record.key(), record.value()); } }
啟動spring-boot程序,在kafka集群,模擬發(fā)送topic,檢驗接收
編寫producer代碼
package com.example.demo.producer; import org.apache.kafka.clients.producer.ProducerRecord; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; @Component public class KafkaProducer { @Autowired private KafkaTemplate kafkaTemplate; String topic="test"; public void sendMessage(String key,String data){ kafkaTemplate.send(new ProducerRecord(topic,key,data)); } }
建立一個restful模擬發(fā)送( //http://localhost:8080/kafka/send.do?key=2&data=allen-test-message)
package com.example.demo.controller; import com.example.demo.producer.KafkaProducer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class ProducerController { @Autowired private KafkaProducer kafkaProducer; @RequestMapping(value = "/kafka/send.do", method = RequestMethod.GET) public String sendMessage(@RequestParam(value = "key") String key, @RequestParam(value = "data") String data) { kafkaProducer.sendMessage(key, data); return "sucess"; } }
可以發(fā)現(xiàn) spring-kafka大大減少了代碼工作量.
官方文檔: https://docs.spring.io/spring-kafka/docs/1.2.2.RELEASE/reference/html/
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享名稱:springboot與kafka集成的示例代碼
路徑分享:http://aaarwkj.com/article48/igggep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、微信公眾號、網(wǎng)站策劃、電子商務(wù)、域名注冊、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)