项目中需要用到缓存减少数据库压力,选择redis作为工具,构建一个jedis池达到实际效果
1.JedisPoolCacheUtils
redis.clients
jedis
2.9.0
package com.ithzk.common.redis;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ithzk.common.util.Detect;
import com.ithzk.common.util.JsonUtil;
import com.ithzk.common.util.PropertiesUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* redis 操作数据库配置类
* @author huzekun
* @date:2017年12月27日 下午3:01:04
*/
@Component("JedisPoolCacheUtils")
public class JedisPoolCacheUtils {
private final static Logger log = Logger.getLogger(JedisPoolCacheUtils.class);
public final static String DATA_REDIS_KEY = "data_";
/**
* redis过期时间,以秒为单位
*/
public final static int EXRP_HOUR = 60 * 60; //一小时
public final static int EXRP_HALF_DAY = 60 * 60 * 12; //半天
public final static int EXRP_DAY = 60 * 60 * 24; //一天
public final static int EXRP_MONTH = 60 * 60 * 24 * 30; //一个月
private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
public static void initialPool(String path){
Properties prop = new Properties();
try {
prop.load(JedisPoolCacheUtils.class.getClassLoader().getResourceAsStream(path+"-conf/redis.properties"));
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Detect.asPrimitiveInt(prop.getProperty("redis.pool.maxActive")));
config.setMaxIdle(Detect.asPrimitiveInt(prop.getProperty("redis.pool.maxIdle")));
config.setMinIdle(Detect.asPrimitiveInt(prop.getProperty("redis.pool.minIdle")));
config.setMaxWaitMillis(Detect.asPrimitiveInt(prop.getProperty("redis.pool.maxWait")));
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
config.setTestWhileIdle(true);
String host = prop.getProperty("redis.host");
String port = prop.getProperty("redis.port");
String timeOut = prop.getProperty("redis.timeout");
jedisPool = new JedisPool(config, host, Detect.asPrimitiveInt(port), Detect.asPrimitiveInt(timeOut));
} catch (Exception e) {
log.error("First create JedisPool error : "+e);
try{
//如果第一个IP异常,则访问第二个IP
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Detect.asPrimitiveInt(PropertiesUtil.getValueByBundleFromConf(path+"-conf/redis.properties","redis.pool.maxActive")));
config.setMaxIdle(Detect.asPrimitiveInt(PropertiesUtil.getValueByBundleFromConf(path+"-conf/redis.properties","redis.pool.maxIdle")));
config.setMinIdle(Detect.asPrimitiveInt(PropertiesUtil.getValueByBundleFromConf(path+"-conf/redis.properties","redis.pool.minIdle")));
config.setMaxWaitMillis(Detect.asPrimitiveInt(PropertiesUtil.getValueByBundleFromConf(path+"-redis.properties","redis.pool.maxWait")));
config.setTestOnBorrow(true);
String host = PropertiesUtil.getValueByBundleFromConf(path+"-conf/redis.properties","redis.host");
String port = PropertiesUtil.getValueByBundleFromConf(path+"-conf/redis.properties","redis.port");
String timeOut = PropertiesUtil.getValueByBundleFromConf(path+"-conf/redis.properties","redis.timeout");
jedisPool = new JedisPool(config, host, Detect.asPrimitiveInt(port), Detect.asPrimitiveInt(timeOut));
}catch(Exception e2){
log.error("Second create JedisPool error : "+e2);
}
}
//Jedis jedis = jedisPool.getResource();
//log.info("=====初始化redis池成功! 状态:"+ jedis.ping());
log.info("=====初始化redis池成功!");
}
/**
*
* setVExpire(设置key值,同时设置失效时间 秒)
* @Title: setVExpire
* @param @param key
* @param @param value
* @param @param seconds
* @param index 具体数据库 默认使用0号库
* @return void
* @throws
*/
public static void setVExpire(String key, V value,int seconds,int index) {
String json = JsonUtil.object2json(value);
//String json = JSON.toJSONString(value);
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(index);
jedis.set(DATA_REDIS_KEY +key, json);
jedis.expire(DATA_REDIS_KEY +key, seconds);
} catch (Exception e) {
log.error("setV初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
}
/**
*
* (存入redis数据)
* @Title: setV
* @param @param key
* @param @param value
* @param index 具体数据库
* @return void
* @throws
*/
public static void setV(String key, V value,int index) {
String json = JSON.toJSONString(value);
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(index);
jedis.set(DATA_REDIS_KEY +key, json);
} catch (Exception e) {
log.error("setV初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
}
/**
*
* getV(获取redis数据信息)
* @Title: getV
* @param @param key
* @param index 具体数据库 0:常用数据存储 3:session数据存储
* @param @return
* @return V
* @throws
*/
@SuppressWarnings("unchecked")
public static V getV(String key,int index) {
String value = "";
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(index);
value = jedis.get(DATA_REDIS_KEY +key);
} catch (Exception e) {
log.error("getV初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
return (V)JSONObject.parse(value);
}
/**
*
* getVString(返回json字符串)
* @Title: getVString
* @param @param key
* @param @param index
* @param @return
* @return String
* @throws
*/
public static String getVStr(String key,int index) {
String value = "";
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(index);
value = jedis.get(DATA_REDIS_KEY +key);
} catch (Exception e) {
log.error("getVString初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
return value;
}
/**
*
* Push(存入 数据到队列中)
*
* @Title: Push
* @param @param key
* @param @param value
* @return void
* @throws
*/
public static void Push(String key, V value) {
String json = JSON.toJSONString(value);
Jedis jedis = null;
try {
log.info("存入 数据到队列中");
jedis = jedisPool.getResource();
jedis.select(15);
jedis.lpush(key, json);
} catch (Exception e) {
log.error("Push初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
}
/**
*
* Push(存入 数据到队列中)
*
* @Title: PushV
* @param key
* @param value
* @param dBNum
* @return void
* @throws
*/
public static void PushV(String key, V value,int dBNum) {
String json = JSON.toJSONString(value);
Jedis jedis = null;
try {
log.info("存入 数据到队列中");
jedis = jedisPool.getResource();
jedis.select(dBNum);
jedis.lpush(key, json);
} catch (Exception e) {
log.error("Push初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
}
/**
*
* Push(存入 数据到队列中)
*
* @Title: Push
* @param @param key
* @param @param value
* @return void
* @throws
*/
public static void PushEmail(String key, V value) {
String json = JsonUtil.object2json(value);
Jedis jedis = null;
try {
log.info("存入 数据到队列中");
jedis = jedisPool.getResource();
jedis.select(15);
jedis.lpush(key, json);
} catch (Exception e) {
log.error("Push初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
}
/**
*
* Pop(从队列中取值)
*
* @Title: Pop
* @param @param key
* @param @return
* @return V
* @throws
*/
@SuppressWarnings("unchecked")
public static V Pop(String key) {
String value = "";
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(15);
value = jedis.rpop(DATA_REDIS_KEY +key);
} catch (Exception e) {
log.error("Pop初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
return (V) value;
}
/**
*
* expireKey(限时存入redis服务器)
*
* @Title: expireKey
* @param @param key
* @param @param seconds
* @return void
* @throws
*/
public static void expireKey(String key, int seconds) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.select(3);
jedis.expire(DATA_REDIS_KEY +key, seconds);
} catch (Exception e) {
log.error("Pop初始化jedis异常:" + e);
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedis.close();
}
} finally {
closeJedis(jedis);
}
}
/**
*
* closeJedis(释放redis资源)
*
* @Title: closeJedis
* @param @param jedis
* @return void
* @throws
*/
public static void closeJedis(Jedis jedis) {
try {
if (jedis != null) {
/*jedisPool.returnBrokenResource(jedis);
jedisPool.returnResource(jedis);
jedisPool.returnResourceObject(jedis);*/
//高版本jedis close 取代池回收
jedis.close();
}
} catch (Exception e) {
log.error("释放资源异常:" + e);
}
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
}
2.配置文件 redis.properties
#最大连接数
redis.pool.maxActive=300
#最大空闲连接数
redis.pool.maxIdle=150
#最小空闲连接数
redis.pool.minIdle=10
#最大等待时间
redis.pool.maxWait=300
#redis基本配置 ip 端口 超时
redis.host=10.200.9.251
redis.port=6379
redis.timeout=6000
3.配置文件 web.xml配置监听器
配置监听器使项目启动自动初始化jedisPool
com.ithzk.common.listener.ApplicationListener
4.ApplicationListener监听器
package com.ithzk.common.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ithzk.common.Consts;
import com.ithzk.common.listener.load.LinkAdmin;
import com.ithzk.common.redis.JedisPoolCacheUtils;
public class ApplicationListener implements ServletContextListener {
private static Logger log = LoggerFactory.getLogger(ApplicationListener.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
initProjectEnvironment();
}
/**
* 初始化项目环境
*/
private void initProjectEnvironment() {
//初始化jedis池
JedisPoolCacheUtils.initialPool(path);
}
@Override
public void contextDestroyed(ServletContextEvent sce) { }
}
5.使用jedisPool操作数据
package com.ithzk.controller;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ithzk.common.consts.Consts;
import com.ithzk.common.exception.NotMactionException;
import com.ithzk.common.redis.JedisPoolCacheUtils;
import com.ithzk.common.util.Base64Util;
import com.ithzk.common.util.CdnUtil;
import com.ithzk.common.util.Detect;
import com.ithzk.common.util.HttpObjUtil;
import com.ithzk.common.util.Inspection;
import com.ithzk.common.util.JsonUtil;
import com.ithzk.common.util.Response;
import com.ithzk.service.IDataService ;
@Controller
@RequestMapping("/data")
public class DataController {
private static Logger log = LoggerFactory.getLogger(DataController .class);
@Autowired
private IDataService dataService;
@RequestMapping(value="/mkDetails",method=RequestMethod.POST)
public void mkDetails(HttpServletResponse hResponse,HttpServletRequest request) throws IOException {
hResponse.setCharacterEncoding("UTF-8");
hResponse.setContentType("text/html;charset=utf-8");
//跨域请求 * 允许所有
hResponse.setHeader("Access-Control-Allow-Origin", "*");
hResponse.setHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE");
hResponse.setHeader("Access-Control-Max-Age", "3600");
hResponse.setHeader("Access-Control-Allow-Headers", "x-requested-with");
PrintWriter out = hResponse.getWriter();
Response response = new Response();
response.setSuccess(true);
String user= (String) request.getParameter("user");
if (Detect.notEmpty(mac)) {
String redisResponse = JedisPoolCacheUtils.getVStr(user.trim(), 0);
//Response parseObject = JSON.parseObject(redisResponse,Response.class);
if (Detect.notEmpty(redisResponse)) {
log.info("=======>redis "+redisResponse);
JsonUtil.writeDirect(out, redisResponse);
//JsonUtil.write(out, parseObject);
}else {
response = dataService.mkDetails(user.trim());
log.info("=======>mysql "+response.toString());
JedisPoolCacheUtils.setVExpire(user.trim(), response, JedisPoolCacheUtils.EXRP_HALF_DAY, 0);
}
}else {
response.setSuccess(false);
response.setCode(4001);
response.setMsg("参数无效!");
}
JsonUtil.write(out, response);
}
}
/jedisPool.returnBrokenResource(jedis);
jedisPool.returnResource(jedis);
jedisPool.returnResourceObject(jedis);/
//高版本jedis close 取代池回收
jedis.close();
6.回收资源
老版本
jedisPool.returnBrokenResource(jedis);
jedisPool.returnResource(jedis);
jedisPool.returnResourceObject(jedis);
高版本jedis close 取代池回收
jedis.close();
注意事项:最近在部署到服务器的时候遇到了一些问题分享给大家
首先是由于服务器上是集群化的redis,但是我们使用了一些特殊命令,导致项目部署之后redis无法使用
比如集群化的redis可能不支持select更改库的命令
然后第二个问题是初始化的问题,当配置文件或者初始化参数有问题的时候,比如maxTotal我们设为0,开发时单机版的redis可以顺利使用,但是部署到服务器上(这里我们使用Codis)会出现无法从池中获取资源的问题