配置文件
yaml文件
spring:
redis: #默认数据源
expireSeconds: 120
commandTimeout: 10000 #redis操作的超时时间
pool:
maxActive: 5000 #最大连接数
maxIdle: 30 #最大空闲连接数
minIdle: 5 #最小空闲连接数
maxWait: 3000
password: 123456
host: 127.0.0.1
port: 6379
secondary-redis: #第二个数据源
expireSeconds: 120
commandTimeout: 10000 #redis操作的超时时间
pool:
maxActive: 5000 #最大连接数
maxIdle: 30 #最大空闲连接数
minIdle: 5 #最小空闲连接数
maxWait: 3000
password: 123456
host: 127.0.0.1
port: 6379
配置类
redis配置类
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String firstRedisHost;
@Value("${spring.redis.port}")
private Integer firstRedisPort;
@Value("${spring.redis.password:}")
private String firstRedisPassword;
@Value("${spring.redis.pool.maxIdle}")
private Integer firstRedisMaxIdle;
@Value("${spring.redis.pool.maxActive}")
private Integer firstRedisMaxActive;
@Value("${spring.redis.pool.maxWait}")
private Integer firstRedisMaxWait;
@Value("${spring.secondary-redis.host}")
private String secondaryRedisHost;
@Value("${spring.secondary-redis.port}")
private Integer secondaryRedisPort;
@Value("${spring.secondary-redis.password:}")
private String secondaryRedisPassword;
@Value("${spring.secondary-redis.pool.maxIdle}")
private Integer secondaryRedisMaxIdle;
@Value("${spring.secondary-redis.pool.maxActive}")
private Integer secondaryRedisMaxActive;
@Value("${spring.secondary-redis.pool.maxWait}")
private Integer secondaryRedisMaxWait;
//配置工厂
public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle,
int maxTotal, long maxWaitMillis, int index) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(port);
if (!StringUtils.isEmpty(password)) {
jedisConnectionFactory.setPassword(password);
}
if (index != 0) {
jedisConnectionFactory.setDatabase(index);
}
jedisConnectionFactory.setPoolConfig(poolConfig(maxIdle, maxTotal, maxWaitMillis, false));
jedisConnectionFactory.afterPropertiesSet();
return jedisConnectionFactory;
}
//连接池配置
public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxTotal(maxTotal);
poolConfig.setMaxWaitMillis(maxWaitMillis);
poolConfig.setTestOnBorrow(testOnBorrow);
return poolConfig;
}
@Bean(name = "stringRedisTemplate")
public StringRedisTemplate firstRedisTemplate() {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(
connectionFactory(firstRedisHost, firstRedisPort, firstRedisPassword, firstRedisMaxIdle, firstRedisMaxActive, firstRedisMaxWait, 0));
return template;
}
@Bean(name = "secondaryRedisTemplate")
public StringRedisTemplate secondaryRedisTemplate() {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(
connectionFactory(secondaryRedisHost, secondaryRedisPort, secondaryRedisPassword, secondaryRedisMaxIdle, secondaryRedisMaxActive, secondaryRedisMaxWait, 0));
return template;
}
}
使用
// 使用默认的数据源
@Resource(name = "stringRedisTemplate")
private StringRedisTemplate redisTemplate;
// 使用第二个数据源
@Resource(name = "secondaryRedisTemplate")
private StringRedisTemplate secondaryRedis;