This project is a multi-datasource auto-configuration project based on Spring Boot Starter. It simplifies the complex process of creating multiple data sources, requiring only configuration of properties, just like in a typical Spring Boot project.
To use Redis multi-data sources, you need to add the Redis Multi Sources Spring Boot Starter dependency to your project.
<dependency>
<groupId>com.childrengreens</groupId>
<artifactId>redis-multi-source-spring-boot-starter</artifactId>
<version>2.3</version>
</dependency>
To use Jedis instead of Lettuce as the client, you need to add the following dependencies:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
Add the following configuration to your properties file.
# set the primary-key data source (must)
spring.multi-sources.redis.primary-key=cn
# cn data source
spring.multi-sources.redis.source.cn.host=127.0.0.1
spring.multi-sources.redis.source.cn.port=6379
spring.multi-sources.redis.source.cn.password=Asd12345!.
# hk data source
spring.multi-sources.redis.sources.hk.sentinel.master=hk-redis-master
spring.multi-sources.redis.sources.hk.database=0
# jedis client
spring.multi-sources.redis.sources.hk.client-type=jedis
spring.multi-sources.redis.sources.hk.password=Asd12345!.
spring.multi-sources.redis.sources.hk.lettuce.pool.enabled=true
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.sentinel.nodes[2]=192.168.1.2:26379
# us data source
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=Asd12345!.
spring:
multi-sources:
redis:
# set the primary-key data source (must)
primary-key: cn
# cn data source
source:
cn:
host: 127.0.0.1
port: 6379
password: Asd12345!.
# hk data source
sources:
hk:
sentinel:
master: hk-redis-master
# Sentinel nodes
nodes:
- 192.168.1.1:26379
- 192.168.1.2:26379
- 192.168.1.2:26379
database: 0
# jedis client
client-type: jedis
password: Asd12345!.
lettuce:
pool:
enabled: true
# us data source
us:
cluster:
# Cluster nodes
nodes:
- 127.0.0.1:6379
- 127.0.0.1:6380
- 127.0.0.1:6381
max-redirects: 3
password: Asd12345!.
Redis Multi Sources Spring Boot Starter will create multiple sets of LettuceConnectionFactory
or JedisConnectionFactory
, RedisTemplate
, and StringRedisTemplate
beans based on the configuration and register them in the Spring container.
The naming convention for the bean names is: sourceName
+ className
. Therefore, the usage is as follows:
@Autowired
@Qualifier("cnStringRedisTemplate")
private StringRedisTemplate stringRedisTemplate;
@Autowired
@Qualifier("hkRedisTemplate")
private RedisTemplate<String, Object> redisTemplate;
@Autowired
@Qualifier("usLettuceConnectionFactory")
private RedisConnectionFactory redisConnectionFactory;
@Autowired
@Qualifier("hkJedisConnectionFactory")
private RedisConnectionFactory redisConnectionFactory;
To use RabbitMQ multi-data sources, you need to add the AMQP Multi Sources Spring Boot Starter dependency to your project.
<dependency>
<groupId>com.childrengreens</groupId>
<artifactId>amqp-multi-source-spring-boot-starter</artifactId>
<version>2.3</version>
</dependency>
Add the following configuration to your properties file.
# set the primary-key data source (must)
spring.multi-sources.rabbitmq.primary-key=cn
# cn data source
spring.multi-sources.rabbitmq.sources.cn.port=5672
spring.multi-sources.rabbitmq.sources.cn.host=192.168.1.1
spring.multi-sources.rabbitmq.sources.cn.username=guest
spring.multi-sources.rabbitmq.sources.cn.password=guest
spring.multi-sources.rabbitmq.sources.cn.virtual-host=/
# hk data source
spring.multi-sources.rabbitmq.sources.hk.port=5672
spring.multi-sources.rabbitmq.sources.hk.host=192.168.1.2
spring.multi-sources.rabbitmq.sources.hk.username=guest
spring.multi-sources.rabbitmq.sources.hk.password=guest
spring.multi-sources.rabbitmq.sources.hk.virtual-host=/
# set the primary-key data source (must)
spring:
multi-sources:
rabbitmq:
primary-key: cn
# cn data source
sources:
cn:
port: 5672
host: 192.168.1.1
username: guest
password: guest
virtual-host: /
# hk data source
hk:
port: 5672
host: 192.168.1.2
username: guest
password: guest
virtual-host: /
AMQP Multi Sources Spring Boot Starter will create multiple sets of CachingConnectionFactory
, SimpleRabbitListenerContainerFactory
, RabbitTemplate
, and RabbitMessagingTemplate
beans based on the configuration and register them in the Spring container.
The naming convention for the bean names is: sourceName
+ className
. Therefore, the usage is as follows:
@Autowired
@Qualifier("cnCachingConnectionFactory")
private CachingConnectionFactory cachingConnectionFactory;
@Autowired
@Qualifier("hkRabbitTemplate")
private RabbitTemplate rabbitTemplate;
@Autowired
@Qualifier("hkSimpleRabbitListenerContainerFactory")
private SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory;
To use InfluxDB2 multi-data sources, you need to add the Influx2 Multi Sources Spring Boot Starter dependency to your project.
<dependency>
<groupId>com.childrengreens</groupId>
<artifactId>influx2-multi-source-spring-boot-starter</artifactId>
<version>2.3</version>
</dependency>
Add the following configuration to your properties file.
# set the primary-key data source (must)
spring.multi-sources.influx.primary-key=cn
# cn data source
spring.multi-sources.influx.sources.cn.url=http://127.0.0.1:8086/
spring.multi-sources.influx.sources.cn.token=wkOEKEVJ4jJBUhTwzKLsFVTpqR_JsGR3w6ybS3xTDy67WSaY14W4HQf59r69pX0YDaboNDRo_SrMa_T3clNMPA==
spring.multi-sources.influx.sources.cn.org=cn-market-data
spring.multi-sources.influx.sources.cn.bucket=cn-data
# hk data source
spring.multi-sources.influx.sources.hk.url=http://127.0.0.2:8086/
spring.multi-sources.influx.sources.hk.token=wkOEKEVJ4jJBUhTwzKLsFVTpqR_JsGR3w6ybS3xTDy67WSaY14W4HQf59r69pX0YDaboNDRo_FkMa_T3clNMPA==
spring.multi-sources.influx.sources.hk.org=hk-market-data
spring.multi-sources.influx.sources.hk.bucket=hk-data
# set the primary-key data source (must)
spring:
multi-sources:
influx:
primary-key: cn
# cn data source
sources:
cn:
url: http://127.0.0.1:8086/
token: wkOEKEVJ4jJBUhTwzKLsFVTpqR_JsGR3w6ybS3xTDy67WSaY14W4HQf59r69pX0YDaboNDRo_SrMa_T3clNMPA==
org: cn-market-data
bucket: cn-data
# hk data source
hk:
url: http://127.0.0.2:8086/
token: wkOEKEVJ4jJBUhTwzKLsFVTpqR_JsGR3w6ybS3xTDy67WSaY14W4HQf59r69pX0YDaboNDRo_FkMa_T3clNMPA==
org: hk-market-data
bucket: hk-data
Influx2 Multi Sources Spring Boot Starter will create multiple InfluxDBClient
bean based on the configuration and register them in the Spring container.
The naming convention for the bean names is: sourceName
+ className
. Therefore, the usage is as follows:
@Autowired
@Qualifier("cnInfluxDBClient")
private InfluxDBClient influxDBClient;
Spring Boot is Open Source software released under the Apache 2.0 license.