配置文件對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);
}
}
}
好資源希望的到支持哈~~

