1、添加pom文件
<!-- Jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.2.1</version>
</dependency>
2、添加redis.properties文件
book.redis.pool.maxWait=1000
book.redis.pool.maxActive=1024
book.redis.pool.maxIdle=200
book.redis.pool.ip=localhost
book.redis.pool.port=6379
3、添加RedisPool.java类
public class RedisPool<T> {
private String poolName;
private String ip;
private String pore;
private int maxWait;
private int maxActive;
private int maxIdle;
private static JedisPool jedisPool;
public RedisPool() {
}
public RedisPool(String poolName, String ip, String pore, int maxWait, int maxActive, int maxIdle) {
this.poolName = poolName;
this.ip = ip;
this.pore = pore;
this.maxWait = maxWait;
this.maxActive = maxActive;
this.maxIdle = maxIdle;
createPool();
}
//创建池
private void createPool(){
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
jedisPoolConfig.setMaxActive(maxActive);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWait(maxWait);
jedisPoolConfig.setTestOnBorrow(false);
jedisPool =new JedisPool(jedisPoolConfig,ip, SsmUtil.convert(pore,6379),maxWait);
}
public String getPoolName() {
return poolName;
}
public void setPoolName(String poolName) {
this.poolName = poolName;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPore() {
return pore;
}
public void setPore(String pore) {
this.pore = pore;
}
public int getMaxWait() {
return maxWait;
}
public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
//普通类set方法
public void set(String key, String value) {
boolean flag = true;
Jedis jedis = jedisPool.getResource();
try{
if(jedis!=null){
jedis.set(key, value);
}
}catch(Exception e){
flag = false;
e.printStackTrace();
}finally{
if (flag){
jedisPool.returnResource(jedis);
}else{
jedisPool.returnBrokenResource(jedis);
}
}
}
//String 类get方法
public String get(String key) {
boolean flag = true;
Jedis jedis =jedisPool.getResource();
try{
if(jedis!=null){
return jedis.get(key);
}
}catch(Exception e){
flag = false;
e.printStackTrace();
}finally{
if (flag){
//把数据还回数据池
jedisPool.returnResource(jedis);
}else{
//销毁数据
jedisPool.returnBrokenResource(jedis);
}
}
return null;
}
}
4、添加 RedisUtil类
public class RedisUtil {
/**
* redis连接池Map
* 可以有多个连接池对象
*/
private static HashMap<String, RedisPool> redisPoolMap = new HashMap<>();
private final static String REDIS_PROFILE = "properties/redis.properties";
/**
* 获取连接池对象
*
* @param poolName 连接池的名称 需要和配置文件对应
* @return 连接池对象
*/
public static synchronized RedisPool getRedisPool(String poolName) {
RedisPool rp = null;
if (redisPoolMap.containsKey(poolName)) {
rp = redisPoolMap.get(poolName);
} else {
//读取poolname对应的配置文件的属性来构建poolName
try {
Properties pro = PropertiesUtil.getProperties(REDIS_PROFILE,"utf-8");
rp = createRedisPool(poolName, pro);
redisPoolMap.put(poolName, rp);
} catch (Exception e) {
e.printStackTrace();
}
}
return rp;
}
//创建redis数据源
private static RedisPool createRedisPool(String poolName, Properties pro) {
String ip = pro.getProperty(poolName + ".redis.pool.ip");
String port = pro.getProperty(poolName + ".redis.pool.port");
System.out.println(poolName + ".redis.pool.maxWait");
int maxWait = SsmUtil.convert(pro.getProperty(poolName + ".redis.pool.maxWait"), 0);
int maxActive = SsmUtil.convert(pro.getProperty(poolName + ".redis.pool.maxActive"), 0);
int maxIdle = SsmUtil.convert(pro.getProperty(poolName + ".redis.pool.maxIdle"), 0);
System.out.println((poolName + ".redis.pool.ip") + ":[ip:" + ip + " 端口:" + port + " 等待:" + maxWait + " maxActive:" + maxActive + " maxIdle:" + maxIdle + "]");
return new RedisPool(poolName, ip, port, maxWait, maxActive, maxIdle);
}
}
5、添加PropertiesUtil类
public class PropertiesUtil {
//用来测试的方法
public static void main(String [] args)
{
Properties p = getProperties("properties/redis.properties","utf-8");
System.out.println(p);
}
//解析properties文件添加到properties类
public static Properties getProperties(String filepath,String charset) {
Properties pro = new Properties();
Reader reader = null;
InputStream is = null;
try{
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filepath);
reader = new InputStreamReader(is, Charset.forName(charset));
pro.load(reader);
}
catch (Exception e)
{
e.printStackTrace();
}
finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return pro;
}
}
6、添加 SsmUtil类
public class SsmUtil {
//转换数据类型方法
public static int convert(String property, int i) {
return Integer.parseInt(property);
}
}