標籤:array 存在 log4 配置 ret public get pack class
設定檔對redis的配置:
#REDIS_CONFIGredis.sentinels = x.x.x.x:p,x.x.x.x:p,x.x.x.x:predis.sentinel.master = redis-masterredis.password = passwordredispool.maxtotal = 6000redispool.maxidle = 300redispool.maxwaitmillis = 10000redispool.timeout = 100#redis單機配置redis.pool.host=127.0.0.1redis.pool.port=6379#最大能夠保持idel狀態的對象數redis.pool.maxIdle=100#最大分配的對象數redis.pool.maxTotal=6000#等待可用串連的最大時間,單位是毫秒,預設值為-1,表示永不逾時。#如果超過等待時間,則直接拋出JedisConnectionExceptionredis.pool.maxWaitMillis=10000redis.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); } }}
好資源希望的到支援哈~~
java項目中redis的配置好工具方法