Spring Boot 项目
这里使用 INTELDIJ 아이디어
Kafka ka 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.xsd"> <modeversion> 4.0.0 </modelversion> <groupid> com.example> <artifactid> </artifactid> <버전> 0.0.1-snapShot </version> <packaging> jar </packaging> <name> demo </name> <description> 스프링 부츠를위한 데모 프로젝트 </descrip> <perge> org.springframework.boot </groupid> <artifactid> spring-boot-starter-parent </artifactid> <version> 1.8.8. rreleale> </relate>. 저장소에서 부모 조회-> </parent> <properties> <project.build.build.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.versition> 1.8 </java. <pectionency> <groupId> org.springframework.boot </groupid> <artifactid> 스프링-부트-스타터-web </artifactid> </fectionement> <groupid> org.springframework.kafka </groupid> <artifactid> spring-kafka </artifactid> </felevency> <groupid> org.springframework.boot </groupid> <artifactid> 스프링-부트-스타터-테스트 </artifactid> <cope> test </scope> </fectionency> </spections> <플러그인> <grugin> <groupid> org.springframework.boot> <Artifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </build> </project>
项目中 application.properties 添加
spring.kafka.bootstrap-servers = vm208 : 9092, vm : 9092, vm50 : 9092spring.kafka.consumer.auto-offset-reset = hicksspring.kafka.consumer.group-id = l OCAL_TESTSPRING.KAFKA.CONSUMER.KEY-DESERIALIZER = 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 消费类
package com.example.demo.consumer; import org.apache.kafka.clients.consumerrecord; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.spramframework.kafka.annotation.kafkalistener; org.springframework.stereotype.component; @componentpublic class kafkaconsumer {private logger = loggerfactory.getLogger (this.getClass ()); @kafkalistener (주제 = { "test"}) public void Listen (CopbileRecord <?,?> record) {System.out.printf ( "offset =%d, key =%s, value =%s/n", record.offset (), record.key (), record.value (); }}스프링 부트-, 在 kafka 集群, 模拟发送 주제, 检验接收
复制代码代码如下 : bin/kafka-console-producer.sh -Broker-list vm208 : 9092, vm210 : 9092, vm50 : 9092-주제 테스트
编写 생산자 代码
package com.example.demo.producer; import org.apache.kafka.clients.producer.produceRecord; import org.springframework.beans.beans.annotation.autowired; import org.springframework.kafka.core.kafkatemplate; import org.springframework.stereotype.compontor.component; kafkaproducer {@autowired private kafkatemplate kafkatemplate; 문자열 주제 = "테스트"; public void sendmessage (문자열 키, 문자열 데이터) {kafkatemplate.send (new ProduceRrecord (주제, 키, 데이터)); }}RESTFULL 模拟发送 (//http://localhost:8080/kafka/send.do?key=2&data=lallen-testmessage)
package com.example.demo.controller; import com.example.demo.producer.kafkaproducer; import org.springframework.bean.bean.bean.annotation.autowired; import org.springframework.web.bind.bind.bind.bind.springfrffffort.westom.westom.bind.bind org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; @RestControllerPublic ClassErconTroller {@autowired private kafkaproducer kafkaprocerer; @requestmapping (value = "/kafka/send.do", method = requestmethod.get) public String sendMessage (@requestparam (value = "key") String key, @requestparam (value = "data") 문자열 데이터) {kafkaprocter.sendmessage (key, data); "성공"을 반환합니다. }}Spring-kafka 大大减少了代码工作量.
官方文档 : https://docs.spring.io/spring-kafka/docs/1.2.2.release/reference/html/
以上就是本文的全部内容 以上就是本文的全部内容, 希望对大家的学习有所帮助, 也希望大家多多支持武林网。