zoukankan      html  css  js  c++  java
  • kafka与springboot集成2

      一、加入依赖(springboot的其他依赖这里展示了,版本使用的是2.0.4.RELEASE)

    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
    </dependency>

      二、加入配置

    spring:
      kafka:
        # 地址
        bootstrap-servers: 192.168.5.100:9092,192.168.5.100:9093
        producer:
          # 0 发送出去即成功 1 发送出去等待leader落盘后,认为成功 -1 认为在ISR集群中leader和follower同步完成为成功
          acks: 1
          # 重试次数
          retries: 3
          # kafka一次拉取的缓存数据量默认16KB
          batch-size: 16384
          # 本地缓存大小默认32M
          buffer-memory: 33554432
          # 序列化
          key-serializer: org.apache.kafka.common.serialization.BytesSerializer
          value-serializer: org.apache.kafka.common.serialization.BytesSerializer
        consumer:
          # 默认组
          group-id: kafka
          # 自动提交一般关闭
          enable-auto-commit: false
          # 一次性拉取数量
          max-poll-records: 500
          # 反序列化
          key-deserializer: org.apache.kafka.common.serialization.BytesDeserializer
          value-deserializer: org.apache.kafka.common.serialization.BytesDeserializer
        listener:
          # 确认模式
          ack-mode: manual_immediate

      三、编码

      1、topic配置

    @Configuration
    public class TopicConfiguration {
    
        @Bean
        public NewTopic log() {
            return new NewTopic("log", 4, (short) 1);
        }
    
        @Bean
        public NewTopic data() {
            return new NewTopic("data", 4, (short) 1);
        }
    }

      2、生产者

    @Component
    public class Producer {
    
        @Autowired
        private KafkaTemplate<Bytes, Bytes> kafkaTemplate;
    
        private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
        @Scheduled(cron = "0 0/1 * * * ?")
        public void send() {
            String now = formatter.format(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
            kafkaTemplate.send("log", Bytes.wrap("log".getBytes()), Bytes.wrap(now.getBytes()));
            kafkaTemplate.send("data", Bytes.wrap("data".getBytes()), Bytes.wrap(now.getBytes()));
        }
    }

      3、消费者

    @Component
    public class Consumer {
    
        @KafkaListener(topics = "log")
        public void log(ConsumerRecord<Bytes, Bytes> record, Acknowledgment ack) {
            System.out.println(MessageFormat.format("log key {0}, value {1}", record.key(), record.value()));
            ack.acknowledge();
        }
    
        @KafkaListener(topics = "data")
        public void data(ConsumerRecord<Bytes, Bytes> record, Acknowledgment ack) {
            System.out.println(MessageFormat.format("data key {0}, value {1}", record.key(), record.value()));
            ack.acknowledge();
        }
    }

      4、测试:

      

       

  • 相关阅读:
    【读书笔记】构建之法(CH7~CH8)
    【课后作业】软件创新
    【个人开发】词频统计
    【读书笔记】没有银弹
    【个人开发】词频统计-代码规范
    【个人开发】词频统计-文档设计
    GitBook 使用
    Android NDK 入门与实践
    Python 爬虫实战(一):使用 requests 和 BeautifulSoup
    手把手教你做个人 app
  • 原文地址:https://www.cnblogs.com/ll409546297/p/15793783.html
Copyright © 2011-2022 走看看