springboot redis多数据源

springboot中默认的redis配置是只能对单个redis库进行操作的。

那么我们需要多个库操作的时候这个时候就可以采用redis多数据源。

本代码参考RedisAutoConfiguration源码进行整改成的多数据源配置

注意:数据源2就是数据源1copy了一份代码,把所有的gisredis改成appredis,并去除数据源2的@Primary即可。如果还需要增加多个数据源,以此类推

配置redis数据源1:

 package com.aaaa.config.redis;

 import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import redis.clients.jedis.JedisPoolConfig; /**
*
* @desc Redis配置
* @author josnow
* @date 2018年1月17日 上午10:51:36
* @version 1.0.0
*/
@Configuration
public class RedisConfiggisredis { @Bean(name = "gisredisRedisProperties")
@ConfigurationProperties(prefix = "spring.redis.gisredis")
@Primary
public RedisProperties gisredisRedisProperties() {
RedisProperties redisProperties = new RedisProperties();
return redisProperties;
} // //@Bean("gisredisRedisTemplate")
// public RedisTemplate<Object, Object> redisTemplate(@Qualifier("gisredisRedisConnectionFactory")RedisConnectionFactory
// redisConnectionFactory)
// throws UnknownHostException {
// RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
// template.setConnectionFactory(redisConnectionFactory);
// return template;
// } @Bean("gisredisStringRedisTemplate")
@Primary
public StringRedisTemplate stringRedisTemplate(
@Qualifier("gisredisRedisConnectionFactory")RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
} @Bean("gisredisRedisConnectionFactory")
@Primary
public RedisConnectionFactory redisConnectionFactory(
@Qualifier("gisredisRedisProperties") RedisProperties redisProperties) throws UnknownHostException { return setRedisConnectionFactory(redisProperties, null, null);
} private JedisConnectionFactory setRedisConnectionFactory(RedisProperties redisProperties,
RedisSentinelConfiguration sentinelConfiguration, RedisClusterConfiguration clusterConfiguration)
throws UnknownHostException {
return applyProperties(
createJedisConnectionFactory(redisProperties, sentinelConfiguration, clusterConfiguration),
redisProperties);
} private final JedisConnectionFactory applyProperties(JedisConnectionFactory factory,
RedisProperties redisProperties) {
factory.setHostName(redisProperties.getHost());
factory.setPort(redisProperties.getPort());
if (redisProperties.getPassword() != null) {
factory.setPassword(redisProperties.getPassword());
}
factory.setDatabase(redisProperties.getDatabase());
if (redisProperties.getTimeout() > 0) {
factory.setTimeout(redisProperties.getTimeout());
}
return factory;
} private final RedisSentinelConfiguration getSentinelConfig(RedisProperties redisProperties,
RedisSentinelConfiguration sentinelConfiguration) {
if (sentinelConfiguration != null) {
return sentinelConfiguration;
}
Sentinel sentinelProperties = redisProperties.getSentinel();
if (sentinelProperties != null) {
RedisSentinelConfiguration config = new RedisSentinelConfiguration();
config.master(sentinelProperties.getMaster());
config.setSentinels(createSentinels(sentinelProperties));
return config;
}
return null;
} /**
* Create a {@link RedisClusterConfiguration} if necessary.
*
* @return {@literal null} if no cluster settings are set.
*/
private final RedisClusterConfiguration getClusterConfiguration(RedisProperties redisProperties,
RedisClusterConfiguration clusterConfiguration) {
if (clusterConfiguration != null) {
return clusterConfiguration;
}
if (redisProperties.getCluster() == null) {
return null;
}
Cluster clusterProperties = redisProperties.getCluster();
RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes()); if (clusterProperties.getMaxRedirects() != null) {
config.setMaxRedirects(clusterProperties.getMaxRedirects());
}
return config;
} private List<RedisNode> createSentinels(Sentinel sentinel) {
List<RedisNode> nodes = new ArrayList<RedisNode>();
for (String node : StringUtils.commaDelimitedListToStringArray(sentinel.getNodes())) {
try {
String[] parts = StringUtils.split(node, ":");
Assert.state(parts.length == 2, "Must be defined as 'host:port'");
nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));
} catch (RuntimeException ex) {
throw new IllegalStateException("Invalid redis sentinel " + "property '" + node + "'", ex);
}
}
return nodes;
} private JedisConnectionFactory createJedisConnectionFactory(RedisProperties redisProperties,
RedisSentinelConfiguration sentinelConfiguration, RedisClusterConfiguration clusterConfiguration) {
JedisPoolConfig poolConfig = redisProperties.getPool() != null ? jedisPoolConfig(redisProperties)
: new JedisPoolConfig(); if (getSentinelConfig(redisProperties, sentinelConfiguration) != null) {
return new JedisConnectionFactory(getSentinelConfig(redisProperties, sentinelConfiguration), poolConfig);
}
if (getClusterConfiguration(redisProperties, clusterConfiguration) != null) {
return new JedisConnectionFactory(getClusterConfiguration(redisProperties, clusterConfiguration),
poolConfig);
}
return new JedisConnectionFactory(poolConfig);
} private JedisPoolConfig jedisPoolConfig(RedisProperties redisProperties) {
JedisPoolConfig config = new JedisPoolConfig();
RedisProperties.Pool props = redisProperties.getPool();
config.setMaxTotal(props.getMaxActive());
config.setMaxIdle(props.getMaxIdle());
config.setMinIdle(props.getMinIdle());
config.setMaxWaitMillis(props.getMaxWait());
return config;
} }

