以下是一个Spring Boot应用程序集成Apache Kafka的示例,展示了如何发送和消费消息。我们将使用Spring Kafka模块来实现这个示例。
创建Spring Boot项目
首先,确保你已经创建了一个Spring Boot项目,并在项目的 pom.xml
文件中添加了以下依赖:
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<!-- Spring Boot Starter Kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
</dependencies>
配置Kafka
接下来,在项目的 `application.yml 文件中添加Kafka的配置:
spring:
kafka:
bootstrap-servers: 127.0.0.1:9092
consumer:
enable-auto-commit: true
auto-offset-reset: latest
max-poll-records: 1 # 每次只消费一条消息
#group-id: pbt-scheduler
auto-commit-interval: 1000
heartbeat-interval: 3000
session:
timeout:
ms: 20000
# 指定consumer两次poll的最大时间间隔(默认5分钟) ,超过这个时间,则消费者将重新拉取该消息进行处理
properties:
max:
poll:
interval:
ms: 300000
producer:
acks: all
retries: 0
batch-size: 4096
buffer-memory: 40960
linger:
ms: 1
编写生产者代码
创建一个Kafka生产者类,用于发送消息:
package com.example.kafkademo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
@Service
public class KafkaProducer {
private static final String TOPIC = "my_topic";
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void sendMessage(String message) {
kafkaTemplate.send(TOPIC, message);
}
}
编写消费者代码
创建一个Kafka消费者类,用于消费消息:
package com.example.kafkademo;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
public class KafkaConsumer {
@KafkaListener(topics = "my_topic", groupId = "my-group")
public void consume(String message) {
System.out.println("Received message: " + message);
}
}
创建控制器
创建一个控制器来触发消息的发送:
package com.example.kafkademo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class KafkaController {
@Autowired
private KafkaProducer kafkaProducer;
@GetMapping("/send")
public String sendMessage(@RequestParam("message") String message) {
kafkaProducer.sendMessage(message);
return "Message sent to Kafka Topic!";
}
}
Spring Boot 主类
创建Spring Boot应用程序的主类:
package com.example.kafkademo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class KafkaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(KafkaDemoApplication.class, args);
}
}
运行应用程序
启动Spring Boot应用程序,确保Kafka服务器正在运行。然后,使用浏览器或工具(如Postman)访问以下URL以发送消息:
http://localhost:8080/send?message=HelloKafka
验证消费
你应该能够在控制台上看到消费者接收到的消息输出:
Received message: HelloKafka
解释
-
依赖管理:
- 使用Spring Boot Starter Web和Spring Boot Starter Kafka来简化Kafka集成。
-
配置:
- 在
application.yml
中指定Kafka服务器地址和消费者组ID。
- 在
-
生产者服务:
- 使用
KafkaTemplate
来发送消息到指定的主题。
- 使用
-
消费者服务:
- 使用
@KafkaListener
注解来监听指定主题的消息并处理。
- 使用
-
控制器:
- 创建一个简单的REST控制器来触发消息发送操作。
通过这个示例,你可以轻松地使用Spring Boot集成Kafka来实现消息的发送和消费。