package zs.payment.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; public class Redis { private static final Logger log = LoggerFactory.getLogger(Redis.class); @FunctionalInterface public interface RedisCallback { V execute(Jedis jedis) throws Exception; } /** * 主从模式 * @param ipPort Redis server address * @param callback Redis callback * @return callback result */ public static V with(String ipPort, RedisCallback callback) { JedisPool pool = pool(ipPort); Jedis jedis = null; V val = null; try { for (int i = 1; i <= 2; i++) { if (jedis == null) { if (i > 1) { Thread.sleep(200); } jedis = pool.getResource(); } } if (callback != null) { val = callback.execute(jedis); } } catch (Exception e) { log.error("redis " + ipPort, e); returnResource(pool, jedis); } finally { returnResource(pool, jedis); } return val; } /** * 集群模式(指定数据库) * @param ipPorts Redis cluster addresses * @param db Redis database index * @param callback Redis callback * @return callback result */ public static V withClusterDb(String ipPorts, int db, RedisCallback callback) { String[] ipPortArray = ipPorts.split(","); String ipPort = ""; if (ipPortArray.length > 0) { Random random = new Random(); int index = random.nextInt(ipPortArray.length); ipPort = ipPortArray[index]; } JedisPool pool = poolClusterDb(ipPort, db); Jedis jedis = null; V val = null; try { for (int i = 1; i <= 2; i++) { if (jedis == null) { if (i > 1) { Thread.sleep(200); } jedis = pool.getResource(); } } if (callback != null) { val = callback.execute(jedis); } } catch (Exception e) { log.error("redis " + ipPort + " db" + db, e); returnResource(pool, jedis); } finally { returnResource(pool, jedis); } return val; } /** * 集群模式 * @param ipPorts Redis cluster addresses * @param callback Redis callback * @return callback result */ public static V withCluster(String ipPorts, RedisCallback callback) { String[] ipPortArray = ipPorts.split(","); String ipPort = ""; if (ipPortArray.length > 0) { Random random = new Random(); int index = random.nextInt(ipPortArray.length); ipPort = ipPortArray[index]; } JedisPool pool = poolCluster(ipPort); Jedis jedis = null; V val = null; try { for (int i = 1; i <= 2; i++) { if (jedis == null) { if (i > 1) { Thread.sleep(200); } jedis = pool.getResource(); } } if (callback != null) { val = callback.execute(jedis); } } catch (Exception e) { log.error("redis " + ipPort, e); returnResource(pool, jedis); } finally { returnResource(pool, jedis); } return val; } private static ConcurrentHashMap pools = new ConcurrentHashMap(); private static JedisPoolConfig poolConfig = config(); private static JedisPoolConfig poolConfigCluster = configCluster(); private static JedisPoolConfig config() { JedisPoolConfig cfg = new JedisPoolConfig(); cfg.setMaxTotal(500); cfg.setMinIdle(50); cfg.setMaxIdle(100); cfg.setMaxWaitMillis(5000); cfg.setTestOnBorrow(true); cfg.setTestOnReturn(true); cfg.setTestWhileIdle(true); cfg.setMinEvictableIdleTimeMillis(300000); cfg.setTimeBetweenEvictionRunsMillis(60000); cfg.setNumTestsPerEvictionRun(10); return cfg; } private static JedisPool pool(String ipPort) { JedisPool pool = pools.get(ipPort); if (pool == null) { String host = null; int port = 6379; String[] hp = ipPort.split(":"); if (hp.length > 0) { host = hp[0]; if (hp.length > 1 && Pattern.matches("[0-9]+", hp[1])) { port = Integer.parseInt(hp[1]); } } pool = new JedisPool(poolConfig, host, port); pools.put(ipPort, pool); } return pool; } /** * 集群配置 * @return */ private static JedisPoolConfig configCluster() { JedisPoolConfig cfg = new JedisPoolConfig(); cfg.setMaxTotal(500); cfg.setMinIdle(50); cfg.setMaxIdle(100); cfg.setMaxWaitMillis(5000); cfg.setTestOnBorrow(true); cfg.setTestOnReturn(true); cfg.setTestWhileIdle(true); cfg.setMinEvictableIdleTimeMillis(300000); cfg.setTimeBetweenEvictionRunsMillis(60000); cfg.setNumTestsPerEvictionRun(10); return cfg; } private static JedisPool poolCluster(String ipPort) { JedisPool pool = pools.get(ipPort); if (pool == null) { String host = null; int port = 6379; String[] hp = ipPort.split(":"); if (hp.length > 0) { host = hp[0]; if (hp.length > 1 && Pattern.matches("[0-9]+", hp[1])) { port = Integer.parseInt(hp[1]); } } pool = new JedisPool(poolConfigCluster, host, port); pools.put(ipPort, pool); } return pool; } private static JedisPool poolClusterDb(String ipPort, int db) { String poolKey = ipPort + "#db" + db; JedisPool pool = pools.get(poolKey); if (pool == null) { String host = null; int port = 6379; String[] hp = ipPort.split(":"); if (hp.length > 0) { host = hp[0]; if (hp.length > 1 && Pattern.matches("[0-9]+", hp[1])) { port = Integer.parseInt(hp[1]); } } pool = new JedisPool(poolConfigCluster, host, port, 2000, null, db); pools.put(poolKey, pool); } return pool; } public static void returnResource(JedisPool jedisPool, Jedis jedis) { if (jedisPool != null && jedis != null) { jedisPool.returnResource(jedis); } } }