配置redis数据源2:

 package com.aaaa.config.redis;

 import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import redis.clients.jedis.JedisPoolConfig; /**
*
* @desc Redis配置
* @author josnow
* @date 2018年1月17日 上午10:51:36
* @version 1.0.0
*/
@Configuration
public class RedisConfigappredis { @Bean(name = "appredisRedisProperties")
@ConfigurationProperties(prefix = "spring.redis.appredis")
public RedisProperties appredisRedisProperties() {
RedisProperties redisProperties = new RedisProperties();
return redisProperties;
} // //@Bean("appredisRedisTemplate")
// public RedisTemplate<Object, Object> redisTemplate(@Qualifier("appredisRedisConnectionFactory")RedisConnectionFactory
// redisConnectionFactory)
// throws UnknownHostException {
// RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
// template.setConnectionFactory(redisConnectionFactory);
// return template;
// } @Bean("appredisStringRedisTemplate")
public StringRedisTemplate stringRedisTemplate(
@Qualifier("appredisRedisConnectionFactory") RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
} @Bean("appredisRedisConnectionFactory")
public RedisConnectionFactory redisConnectionFactory(
@Qualifier("appredisRedisProperties") RedisProperties redisProperties) throws UnknownHostException { return setRedisConnectionFactory(redisProperties, null, null);
} private JedisConnectionFactory setRedisConnectionFactory(RedisProperties redisProperties,
RedisSentinelConfiguration sentinelConfiguration, RedisClusterConfiguration clusterConfiguration)
throws UnknownHostException {
return applyProperties(
createJedisConnectionFactory(redisProperties, sentinelConfiguration, clusterConfiguration),
redisProperties);
} private final JedisConnectionFactory applyProperties(JedisConnectionFactory factory,
RedisProperties redisProperties) {
factory.setHostName(redisProperties.getHost());
factory.setPort(redisProperties.getPort());
if (redisProperties.getPassword() != null) {
factory.setPassword(redisProperties.getPassword());
}
factory.setDatabase(redisProperties.getDatabase());
if (redisProperties.getTimeout() > 0) {
factory.setTimeout(redisProperties.getTimeout());
}
return factory;
} private final RedisSentinelConfiguration getSentinelConfig(RedisProperties redisProperties,
RedisSentinelConfiguration sentinelConfiguration) {
if (sentinelConfiguration != null) {
return sentinelConfiguration;
}
Sentinel sentinelProperties = redisProperties.getSentinel();
if (sentinelProperties != null) {
RedisSentinelConfiguration config = new RedisSentinelConfiguration();
config.master(sentinelProperties.getMaster());
config.setSentinels(createSentinels(sentinelProperties));
return config;
}
return null;
} /**
* Create a {@link RedisClusterConfiguration} if necessary.
*
* @return {@literal null} if no cluster settings are set.
*/
private final RedisClusterConfiguration getClusterConfiguration(RedisProperties redisProperties,
RedisClusterConfiguration clusterConfiguration) {
if (clusterConfiguration != null) {
return clusterConfiguration;
}
if (redisProperties.getCluster() == null) {
return null;
}
Cluster clusterProperties = redisProperties.getCluster();
RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes()); if (clusterProperties.getMaxRedirects() != null) {
config.setMaxRedirects(clusterProperties.getMaxRedirects());
}
return config;
} private List<RedisNode> createSentinels(Sentinel sentinel) {
List<RedisNode> nodes = new ArrayList<RedisNode>();
for (String node : StringUtils.commaDelimitedListToStringArray(sentinel.getNodes())) {
try {
String[] parts = StringUtils.split(node, ":");
Assert.state(parts.length == 2, "Must be defined as 'host:port'");
nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));
} catch (RuntimeException ex) {
throw new IllegalStateException("Invalid redis sentinel " + "property '" + node + "'", ex);
}
}
return nodes;
} private JedisConnectionFactory createJedisConnectionFactory(RedisProperties redisProperties,
RedisSentinelConfiguration sentinelConfiguration, RedisClusterConfiguration clusterConfiguration) {
JedisPoolConfig poolConfig = redisProperties.getPool() != null ? jedisPoolConfig(redisProperties)
: new JedisPoolConfig(); if (getSentinelConfig(redisProperties, sentinelConfiguration) != null) {
return new JedisConnectionFactory(getSentinelConfig(redisProperties, sentinelConfiguration), poolConfig);
}
if (getClusterConfiguration(redisProperties, clusterConfiguration) != null) {
return new JedisConnectionFactory(getClusterConfiguration(redisProperties, clusterConfiguration),
poolConfig);
}
return new JedisConnectionFactory(poolConfig);
} private JedisPoolConfig jedisPoolConfig(RedisProperties redisProperties) {
JedisPoolConfig config = new JedisPoolConfig();
RedisProperties.Pool props = redisProperties.getPool();
config.setMaxTotal(props.getMaxActive());
config.setMaxIdle(props.getMaxIdle());
config.setMinIdle(props.getMinIdle());
config.setMaxWaitMillis(props.getMaxWait());
return config;
} }

redis数据源1和数据源2的配置yml

  
   spring:
   redis:
gisredis:
host: 10.142.142.142
port: 6379
database: 6
testOnBorrow: true
pool:
max-active: 10000
max-idle: 1000
max-wait: 60000
min-idle: 0
appredis:
host: 10.142.141.84
port: 6379
database: 5
testOnBorrow: true
pool:
max-active: 10000
max-idle: 1000
max-wait: 60000
min-idle: 0

配置就这么多,要注意的是多个redis数据源必须指定一个默认的数据源。使用@Primary来指定,并且只能指定一个默认redis数据源。

注意:数据源2就是数据源1copy了一份代码,把所有的gisredis改成appredis,并去除数据源2的@Primary即可。如果还需要增加多个数据源,以此类推

最后在使用的时候

 @Autowired
@Qualifier("appredisStringRedisTemplate")
private StringRedisTemplate appredisStringRedisTemplate;

即可得到指定的redis源,使用起来也是很方便

上一篇:剑指offer(一):二维数组中的查找


下一篇:关于Delphi中二维数组的声明和大小调整