部署本地kafka服务
本次demo使用的是本地的kafka服务,首先在官网下载部署包,然后解压。
kafka在2.8的版本中,已经取消了对zookeeper的依赖,新的模式为KRaft,本文为了方便,仍然使用内置的zookeeper。
解压后直接运行zookeeper和kafka启动脚本。
下文附上快捷启动的bat脚本。把路径换成自己的路径即可。
title zk
C:\work\tools\kafka_2.13-2.8.0\bin\windows\zookeeper-server-start.bat C:\work\tools\kafka_2.13-2.8.0\config\zookeeper.properties
title kafka
C:\work\tools\kafka_2.13-2.8.0\bin\windows\kafka-server-start.bat C:\work\tools\kafka_2.13-2.8.0\config\server.properties
添加kafka依赖
项目的pom.xml文件添加对应的依赖
<!-- 添加kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
添加配置文件
如果想要修改kafka的端口,除了kafka的配置文件需要修改外,项目的bootstrap-servers属性也需要修改。由于这里是使用本地,因此ip地址填127.0.0.1,不建议填localhost,具体可自己尝试。
consumer的group-id属性需要配置,否则会报错。
spring:
kafka:
bootstrap-servers: 127.0.0.1:9092
producer:
# 发生错误后,消息重发的次数。
retries: 0
#当有多个消息需要被发送到同一个分区时,生产者会把它们放在同一个批次里。该参数指定了一个批次可以使用的内存大小,按照字节数计算。
batch-size: 16384
# 设置生产者内存缓冲区的大小。
buffer-memory: 33554432
# 键的序列化方式
key-serializer: org.apache.kafka.common.serialization.StringSerializer
# 值的序列化方式
value-serializer: org.apache.kafka.common.serialization.StringSerializer
# acks=0 : 生产者在成功写入消息之前不会等待任何来自服务器的响应。
# acks=1 : 只要集群的首领节点收到消息,生产者就会收到一个来自服务器成功响应。
# acks=all :只有当所有参与复制的节点全部收到消息时,生产者才会收到一个来自服务器的成功响应。
acks: 1
consumer:
group-id: dnf-group
# 自动提交的时间间隔 在spring boot 2.X 版本中这里采用的是值的类型为Duration 需要符合特定的格式,如1S,1M,2H,5D
auto-commit-interval: 1S
# 该属性指定了消费者在读取一个没有偏移量的分区或者偏移量无效的情况下该作何处理:
# latest(默认值)在偏移量无效的情况下,消费者将从最新的记录开始读取数据(在消费者启动之后生成的记录)
# earliest :在偏移量无效的情况下,消费者将从起始位置读取分区的记录
auto-offset-reset: earliest
# 是否自动提交偏移量,默认值是true,为了避免出现重复数据和数据丢失,可以把它设置为false,然后手动提交偏移量
enable-auto-commit: false
# 键的反序列化方式
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
# 值的反序列化方式
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
listener:
# 在侦听器容器中运行的线程数。
concurrency: 5
#listner负责ack,每调用一次,就立即commit
ack-mode: manual_immediate
missing-topics-fatal: false
添加生产者和消费者
生产者
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
@Component
public class KafkaProducer {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public static final String TEST_TOPIC = "test";
public static final String DNF_TOPIC = "dnf";
public void send(String topic, String message) {
System.err.println("Kafka send message: -" + topic + "-" + message);
kafkaTemplate.send(topic, message);
}
}
消费者
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class KafkaConsumer {
@KafkaListener(topics = KafkaProducer.TEST_TOPIC)
public void topic_test(ConsumerRecord<?, ?> record) {
log.info("Kafka receive message: {}-{}", record.topic(), record.value());
System.err.println("Kafka receive message: " + record.topic() + "-" + record.partition() + "-" + record.value());
}
@KafkaListener(topics = KafkaProducer.DNF_TOPIC)
public void topic_dnf(ConsumerRecord<?, ?> record) {
System.err.println("Kafka receive message: " + record.topic() + "-" + record.partition() + "-" + record.value());
}
}
添加测试入口
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("/kafkaProduce")
public void hello(@RequestParam(value = "message", defaultValue = "World") String message,
@RequestParam(value = "topic", defaultValue = "test") String topic) {
kafkaProducer.send(topic, message);
}
}
结果
浏览器访问 http://localhost:8080/kafkaProduce