赞
踩
- <dependency>
- <groupId>org.springframework.kafka</groupId>
- <artifactId>spring-kafka</artifactId>
- </dependency>
- ## Spring 整合 kafka的服务地址ip列表
- spring.kafka.bootstrap-servers=192.168.31.101:9092
- ## kafka producer 发送消息失败时的一个重试的次数
- spring.kafka.producer.retries=0
- ## 批量发送数据的配置
- spring.kafka.producer.batch-size=16384
- ## 设置kafka 生产者内存缓存区的大小(32M)
- spring.kafka.producer.buffer-memory=33554432
- ## kafka消息的序列化配置
- spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
- spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
-
- # acks=0 : 生产者在成功写入消息之前不会等待任何来自服务器的响应。
- # acks=1 : 只要集群的首领节点收到消息,生产者就会收到一个来自服务器成功响应。
- # acks=-1: 表示分区leader必须等待消息被成功写入到所有的ISR副本(同步副本)中才认为producer请求成功。这种方案提供最高的消息持久性保证,但是理论上吞吐率也是最差的。
-
- ## 这个是kafka生产端最重要的选项
- spring.kafka.producer.acks=1
- @Component
- public class KafkaProducerService {
-
-
- @Autowired
- private KafkaTemplate<String, Object> kafkaTemplate;
-
- public void sendMessage(String topic, Object object) {
-
- ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(topic, object);
-
- future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
- @Override
- public void onSuccess(SendResult<String, Object> result) {
- log.info("发送消息成功: " + result.toString());
- }
-
- @Override
- public void onFailure(Throwable throwable) {
- log.error("发送消息失败: " + throwable.getMessage());
- }
- });
-
- }
- }
- <dependency>
- <groupId>org.springframework.kafka</groupId>
- <artifactId>spring-kafka</artifactId>
- </dependency>
- # kafka服务的ip地址列表
- spring.kafka.bootstrap-servers=192.168.31.101:9092
-
- ## consumer 消息的签收机制:手工签收
- spring.kafka.consumer.enable-auto-commit=false
- spring.kafka.listener.ack-mode=manual
- # 该属性指定了消费者在读取一个没有偏移量的分区或者偏移量无效的情况下该作何处理:
- # latest(默认值)在偏移量无效的情况下,消费者将从最新的记录开始读取数据(在消费者启动之后生成的记录)
- # earliest :在偏移量无效的情况下,消费者将从起始位置读取分区的记录
- spring.kafka.consumer.auto-offset-reset=earliest
- ## 序列化配置
- spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
- spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
-
- spring.kafka.listener.concurrency=5
-
- @Component
- public class KafkaConsumerService {
-
- @KafkaListener(groupId = "group02", topics = "topic02")
- public void onMessage(ConsumerRecord<String, Object> record, Acknowledgment acknowledgment, Consumer<?, ?> consumer) {
- log.info("消费端接收消息: {}", record.value());
- // 收工签收机制
- acknowledgment.acknowledge();
- }
- }
首先在 kafka 节点上创建 topic:
打开kafka节点服务器的终端,输入以下命令:
/usr/local/kafka-3.2.1/bin/kafka-topics.sh --bootstrap-server 192.168.31.101:9092 --create --topic topic02 --partitions 2 --replication-factor 1
一些常用命令:
- # 创建 topic
- ./kafka-topics.sh --bootstrap-server 192.168.31.101:9092 --create --topic topic02 --partitions 1 --replication-factor 1
-
- # 查看 kafka 中topic列表
- ./kafka-topics.sh --bootstrap-server 192.168.31.101:9092 --list
-
- # 查看消费者组group02订阅的topic的消费进度
- ./kafka-consumer-groups.sh --bootstrap-server 192.168.31.101:9092 --describe --group group02
编写测试代码:ApplicationTests.java
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class ApplicationTests {
-
- @Autowired
- private KafkaProducerService kafkaProducerService;
-
- @Test
- public void send() throws InterruptedException {
-
- String topic = "topic02";
- for(int i=0; i < 1000; i ++) {
- kafkaProducerService.sendMessage(topic, "hello kafka" + i);
- Thread.sleep(5);
- }
-
- Thread.sleep(Integer.MAX_VALUE);
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。