|
|
@ -1,5 +1,7 @@ |
|
|
|
package com.fr.plugin.db.redis.core; |
|
|
|
package com.fr.plugin.db.redis.core; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.fr.config.Configuration; |
|
|
|
|
|
|
|
import com.fr.config.holder.ConfigChangeListener; |
|
|
|
import com.fr.log.FineLoggerFactory; |
|
|
|
import com.fr.log.FineLoggerFactory; |
|
|
|
import com.fr.plugin.db.redis.core.emb.Redis; |
|
|
|
import com.fr.plugin.db.redis.core.emb.Redis; |
|
|
|
import com.fr.plugin.db.redis.core.emb.impl.ProxyRedis; |
|
|
|
import com.fr.plugin.db.redis.core.emb.impl.ProxyRedis; |
|
|
@ -13,6 +15,8 @@ import com.fr.stable.StringUtils; |
|
|
|
import com.fr.third.redis.clients.jedis.Jedis; |
|
|
|
import com.fr.third.redis.clients.jedis.Jedis; |
|
|
|
import com.fr.third.redis.clients.jedis.JedisPool; |
|
|
|
import com.fr.third.redis.clients.jedis.JedisPool; |
|
|
|
import com.fr.third.redis.clients.jedis.JedisPoolConfig; |
|
|
|
import com.fr.third.redis.clients.jedis.JedisPoolConfig; |
|
|
|
|
|
|
|
import com.fr.transaction.Configurations; |
|
|
|
|
|
|
|
import com.fr.transaction.ValidateProxy; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
@ -24,16 +28,32 @@ import java.util.concurrent.ConcurrentHashMap; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class RedisPool { |
|
|
|
public class RedisPool { |
|
|
|
|
|
|
|
|
|
|
|
private static final int TIME_OUT = 100 * 1000; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static RedisPool pool = new RedisPool(); |
|
|
|
private static RedisPool pool = new RedisPool(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
|
|
|
|
ValidateProxy.getInstance().getValidateManager().registerListener(new ConfigChangeListener() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void change() { |
|
|
|
|
|
|
|
pool.clearAll(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean accept(Class<? extends Configuration> clazz) { |
|
|
|
|
|
|
|
return RedisConnectionPoolConfig.class == clazz; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static RedisPool getPool() { |
|
|
|
public static RedisPool getPool() { |
|
|
|
return pool; |
|
|
|
return pool; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Map<String, JedisPool> jedisPoolMap = new ConcurrentHashMap<String, JedisPool>(); |
|
|
|
private Map<String, JedisPool> jedisPoolMap = new ConcurrentHashMap<String, JedisPool>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void clearAll() { |
|
|
|
|
|
|
|
jedisPoolMap.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Redis getResource(String host, int port, String password) { |
|
|
|
public Redis getResource(String host, int port, String password) { |
|
|
|
if (RedisConnectionProxyConfig.getInstance().isOpen()) { |
|
|
|
if (RedisConnectionProxyConfig.getInstance().isOpen()) { |
|
|
|
try { |
|
|
|
try { |
|
|
@ -42,10 +62,11 @@ public class RedisPool { |
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
JedisPool jedisPool = jedisPoolMap.get(host); |
|
|
|
String feature = host + ":" + port + "@" + password; |
|
|
|
|
|
|
|
JedisPool jedisPool = jedisPoolMap.get(feature); |
|
|
|
if (jedisPool == null) { |
|
|
|
if (jedisPool == null) { |
|
|
|
jedisPool = createJedisPool(host, port, password); |
|
|
|
jedisPool = createJedisPool(host, port, password); |
|
|
|
jedisPoolMap.put(host, jedisPool); |
|
|
|
jedisPoolMap.put(feature, jedisPool); |
|
|
|
} |
|
|
|
} |
|
|
|
return new SimpleRedis(jedisPool.getResource()); |
|
|
|
return new SimpleRedis(jedisPool.getResource()); |
|
|
|
} |
|
|
|
} |
|
|
@ -58,8 +79,8 @@ public class RedisPool { |
|
|
|
RedisConnectionPoolConfig poolConfig = RedisConnectionPoolConfig.getInstance(); |
|
|
|
RedisConnectionPoolConfig poolConfig = RedisConnectionPoolConfig.getInstance(); |
|
|
|
JedisPoolConfig config = new JedisPoolConfig(); |
|
|
|
JedisPoolConfig config = new JedisPoolConfig(); |
|
|
|
config.setMaxTotal(poolConfig.getMaxTotal()); |
|
|
|
config.setMaxTotal(poolConfig.getMaxTotal()); |
|
|
|
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
|
|
|
|
//连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true
|
|
|
|
config.setBlockWhenExhausted(true); |
|
|
|
config.setBlockWhenExhausted(poolConfig.getBlockWhenExhausted()); |
|
|
|
//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
|
|
|
|
//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
|
|
|
|
config.setEvictionPolicyClassName("com.fr.third.org.apache.commons.pool2.impl.DefaultEvictionPolicy"); |
|
|
|
config.setEvictionPolicyClassName("com.fr.third.org.apache.commons.pool2.impl.DefaultEvictionPolicy"); |
|
|
|
//是否启用pool的jmx管理功能, 默认true
|
|
|
|
//是否启用pool的jmx管理功能, 默认true
|
|
|
@ -67,9 +88,9 @@ public class RedisPool { |
|
|
|
//MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
|
|
|
|
//MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
|
|
|
|
config.setJmxNamePrefix("pool"); |
|
|
|
config.setJmxNamePrefix("pool"); |
|
|
|
//是否启用后进先出, 默认true
|
|
|
|
//是否启用后进先出, 默认true
|
|
|
|
config.setLifo(true); |
|
|
|
config.setLifo(poolConfig.getLifo()); |
|
|
|
//最大空闲连接数
|
|
|
|
//最大空闲连接数
|
|
|
|
config.setMaxIdle(10); |
|
|
|
config.setMaxIdle(poolConfig.getMaxIdle()); |
|
|
|
//最大连接数,可配置
|
|
|
|
//最大连接数,可配置
|
|
|
|
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
|
|
|
|
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
|
|
|
|
config.setMaxWaitMillis(poolConfig.getMaxWait()); |
|
|
|
config.setMaxWaitMillis(poolConfig.getMaxWait()); |
|
|
@ -88,9 +109,9 @@ public class RedisPool { |
|
|
|
//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
|
|
|
|
//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
|
|
|
|
config.setTimeBetweenEvictionRunsMillis(-1); |
|
|
|
config.setTimeBetweenEvictionRunsMillis(-1); |
|
|
|
if (StringUtils.isNotBlank(password)) { |
|
|
|
if (StringUtils.isNotBlank(password)) { |
|
|
|
return new JedisPool(config, host, port, TIME_OUT, password); |
|
|
|
return new JedisPool(config, host, port, poolConfig.getTimeout(), password); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return new JedisPool(config, host, port, TIME_OUT); |
|
|
|
return new JedisPool(config, host, port, poolConfig.getTimeout()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|