java项目中redis的配置和工具方法
配置文件对 redis 的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #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 工具类的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | 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); } } } |
好资源希望的到支持哈 ~~