| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- 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> {
- V execute(Jedis jedis) throws Exception;
- }
- /**
- * 主从模式
- * @param ipPort Redis server address
- * @param callback Redis callback
- * @return callback result
- */
- public static <V> V with(String ipPort, RedisCallback<V> 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> V withClusterDb(String ipPorts, int db, RedisCallback<V> 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> V withCluster(String ipPorts, RedisCallback<V> 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<String, JedisPool> pools = new ConcurrentHashMap<String, JedisPool>();
- 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);
- }
- }
- }
|