新建 Boot de mola 项目
这里使用 Intellij Idea
添加 Kafka 集成 Maven
<? xml versão = "1.0" coding = "utf-8"?> <Projeto xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.ww3.org/2001/xmlschaMance xsi: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <lodelversion> 4.0.0 </modelversion> <poroud> com.exemple </groupidrsion> 4.0.0 </modelversion> <vroupId> com.exemple </groupid> <Versão> 0.0.1-SNAPSHOT </Version> <batyaging> jar </backaging> <name> Demo </name> <cription> Projeto Demo para bota de primavera </cription> <aroger> <puerpid> org.springframework.boot </groupid> <stifactId> spring-boot-starter-parent </artefactId> </groupid> Lookup Parent de Repository-> </axer> <Properts> <Project.build.sourceEncoding> utf-8 </project.build.sourcencoding> <projeto.reporting.outputEncoding> utf-8 </project.reporting.outputoding> <Java.version> 1.8 </project.reporting.outputoding> <Ependency> <PuerpId> org.springframework.boot </frugiD> <ArtifactId> Spring-boot-starter-web </artifactId> </dependency> <pendency> <purgid> org.springframework.kafka </groupid> <stifactId> Spring-kafka </stringfactid <GrupId> org.springframework.boot </proupId> <TROTIFACTID> Spring-boot-starter-test </ArtifactId> <cope> teste </scope> </dependency> </dependências> <bilt> <clugins> <lugin> <rugnid> org.springFramework.Boot </Gruperid> <TarifactId> Spring-boot-maven-plugin </stifactId> </flugin> </plugins> </fruct> </project>
项目中 Application.Properties 添加
spring.kafka.bootstrap-servers = vm208: 9092, vm: 9092, vm50: 9092spring.kafka.consumer.auto-offset-set = mais lastspring.kafka.consumer.group-id = l ocal_testspring.kafka.consumer.key-deserializer = org.apache.kafka.common.serialization.stringdeserieRializerspring.kafka.consumer.value-desei alizer = org.apache.kafka.common.serialization.stringDeserieRializerspring.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 消费类
pacote com.example.demo.consumer; importar org.apache.kafka.clients.consumer.consumerrecord; importar org.slf4j.logger; importar org.slf4j.loggerfactory; importação org.springframework.kafka.annation.kafkalistner; importação; org.springframework.stereotype.component; @componentPublic Classe kafkaconsumer {private logger logger = loggerFactory.getLogger (thisgetclass ()); @Kafkalistener (tópicos = {"test"}) public void listen (consumerCord <?,?> Registro) {System.out.printf ("offset =%d, key =%s, value =%s/n", registro.offset (), registro.key (), registro.value ()); }}启动 Spring-boot 程序, 在 在 kafka 集群, 模拟发送 tópico, 检验接收
复制代码代码如下: bin/kafka-console-producer.sh-Lista de abordagens VM208: 9092, VM210: 9092, VM50: 9092-Teste-tópico
编写 Produtor 代码
pacote com.example.demo.producer; importar org.apache.kafka.clients.producer.producerrecord; importar org.springframework.beans.factory.annoTation.autowired; importação org.springframework.kafka.core.kafkatemplate; importação; classe kafkaproduces {@aUTowired Private Kafkatemplate kafkatemplate; String tópico = "teste"; public void sendMessage (chave de string, dados da string) {kafkatemplate.send (new ProductherRecord (tópico, chave, dados)); }}建立一个 RESTful 模拟发送 (//http://localhost:8080/kafka/send.do?key=2&data=allen-test-message)
pacote com.example.demo.Controller; importar com.example.demo.producer.kafkaproduces; importar org.springframework.bean.factory.annotation.autowired; importação org.springframework.web.bind.annotation.requestMapping; org.springframework.web.bind.annotation.requestmethod; importar org.springframework.web.bind.annotation.requestparam; importar org.springframework.web.bind.annotation.Restcontroller; @ArcrollerTwork.web.bind.annotation.Restcontroller ;RIGRIRTIRLIGRE kafkaproducedor; @RequestMapping (value = "/kafka/send.do", método = requestmethod.get) public string sendMessage (@RequestParam (value = "key") chave de string, @RequestParam (value = "dados") string dados) {kafkaproducer.sendMessage (chave, dados); retornar "sucesso"; }}可以发现 Spring-kafka 大大减少了代码工作量.
官方文档: https://docs.spring.io/spring-kafka/docs/1.2.2.release/reference/html/
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。