配置文件对redis的配置:
#REDIS_CONFIG redis.sentinels = x.x.x.x:p,x.x.x.x:p,x.x.x.x:p redis.sentinel.master = redis-master redis.password = password redispool.maxtotal = 6000 redispool.maxidle = 300 redispool.maxwaitmillis = 10000 redispool.timeout = 100 #redis单机配置 redis.pool.host=127.0.0.1 redis.pool.port=6379 #最大能够保持idel状态的对象数 redis.pool.maxIdle=100 #最大分配的对象数 redis.pool.maxTotal=6000 #等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。 #如果超过等待时间,则直接抛出JedisConnectionException redis.pool.maxWaitMillis=10000 redis.pool.timeOut=20 #多长时间检查一次连接池中空闲的连接 redis.pool.timeBetweenEvictionRunsMillis=30000 #空闲连接多长时间后会被收回 redis.pool.minEvictableIdleTimeMillis=30000 #当调用borrow Object方法时,是否进行有效性检查 #在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作; #如果为true,则得到的jedis实例均是可用的 redis.pool.testOnBorrow=true ########reids编码格式 redis.encode=utf-8 ######缓存过期时间 秒 1000*60*60*24*7 七天 redis.expire=604800000 ####是否开启Redis服务应用 redis.unlock=false #redis.sentinel.host1=127.0.0.1 #redis.sentinel.port1=26379 # #redis.sentinel.host2=127.0.0.1 #redis.sentinel.port2=26479 # #redis.pool.maxTotal=1024 #redis.pool.maxIdle=200 #redis.pool.maxWaitMillis=1000 #redis.pool.testOnBorrow=true # #redis.pool.timeBetweenEvictionRunsMillis=30000 #redis.pool.minEvictableIdleTimeMillis=30000 #redis.pool.softMinEvictableIdleTimeMillis=10000 #redis.pool.numTestsPerEvictionRun=1024 # ##1000*60*60*1 #redis.pool.expire=3600000 #redis.pool.unlock=false
Redis工具类的实现:
package com.base.redis; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.base.utils.CalendarUtil; import com.base.utils.DateUtils; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.petrochina.dao.BaseStockHlradioMapper; import com.petrochina.pojo.BaseProvincialArea; import com.petrochina.pojo.BaseStockHlradio; import org.apache.commons.lang.StringUtils; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.apache.log4j.Logger; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisSentinelPool; import javax.annotation.Resource; import javax.swing.*; import java.util.*; /** * Explain:Redis连接池 */ public final class RedisUtil { //========================================================================================// //## 单机配置 ##// //========================================================================================// //Redis服务器IP // private static String ADDR = "127.0.0.1"; // private static String ADDR = res.getString("redis.pool.host"); // //Redis的端口号 // private static Integer PORT = Integer.parseInt(res.getString("redis.pool.port")); // //访问密码 // private static String AUTH = ""; // // //可用连接实例的最大数目,默认为8; // //如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽) // private static Integer MAX_TOTAL = Integer.parseInt(res.getString("redis.pool.maxTotal")); // //控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8 // private static Integer MAX_IDLE = Integer.parseInt(res.getString("redis.pool.maxIdle")); // //等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。 // //如果超过等待时间,则直接抛出JedisConnectionException // private static Integer MAX_WAIT_MILLIS = Integer.parseInt(res.getString("redis.pool.maxWaitMillis")); // private static Integer TIMEOUT = Integer.parseInt(res.getString("redis.pool.timeOut")); // //在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作; // //如果为true,则得到的jedis实例均是可用的 // private static Boolean TEST_ON_BORROW = Boolean.valueOf(res.getString("redis.pool.testOnBorrow")); // private static JedisPool jedisPool = null; private static ResourceBundle res = ResourceBundle.getBundle("redis"); private static Logger log = Logger.getLogger(RedisUtil.class); private static String password; private static JedisSentinelPool jedisPool; static { try{ String[] servers = res.getString("redis.sentinels").split(","); int maxtotal = Integer.parseInt(res.getString("redispool.maxtotal")); int maxidle = Integer.parseInt(res.getString("redispool.maxidle")); int maxwaitmillis = Integer.parseInt(res.getString("redispool.maxwaitmillis")); int timeout = Integer.parseInt(res.getString("redispool.timeout")); long timeBetweenEvictionRunsMillis = Long.valueOf(res.getString("redis.pool.timeBetweenEvictionRunsMillis")); long minEvictableIdleTimeMillis = Long.valueOf(res.getString("redis.pool.minEvictableIdleTimeMillis")); String sentinelmaster = res.getString("redis.sentinel.master"); password = res.getString("redis.password"); Set<String> sentinels = new HashSet<>(16); for (String server:servers) { sentinels.add(server); } GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(maxtotal); config.setMaxIdle(maxidle); config.setMaxWaitMillis(maxwaitmillis); config.setTestOnBorrow(false); config.setTestOnReturn(false); config.setTestWhileIdle(true); config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); //setinel客户端提供了master自动发现功能 jedisPool = new JedisSentinelPool(sentinelmaster,sentinels,config,timeout,password); }catch (Exception e){ e.printStackTrace(); } } /** * 获取Jedis实例 * @return */ public static Jedis getJedis(){ long seconds = System.currentTimeMillis(); try { if(jedisPool != null){ Jedis jedis = jedisPool.getResource(); return jedis; }else{ return null; } } catch (Exception e) { e.printStackTrace(); return null; } } public static void returnResource(final Jedis jedis){ long seconds = System.currentTimeMillis(); if(jedis!=null){ jedis.close(); } } public static JSONObject VerifyKey(String key){ Jedis jedis =null; JSONObject jsonReturn = null; try { jedis = getJedis(); if (jedis == null) { return null; } String json = jedis.get(key); if (json==null || json.equals("")) { jsonReturn = null; }else{ jsonReturn = JSONObject.parseObject(json); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != jedis) { returnResource(jedis); } } return jsonReturn; } /** * 验证报警key是否存在 * @param key * @return */ public static String VerifyWarnKey(String key){ Jedis jedis = null; String dateStr = null; try { jedis = getJedis(); dateStr = ""; if (jedis != null) { dateStr = jedis.get(key); } } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis); } return dateStr; } public static Map<String,String> VerifyMap(String key){ Map<String,String> reMap = null; Jedis jedis = null; String jsonReturn = null; try { jedis = getJedis(); if (jedis == null) { return null; } reMap = Maps.newHashMap(); reMap = jedis.hgetAll(key); if (reMap==null || reMap.size() == 0) { reMap = null; } } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis); } return reMap; } public static void setData(String key, String jsonString) { Jedis jedis = null; String jsonReturn = null; try { jedis = getJedis(); if (jedis != null) { String result = jedis.set(key, jsonString); System.out.println(result); jedis.expireAt(key,CalendarUtil.getExpireTime()); } } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis); } } //放入string数据 public static void setString(String key, String value) { Jedis jedis = null; String jsonReturn = null; try { jedis = getJedis(); if (jedis != null) { jedis.set(key,value); } } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis); } } //根据key删除 public static void deleteKey(String key) { Jedis jedis = null; String jsonReturn = null; try { jedis = getJedis(); if (jedis != null) { jedis.del(key); } } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis); } } public static void setMapData(String key, Map<String,String> map) { Jedis jedis = null; String jsonReturn = null; try { jedis = getJedis(); if (jedis != null) { jedis.hmset(key,map); jedis.expireAt(key,CalendarUtil.getExpireTime()); } } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis); } } }
好资源希望的到支持哈~~
原文地址:https://www.cnblogs.com/fjinlong/p/8559251.html
时间: 2024-10-12 04:20:45