redis.clients jedis 2.8.0
jedis版本 < 3.0.0以前的时候:回收资源的方式:
JedisPool jedisPool = null; Jedis jedis = null; jedis = redisPool.getResource(); // 此处业务处理 if (jedis != null) { redisPool.returnBrokenResource(redis); jedis = null; }
jedis版本 >= 3.0.0以后的时候:回收资源的方式:
Jedis jedis = null;
if (jedis != null) {
jedis.close();
jedis = null;
}
(1): API访问 Redis Sentinel 集群(带密码/不带密码)
(2): API访问 Redis master-slave 集群(带密码/不带密码)
(3): API访问 Redis 单机 (带密码/不带密码)
import org.apache.commons.lang.StringUtils; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.apache.log4j.Logger; import redis.clients.jedis.*; import java.util.*; /** * @author jerry * @version 1.0 * @time 2015-04-30 * 操作redis (哨兵集群,主从集群,单机,包含是否有密码)的 工具类 */ public class RedisUtil { private static Logger log = Logger.getLogger(RedisUtil.class); private static JedisPool jedisPool; private static JedisSentinelPool jediSentinelPool; private static JedisCluster jedisCluster; private static Jedis jedis; private static GenericObjectPoolConfig config; /** * 获取 redis 哨兵集群的 redis client 进行读写 * * @param redis_clusterName * @param redis_hostName * @param redis_port * @param redis_passWord * @param redis_database * @return */ public static Jedis getJedisByJedisSentinelPool(String redis_clusterName, String redis_hostName, String redis_port, String redis_passWord, String redis_database) { Set
sentinels = new HashSet<>(); if (null != redis_hostName && !"".equals(StringUtils.trim(redis_hostName))) { String[] hosts = StringUtils.trim(redis_hostName).split(","); String[] ports = StringUtils.trim(redis_port).split(","); for (int i = 0; i < hosts.length; i++) { if (ports.length == 1) { sentinels.add(hosts[i] + ":" + ports[0]); } else { sentinels.add(hosts[i] + ":" + ports[i]); } } } if (null != redis_passWord && !"".equals(redis_passWord.trim())) { //(final GenericObjectPoolConfig poolConfig, final String host, int port,int timeout, final String password) //single machine //jedisPool = new JedisPool(config,redis_hostName,redis_port,30000,redis_passWord,Integer.parseInt(redis_database)); log.info(redis_clusterName + " " + sentinels + " " + config + " " + redis_passWord + " " + redis_database); //cluster with sentinel jediSentinelPool = new JedisSentinelPool(redis_clusterName, sentinels, config, 30000, redis_passWord, Integer.parseInt(redis_database)); //get the redis client to read and write data jedis = jediSentinelPool.getResource(); } else { //single machine //jedisPool = new JedisPool(config,redis_hostName,redis_port); //cluster with sentinel jediSentinelPool = new JedisSentinelPool(redis_clusterName, sentinels, config); //get the redis client to read and write data jedis = jediSentinelPool.getResource(); //specify the redis database jedis.select(Integer.parseInt(redis_database)); } return jedis; } /** * 获取redis redis master-slave 集群(无哨兵进程)的 redis client 进行读写 * * @param redis_hostName * @param redis_port * @param redis_passWord * @param redis_database * @return */ public static JedisCluster getJedisByJedisCluster(String redis_hostName, String redis_port, String redis_passWord, String redis_database) { Set nodes = new HashSet<>(); String[] hosts = StringUtils.trim(redis_hostName).split(","); String[] ports = StringUtils.trim(redis_port).split(","); for (int i = 0; i < hosts.length; i++) { if (ports.length == 1) { // redis cluster with same port nodes.add(new HostAndPort(hosts[1], Integer.parseInt(ports[0]))); } else { // redis cluster with different port nodes.add(new HostAndPort(hosts[1], Integer.parseInt(StringUtils.trim(ports[i])))); } } jedisCluster = new JedisCluster(nodes, 30000, config); //如果有密码,设置密码 if (null != redis_passWord && !"".equals(redis_passWord)) { /** * requirepass "root123" * masterauth "root123" */ jedisCluster.auth("root123"); } //选择数据库 DB-ID jedisCluster.select(Integer.parseInt(redis_database)); return jedisCluster; } /** * 获取redis 单机redis 环境的 redis client 进行读写 * * @param redis_hostName * @param redis_port * @param redis_passWord * @param redis_database * @return */ public static Jedis getJedisBySingMachine(String redis_hostName, String redis_port, String redis_passWord, String redis_database) { if (null != redis_passWord && !"".equals(redis_passWord.trim())) { //(final GenericObjectPoolConfig poolConfig, final String host, int port,int timeout, final String password) //single machine jedisPool = new JedisPool(config, redis_hostName, Integer.parseInt(redis_port), 30000, redis_passWord, Integer.parseInt(redis_database)); //get the redis client to read and write data jedis = jedisPool.getResource(); } else { //single machine jedisPool = new JedisPool(config, redis_hostName, Integer.parseInt(redis_port)); //get the redis client to read and write data jedis = jedisPool.getResource(); //specify the redis database jedis.select(Integer.parseInt(redis_database)); } return jedis; } /*** * 获取操作redis client的主流程(从此传入redis参数) * @param redis_isCluster * @param redis_clusterName * @param redis_hostName * @param redis_port * @param redis_passWord * @param redis_database * @return */ public static Jedis getJedis(String redis_isCluster, String redis_isSentinel, String redis_clusterName, String redis_hostName, String redis_port, String redis_passWord, String redis_database) { log.info("redis_isCluster: " + redis_isCluster + ", redis_clusterName: " + redis_clusterName + ", " + "redis_hostName: " + redis_hostName + ", redis_port: " + redis_port + ", redis_passWord: " + redis_passWord + ", redis_database: " + redis_database); if (null == jedis) { synchronized (RedisUtil.class) { if (null == jedis) { try { config = new GenericObjectPoolConfig(); //redis最大连接数 config.setMaxTotal(150); //空闲时最大连接数 config.setMaxIdle(100); //空闲时最小连接数 config.setMinIdle(50); if (null != redis_isCluster && !"".equals(StringUtils.trim(redis_isCluster)) && "true".equals(StringUtils.trim(redis_isCluster))) { //redis cluster is sentinel cluster(哨兵集群) if ("true".equals(redis_isSentinel)) { jedis = getJedisByJedisSentinelPool(redis_clusterName, redis_hostName, redis_port, redis_passWord, redis_database); // redis cluster with no sentinel(非哨兵集群) } else { jedisCluster = getJedisByJedisCluster(redis_hostName, redis_port, redis_passWord, redis_database); } //single redis instance (redis 单机) } else { jedis = getJedisBySingMachine(redis_hostName, redis_port, redis_passWord, redis_database); } } catch (Exception e) { e.printStackTrace(); } } } } return jedis; } /** * return redis client to jedisPool after operation finished * * @param jedis * @param redis_isCluster * @param redis_isSentinel */ public static void returnResource(Jedis jedis, String redis_isCluster, String redis_isSentinel) { if (null != jedis) { try { if ("true".equals(redis_isCluster)) { if ("true".equals(redis_isSentinel)) { jediSentinelPool.returnBrokenResource(jedis); } } else { jedisPool.returnBrokenResource(jedis); } } catch (Exception e) { log.info("Return Jedis client to jedis pool occurs error! " + e.getMessage()); } } } }