Skip to content

ChildrenGreens/multi-source-spring-boot-starter

Repository files navigation

CircleCI codecov Maven Central Apache 2 Codacy code quality

Spring Boot Starter for Multi Data Source (Spring Boot 4)

Production-ready Spring Boot starters that make it easy to configure and consume multiple Redis, RabbitMQ, and InfluxDB 2.x connections in a single application.

Overview

The starters wrap Spring Boot auto-configuration patterns and expose a consistent properties model under the spring.multi-sources.* namespace. Each data source you define results in a dedicated set of Spring beans so you can connect to many back-ends without duplicating configuration code.

Highlights

  • Manage multiple Redis, RabbitMQ, and InfluxDB 2.x connections using pure configuration.

  • Deterministic bean naming (<sourceName><ClassSimpleName>) keeps injection explicit and type-safe.

  • Built on Spring Boot 4.0.x and Java 17 with familiar @ConfigurationProperties support.

Requirements

  • Java 17+

  • Spring Boot 4.0.x (pulled in via the parent BOM)

  • One or more of the supported back-end clients (Lettuce or Jedis for Redis, Spring AMQP, InfluxDB Java client)

Version Compatibility

  • Spring Boot 4.0.x → use starter 3.x

  • Spring Boot 3.5.x → use starter 2.x

  • Spring Boot 2.7.x → use starter 1.x

Modules

Module Maven coordinates Purpose

redis-multi-source-spring-boot-starter

com.childrengreens:redis-multi-source-spring-boot-starter

Configure multiple Redis data sources backed by Lettuce or Jedis.

amqp-multi-source-spring-boot-starter

com.childrengreens:amqp-multi-source-spring-boot-starter

Manage multiple RabbitMQ connections and messaging infrastructure.

influx2-multi-source-spring-boot-starter

com.childrengreens:influx2-multi-source-spring-boot-starter

Connect to multiple InfluxDB 2.x organizations/buckets.

context-multi-source-spring-boot-starter

com.childrengreens:context-multi-source-spring-boot-starter

Internal support module that contributes shared infrastructure (no direct usage required).

Quick Start

  1. Add the starter(s) you need to your project.

  2. Configure spring.multi-sources.<type> properties for each connection, including a mandatory primary-key.

  3. Inject the generated beans using @Qualifier and the <sourceName><ClassSimpleName> naming scheme.

Dependency example

<dependency>
    <groupId>com.childrengreens</groupId>
    <artifactId>redis-multi-source-spring-boot-starter</artifactId>
    <version>${latest-version}</version>
</dependency>

Need Jedis instead of Lettuce? Add the client to your project:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

Configuration Reference

Redis multi data sources

At minimum specify a primary-key and one or more entries under sources. Each source can target standalone, sentinel, or cluster deployments.

# required default source
spring.multi-sources.redis.primary-key=cn

# standalone example
spring.multi-sources.redis.sources.cn.host=127.0.0.1
spring.multi-sources.redis.sources.cn.port=6379
spring.multi-sources.redis.sources.cn.password=changeme

# sentinel example
spring.multi-sources.redis.sources.hk.sentinel.master=hk-redis-master
spring.multi-sources.redis.sources.hk.sentinel.nodes[0]=192.168.1.1:26379
spring.multi-sources.redis.sources.hk.sentinel.nodes[1]=192.168.1.2:26379
spring.multi-sources.redis.sources.hk.database=0
spring.multi-sources.redis.sources.hk.client-type=jedis
spring.multi-sources.redis.sources.hk.password=changeme
spring.multi-sources.redis.sources.hk.lettuce.pool.enabled=true

