Maven專案中redis(jedis)的配置和使用
上一篇部落格簡單闡述java遠端連線redis遇到的問題,這一篇部落格詳細闡述遠端連線redis的相關配置。
要想在Java中連線Redis,並進行操作,由兩種方式,一種是spring data redis,它是由spring整合的,不支援叢集,一種是官方推薦的jedis,支援叢集,其他功能差不多一樣,
這裡我大致介紹jedis操作例項,以下是使用Jedis的具體步驟:
在javaweb專案(使用了Maven)的pom.xml檔案中新增以下依賴
<dependency>
<groupId>redis.clients</groupId >
<artifactId>jedis</artifactId>
<version>2.5.2</version>
</dependency>
注意:如果不是使用Maven的專案下載包匯入專案即可。
建立redisUtil工具類來封裝jedis的相關配置和方法:
建立工具類的思路:
1>建立jedisPool(JedisPool的配置引數大部分是由JedisPoolConfig的對應項來賦值的)
2>獲取Jedis例項需要從JedisPool中獲取
3>用完Jedis例項需要返回給JedisPool
4>如果Jedis在使用過程中出錯,那麼也需要返回給JedisPool
其中jedisPool 繼承Pool< Jedis > ,檢視原始碼可知建立jedisPool所需要的各種引數,一一配置即可。
其中JedisPoolConfig繼承GenericObjectPoolConfig對一些引數的預設設定如下:
setTestWhileIdle(true);
setMinEvictableIdleTimeMillis(60000);
setTimeBetweenEvictionRunsMillis(30000);
setNumTestsPerEvictionRun(-1);
redisUtil工具類具體程式碼:
public class RedisUtil {
private static Logger logger = Logger.getLogger(RedisUtil.class);
private static JedisPool pool = null;
/**
* 構建redis連線池
*
* @param ip
* @param port
* @return JedisPool
*/
public static JedisPool getPool() {
if (pool == null) {
// jedispool為null則初始化,
JedisPoolConfig config = new JedisPoolConfig();
// 控制一個pool可分配多少個jedis例項,通過pool.getResource()來獲取;
// 如果賦值為-1,則表示不限制;如果pool已經分配了maxTotal個jedis例項,則此時pool的狀態為exhausted(耗盡).
config.setMaxTotal(500);
// 控制一個pool最多有多少個狀態為idle(空閒的)的jedis例項
config.setMaxIdle(5);
// 表示當borrow(引入)一個jedis例項時,最大的等待時間,如果超過等待時間,則直接丟擲JedisConnectionException;
config.setMaxWaitMillis(1000 * 10);
// 在borrow一個jedis例項時,是否提前進行validate操作;如果為true,則得到的jedis例項均是可用的;
config.setTestOnBorrow(true);
pool = new JedisPool(config, localhost, 6379, 10000, "123");
//10000是protocol.timeout 預設值2000
}
return pool;
}
/**
* 獲取資料
*
* @param key
* @return
*/
public static String get(String key) {
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
// TODO: handle exception
// 釋放redis物件
pool.returnBrokenResource(jedis);
logger.error("jedis error is" + "e.printStackTrace()");
logger.error("fail to get data from jedis ", e);
} finally {
// 返還到連線池
pool.returnResource(jedis);
}
return value;
}
/**
* 給key賦值,並生命週期設定為seconds
*
* @param key
* @param seconds
* 生命週期 秒為單位
* @param value
*/
public static void setx(String key, int seconds, String value) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.setex(key, seconds, value);
} catch (Exception e) {
// 釋放redis物件
pool.returnBrokenResource(jedis);
logger.error("fail to set key and seconds", e);
} finally {
// 返還到連線池
pool.returnResource(jedis);
}
}
/**
* 根據key值來刪除已經存在的key-value;
*
* @param key
* @return
*/
public static int removex(String key) {
int temp = 0;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
temp = jedis.del(key).intValue();
} catch (Exception e) {
// TODO: handle exception
pool.returnBrokenResource(jedis);
logger.error("fail to delete the key-value according to the key", e);
} finally {
//返回redis例項
pool.returnResource(jedis);
}
return temp;
}
}
其中在使用完jedis例項時候,進行了資源的返回:pool.returnResource(jedis)
jedis在使用過程出現錯誤,也進行了資源的返回:pool.returnBrokenResource(jedis)
補充jedis版本帶來的問題:
jedis包括2.4.1,2.5.1等高版本的JedisPoolConfig是沒有maxActive和maxWait屬性的
改動如下:
maxActive->maxTotal
maxWait->maxWaitMillis
redisUtil工具類做好了,使用的時候直接使用其中的方法就可以~
通過自己寫jedisPool的配置,深刻領悟到看原始碼的重要性!!!