文章目录
- 一、 pom.xml依赖包
- 二、yml配置文件
- 三、消费者
- 四、生产者
- 总结
提示:这里可以添加本文要记录的大概内容:
一、 pom.xml依赖包
<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>2.8.0</version>
</dependency>
二、yml配置文件
spring:kafka:listener:concurrency: 3 #线程数ack-mode: manual_immediatetype: batch #批量bootstrap-servers: 192.168.1.214:9092# 生产者配置producer:
# retries: 1 # 消息发送重试次数batch-size: 16384buffer-memory: 33554432value-serializer: org.apache.kafka.common.serialization.StringSerializerkey-serializer: org.apache.kafka.common.serialization.StringSerializer#消费者需配置,生产者不需要consumer:key-deserializer: org.apache.kafka.common.serialization.StringDeserializervalue-deserializer: org.apache.kafka.common.serialization.StringDeserializergroup-id: goodwe-touring-car-groupid-1auto-offset-reset: earliest #latest, earliest, noneenable-auto-commit: falseauto-commit-interval: 5000max-poll-records: 1000 #批量消费最大数量topic: portable_performance#自定义项目run, 运行kafka.
custom:run:kafka: true############################### 参数说明 #########################################consumer:# 自动提交的时间间隔 在spring boot 2.X 版本中这里采用的是值的类型为Duration 需要符合特定的格式,如1S,1M,2H,5Dauto-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.StringDeserializerlistener:# 在侦听器容器中运行的线程数。concurrency: 5#listner负责ack,每调用一次,就立即commitack-mode: manual_immediatemissing-topics-fatal: false
三、消费者
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.goodwe.kafkaapi.model.constant.RedisConst;
import com.goodwe.kafkaapi.model.entity.ConsumerMessageData;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;/*** @Description : kafka消费者** @Author : LiYan* @CreateTime : 2023/8/16 8:35*/
@Slf4j
@Configuration
public class KafkaConsumer {private static final String REDIS_KEY = RedisConst.getREDIS_PREFIX() + RedisConst.getKEY();@Resourceprivate RedisTemplate<String,String> redisTemplate;@KafkaListener(topics = "#{'${spring.kafka.topic}'}", autoStartup = "${custom.run.kafka}")public void receive(List<ConsumerRecord<String, String>> listMessage, Acknowledgment ack) {try {log.info("----------------------开始消费消息--------------------------");if (CollectionUtils.isNotEmpty(listMessage)) {Map<String, ConsumerMessageData> dataMap = listMessage.stream().map(message -> JSON.parseObject(message.value(), ConsumerMessageData.class)).collect(Collectors.toMap(ConsumerMessageData::getSn, data -> data, (oldValue, newValue) -> newValue));dataMap.forEach((key, value) -> {redisTemplate.opsForZSet().add(REDIS_KEY, JSON.toJSONString(value), System.currentTimeMillis());});}} catch (Exception ex) {log.info("【断点续传处理】消费断点续传数据error;", ex);} finally {ack.acknowledge();}}
}
四、生产者
@SpringBootTest
class KafkaApiApplicationTests {@Resourceprivate KafkaTemplate<String, String> kafkaTemplate;@Testpublic void testRedis(){List<ConsumerMessageData> messageData = messageData();for (ConsumerMessageData data : messageData) {String topic = "portable_performance";kafkaTemplate.send(topic, JSON.toJSONString(data));}}
}@RestController
public class KafkaController {@Autowiredprivate KafkaTemplate<String, String> kafkaTemplate;@PostMapping("/send")public void sendMessage(@RequestBody String message) {kafkaTemplate.send("my-topic", message);}}
总结
================== 好记性不如烂笔头=========================