kafka生产者与消费者

文章目录

  • 一、 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_immediate
      type: batch #批量
    bootstrap-servers: 192.168.1.214:9092
    # 生产者配置
    producer:
#      retries: 1 # 消息发送重试次数
      batch-size: 16384
      buffer-memory: 33554432
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
      key-serializer: org.apache.kafka.common.serialization.StringSerializer

    #消费者需配置,生产者不需要
    consumer:
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      group-id: goodwe-touring-car-groupid-1
      auto-offset-reset: earliest #latest, earliest, none
      enable-auto-commit: false
      auto-commit-interval: 5000
      max-poll-records: 1000        #批量消费最大数量

    topic: portable_performance

#自定义项目run, 运行kafka.
custom:
  run:
    kafka: true
    
    
    
############################### 参数说明 #########################################
    consumer:
      # 自动提交的时间间隔 在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 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();

    @Resource
    private 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 {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @Test
    public void testRedis(){
        List<ConsumerMessageData> messageData = messageData();
        for (ConsumerMessageData data : messageData) {
            String topic = "portable_performance";
            kafkaTemplate.send(topic, JSON.toJSONString(data));
        }
    }
}


@RestController
public class KafkaController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @PostMapping("/send")
    public void sendMessage(@RequestBody String message) {
        kafkaTemplate.send("my-topic", message);
    }

}

总结

================== 好记性不如烂笔头=========================

你可能感兴趣的:(kafka,linq,分布式)