新建 Spring Boot 项目
这里使用 Ide IntelliJ
添加 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 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> <groupdid> com.example </groupid </groupid> </ModelVersion> <groupdid> Com.example </Groupcing </groupid> <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/> <!-Pencarian Orangtua dari Repositori-> </induk> <properties> <proyity.build.sourceencoding> UTF-8 </project.build.sourceencoding> <poject.report.outputenCoding> UTF-8 </project.reporting.outputenceDing> <Java.8 </project.reporting.outputenCoding> <JaVa <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 </arttifactid> <scope> test </seupope> </dependency> </dependencies> <build> <dlugin> <llupin> <groupid> org.springframework.boot </groupid> <lugin> <groupid> <ArTifactId> spring-boot-maven-plugin </artifactid> </lugin> </lugin> </build> </poject>
项目中 Application.properties 添加
spring.kafka.bootstrap-servers = VM208: 9092, VM: 9092, VM50: 9092Spring.kafka.consumer.auto-offset-reset = lateSpring.kafka.consumer.group-id = l ocal_testspring.kafka.consumer.key-ceserializer = org.apache.kafka.common.serialization.stringdeserializerspring.kafka.consumer.value-deseri alizer = org.apache.kafka.common.serialization.stringdeserializerspring.kafka.producer.key-serializer = org.apache.kafka.common.serialization .Stringserializerspring.kafka.producer.value-serializer = org.apache.kafka.common.serialization.stringserializerspring.kafka.producer.acks = 1
新建 Kafkaconsumer 消费类
Paket com.example.demo.consumer; impor org.apache.kafka.clients.consumer.consumerrecord; impor org.slf4j.logger; impor org.slf4j.loggerFactory; impor org.springframework.kafka.annotation.kafkenener; org.springframework.stereotype.component; @ComponentPublic kelas kafkaconsumer {private Logger Logger = loggerFactory.getLogger (this.getClass ()); @Kafkalistener (topik = {"test"}) void public listen (consumeRecord <?,?> Record) {System.out.printf ("Offset =%d, key =%s, value =%s/n", record.offset (), record.key (), record.value ()); }}启动 Spring-Boot 程序, 在 Kafka 集群, 模拟发送 topik, 检验接收
复制代码代码如下: bin/kafka-console-producer.sh --broker-list VM208: 9092, VM210: 9092, VM50: 9092-Tes Topik
编写 Produser 代码
Paket com.example.demo.producer; impor org.apache.kafka.clients.producer.producerrecord; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.kafka.core.kafkatempleate; org.springframework.stereotype.component; @ComponentPublic kelas kafkaproducer {@autowired private kafkatemplate kafkatemplate; String topic = "test"; public void sendMessage (tombol string, data string) {kafkatemplate.send (produceRecord baru (topik, kunci, data)); }}建立一个 Restful 模拟发送 (//http://localhost:8080/kafka/send.do?key=2&data=allen-test-Sessage)
Paket com.example.demo.controller; import com.example.demo.producer.kafkaproducer; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.web.bind.annotation.requestmapping; org.springframework.web.bind.annotation.requestmethod; impor org.springframework.web.bind.annotation.requestparam; impor org.springframework.web.bind.annotation.restController; @RestControllerPublic Publicrollic CLOURCROLTERROLTER ( kafkaproducer; @RequestMapping (value = "/kafka/send.do", method = requestMethod.get) string publik SendMessage (@RequestParam (value = "key") Key string, @RequestParam (value = "data") data string) {kafkaproducer.sendMessage (key, data); mengembalikan "kesucian"; }}可以发现 Spring-kafka 大大减少了代码工作量.
官方文档: https://docs.spring.io/spring-kafka/docs/1.2.2.release/reference/html/
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。