https://my.oschina.net/u/1445585/blog/1359371
网络上大多都是使用XML配置进行初始化,这里使用Java代码进行初始化。
@Configuration
@PropertySource(value = "classpath:config.properties")
public class RedisClustersConfiguration {
@Value("${redis.pool.maxTotal}")
private int maxTotal;//最大连接数
@Value("${redis.pool.maxIdle}")
private int maxIdle;//最大空闲时间
@Value("${redis.pool.numTestsPerEvictionRun}")
private int numTestsPerEvictionRun;//每次最大连接数
@Value("${redis.pool.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;//释放扫描的扫描间隔
@Value("${redis.pool.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;//连接的最小空闲时间
@Value("${redis.pool.softMinEvictableIdleTimeMillis}")
private int softMinEvictableIdleTimeMillis;//连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
@Value("${redis.pool.maxWaitMillis}")
private int maxWaitMillis;//获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
@Value("${redis.pool.testOnBorrow}")
private boolean testOnBorrow;//在获得链接的时候检查有效性,默认false
@Value("${redis.pool.testWhileIdle}")
private boolean testWhileIdle;//在空闲时检查有效性,默认false
@Value("${redis.pool.blockWhenExhausted}")
private boolean blockWhenExhausted;//false报异常,true阻塞超时 默认:true
@Value("${redis.cluster.connectionTimeout}")
private int connectionTimeout;
@Value("${redis.cluster.soTimeout}")
private int soTimeout;
@Value("${redis.cluster.maxRedirects}")
private int maxRedirects;
@Value("${redis.cluster.nodes}")
private String redisNodes;
@Value("${redis.cluster.password}")
private String password;
/**
*
* @return
*/
@Bean(name = "poolConfig")
@Profile({ "prod", "dev", "test" })
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig bean = new JedisPoolConfig();
bean.setMaxTotal(maxTotal);
bean.setMaxIdle(maxIdle);
bean.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
bean.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
bean.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
bean.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
bean.setMaxWaitMillis(maxWaitMillis);
bean.setTestOnBorrow(testOnBorrow);
bean.setTestWhileIdle(testWhileIdle);
bean.setBlockWhenExhausted(blockWhenExhausted);
return bean;
}
/**
* 基于Java Confing方式定义的JedisCluster
* @param poolConfig
* @return
* @throws Exception
*/
@Bean(name = "jedisCluster")
@Profile({ "prod", "dev", "test" })
public JedisCluster getJedisCluster(JedisPoolConfig poolConfig) throws Exception {
if (StringUtils.isBlank(password)) {
return new JedisCluster(getClusterNodes(), connectionTimeout, soTimeout, maxRedirects,
poolConfig);
}
return new JedisCluster(getClusterNodes(), connectionTimeout, soTimeout, maxRedirects,
password, poolConfig);
}
public Set<HostAndPort> getClusterNodes() {
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
//解析集群配置
String[] clusterNodes = redisNodes.split(";");
for (String clusterNode : clusterNodes) {
String[] node = clusterNode.split(":");
String host = node[0];
int port = Integer.parseInt(node[1]);
jedisClusterNodes.add(new HostAndPort(host, port));
}
return jedisClusterNodes;
}
}
配置文件如下:
redis.pool.maxTotal=30
redis.pool.maxIdle=10
redis.pool.numTestsPerEvictionRun=1024
redis.pool.timeBetweenEvictionRunsMillis=30000
redis.pool.minEvictableIdleTimeMillis=1800000
redis.pool.softMinEvictableIdleTimeMillis=10000
redis.pool.maxWaitMillis=1500
redis.pool.testOnBorrow=true
redis.pool.testWhileIdle=true
redis.pool.blockWhenExhausted=false
redis.cluster.connectionTimeout=60000
redis.cluster.soTimeout=300000
redis.cluster.maxRedirects=8
redis.cluster.nodes=172.23.25.142:6379;172.23.25.142:6380;172.23.25.142:6381;172.23.25.142:6382;172.23.25.143:6379;172.23.25.143:6380;172.23.25.143:6381;172.23.25.143:6382;172.23.25.144:6379;172.23.25.144:6380;172.23.25.144:6381;172.23.25.144:6382
redis.cluster.password=redis
版权声明:本文为djrm11原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。