手撸了一个 SpringBoot Starter 解决了 Kaf

2022-02-24  本文已影响0人  爪哇驿站

前言

日常项目中需要使用两个或以上kafka的数据源地址,面向百度编程的过程中发现,几个数据源就需要配置几个配置文件,并没有优雅的解决方式,于是开发了一款基于Spring-Kafka的kafka-spring-boot-starter除了支持原生配置外,还增加了多数据源配置。

安装

Starter已上传至中央仓库,pom文件中引用即可。

<dependency>
  <groupId>io.github.pg-liudong</groupId>
  <artifactId>kafka-spring-boot-starter</artifactId>
  <version>1.0.5</version>
</dependency>

特征

spring:
  kafka:
    bootstrap-servers: localhost:9092
    consumer:
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      auto-offset-reset: earliest
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
spring:
  kafka:
    multiple:
      # Primary data source is not allowed to be empty.
      primary: ds1  
      consumer:
        key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
        value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
        auto-offset-reset: latest
      producer:
        key-serializer: org.apache.kafka.common.serialization.StringSerializer
        value-serializer: org.apache.kafka.common.serialization.StringSerializer
      datasource:
        ds1:
          bootstrap-servers: localhost:9092
          consumer:
            # Consumer concurrent kafka listener container factory is not allowed to be empty.
            container-factory: ds1KafkaListenerContainerFactory  
            auto-offset-reset: earliest
          producer:
            kafka-template: ds1KafkaTemplate
        ds2:
          bootstrap-servers: localhost:9093
          consumer:
            container-factory: ds2KafkaListenerContainerFactory
            auto-offset-reset: earliest
          producer:
            # Producer kafka template bean name is not allowed to be empty.
            kafka-template: ds2KafkaTemplate

源码

源码以及示例项目,已上传至github,有兴趣的小伙伴可以在微信公众号「爪哇驿站」后台发送“kafka”领取。

上一篇下一篇

猜你喜欢

热点阅读