在SpringBoot官方对kafka的依赖没有使用starter,而是spring-kafka,如下:
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>kafka</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<!-- spring boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot 单元测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
application.yml内容
spring:
kafka:
bootstrap-servers: 192.168.126.156:9092
consumer:
group-id: kafka-demo
# key/value的反序列化
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
producer:
# key/value的序列化
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Kafka 整合
* @author terry
* @version 1.0
* @date 2022/6/7 23:36
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
测试类
import com.terry.App;
import lombok.extern.java.Log;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
@SpringBootTest(classes = App.class)
@Log
public class TestKafka {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
@KafkaListener(topics = "terry-topic")
public void receive(ConsumerRecord record) {
String message = (String) record.value();
log.info("收到消息: " + message);
}
@Test
public void test() throws InterruptedException {
// 模拟一秒钟发送一次消息
while (true) {
kafkaTemplate.send("terry-topic", "hello kafka!");
Thread.sleep(1000);
}
}
}
打印如下:
2022-06-08 00:27:08.267 INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka : 收到消息: hello kafka!
2022-06-08 00:27:09.277 INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka : 收到消息: hello kafka!
2022-06-08 00:27:10.289 INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka : 收到消息: hello kafka!
2022-06-08 00:27:11.288 INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka : 收到消息: hello kafka!
2022-06-08 00:27:12.302 INFO 17352 --- [ntainer#0-0-C-1] com.terry.test.TestKafka : 收到消息: hello kafka!