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、測試:

  

 

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章