①引入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>test</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- kafka --> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-streams</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> </dependencies> </project>
②application.yml配置
server: port: 9012 servlet: context-path: / spring: kafka: bootstrap-servers: 192.168.56.104:9092 producer: # 每次批量发送消息的数量 batch-size: 16 # 缓存容量 buffer-memory: 33554432 #设置大于0的值,则客户端会将发送失败的记录重新发送 retries: 0 # 指定消息key和消息体的编解码方式 UTF-8 key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.apache.kafka.common.serialization.StringSerializer kafka: #订阅的主题 topic: topic-test-01 #主题消费分组 group: group-test-01
③消息发布KafkaProducer
package com.test; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; /** * @Description //TODO * @Date 2020/2/26 17:11 * @Author GaoX */ @Component @AllArgsConstructor @Slf4j public class KafkaProducer { private final KafkaTemplate<String, String> kafkaTemplate; public void data(String data){ try { kafkaTemplate.send("topic-test-01", "这是测试的数据==>"+data ); }catch (Exception e){ e.printStackTrace(); log.error("出错!!!!!!!!!!!"); } } }
④消息消费KafkaConsumer
package com.test; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; /** * @Description //TODO * @Date 2020/2/26 17:13 * @Author GaoX */ @Component @AllArgsConstructor @Slf4j public class KafkaConsumer { @KafkaListener(topics = "${kafka.topic}", groupId = "${kafka.group}") public void data(ConsumerRecord consumerRecord) { Object value = consumerRecord.value(); if (log.isInfoEnabled()) { log.info("offset {}, value {}", consumerRecord.offset(), consumerRecord.value()); } if (null == value) { log.error("kafka消费数据为空"); } log.info((String) value); } }
⑤调用消息发布测试
package com.test; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @Description //TODO * @Date 2020/2/26 16:58 * @Author GaoX */ @RestController @AllArgsConstructor public class TestController { private final KafkaProducer kafkaProducer; @GetMapping("/test") public String aVoid(String str){ try { kafkaProducer.data(str); }catch (Exception e){ e.printStackTrace(); } return "成功=============================="; } }
浏览器访问
控制台打印