# cluster example
spring.multi-sources.redis.sources.us.cluster.nodes[0]=127.0.0.1:6379
spring.multi-sources.redis.sources.us.cluster.nodes[1]=127.0.0.1:6380
spring.multi-sources.redis.sources.us.cluster.nodes[2]=127.0.0.1:6381
spring.multi-sources.redis.sources.us.cluster.max-redirects=3
spring.multi-sources.redis.sources.us.password=changeme
spring:
  multi-sources:
    redis:
      primary-key: cn
      sources:
        cn:
          host: 127.0.0.1
          port: 6379
          password: changeme
        hk:
          client-type: jedis
          password: changeme
          sentinel:
            master: hk-redis-master
            nodes:
              - 192.168.1.1:26379
              - 192.168.1.2:26379
          database: 0
          lettuce:
            pool:
              enabled: true
        us:
          password: changeme
          cluster:
            nodes:
              - 127.0.0.1:6379
              - 127.0.0.1:6380
              - 127.0.0.1:6381
            max-redirects: 3

The starter registers LettuceConnectionFactory or JedisConnectionFactory, RedisTemplate, and StringRedisTemplate per source.

RabbitMQ multi data sources

spring.multi-sources.rabbitmq.primary-key=cn

spring.multi-sources.rabbitmq.sources.cn.host=192.168.1.1
spring.multi-sources.rabbitmq.sources.cn.port=5672
spring.multi-sources.rabbitmq.sources.cn.username=guest
spring.multi-sources.rabbitmq.sources.cn.password=guest
spring.multi-sources.rabbitmq.sources.cn.virtual-host=/

spring.multi-sources.rabbitmq.sources.hk.host=192.168.1.2
spring.multi-sources.rabbitmq.sources.hk.port=5672
spring.multi-sources.rabbitmq.sources.hk.username=guest
spring.multi-sources.rabbitmq.sources.hk.password=guest
spring.multi-sources.rabbitmq.sources.hk.virtual-host=/
spring:
  multi-sources:
    rabbitmq:
      primary-key: cn
      sources:
        cn:
          host: 192.168.1.1
          port: 5672
          username: guest
          password: guest
          virtual-host: /
        hk:
          host: 192.168.1.2
          port: 5672
          username: guest
          password: guest
          virtual-host: /

The starter provisions CachingConnectionFactory, RabbitTemplate, RabbitMessagingTemplate, and SimpleRabbitListenerContainerFactory beans per source.

InfluxDB 2.x multi data sources

spring.multi-sources.influx.primary-key=cn

spring.multi-sources.influx.sources.cn.url=http://127.0.0.1:8086/
spring.multi-sources.influx.sources.cn.token=CHANGEME
spring.multi-sources.influx.sources.cn.org=cn-market-data
spring.multi-sources.influx.sources.cn.bucket=cn-data

spring.multi-sources.influx.sources.hk.url=http://127.0.0.2:8086/
spring.multi-sources.influx.sources.hk.token=CHANGEME
spring.multi-sources.influx.sources.hk.org=hk-market-data
spring.multi-sources.influx.sources.hk.bucket=hk-data
spring:
  multi-sources:
    influx:
      primary-key: cn
      sources:
        cn:
          url: http://127.0.0.1:8086/
          token: CHANGEME
          org: cn-market-data
          bucket: cn-data
        hk:
          url: http://127.0.0.2:8086/
          token: CHANGEME
          org: hk-market-data
          bucket: hk-data

Each configured source exposes an InfluxDBClient bean.

Bean naming strategy

Beans follow the <sourceName><ClassSimpleName> pattern. A source named cn produces beans such as cnStringRedisTemplate, cnCachingConnectionFactory, or cnInfluxDBClient. Inject them explicitly with @Qualifier:

@Autowired
@Qualifier("hkRedisTemplate")
private RedisTemplate<String, Object> hkRedisTemplate;

@Autowired
@Qualifier("usLettuceConnectionFactory")
private RedisConnectionFactory usConnectionFactory;

@Autowired
@Qualifier("cnCachingConnectionFactory")
private CachingConnectionFactory cnRabbitConnectionFactory;

@Autowired
@Qualifier("hkInfluxDBClient")
private InfluxDBClient hkInfluxClient;

Validation & builds

Run mvn validate to apply license headers and mvn clean install to build all modules. Individual modules can be built with mvn -pl <module> -am package.

Feedback

If you have questions or improvement ideas, please open an issue to help improve the project.

License

Spring Boot Multi Source Starters are released under the Apache 2.0 license.

About

Spring Boot Starter for Multi Data Source.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages