一、建一個redis.properties屬性文件java
# Redis Setting
redis.addr = 127.0.0.1
redis.port = 6379
redis.auth = master
redis.maxIdle = 200
redis.maxActive = 1024
redis.maxWait = 10000
redis.timeOut = 10000
redis.testOnBorrow = true
二、新建一個RedisUtil.java文件,文件代碼以下:
package cn.com.taiji.sample.utils; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; public class RedisUtil implements Serializable{ private static final long serialVersionUID = -1149678082569464779L; //Redis服務器IP private static String addr; //Redis的端口號 private static int port; //訪問密碼 private static String auth; //可用鏈接實例的最大數目,默認值爲8; //若是賦值爲-1,則表示不限制;若是pool已經分配了maxActive個jedis實例,則此時pool的狀態爲exhausted(耗盡)。 private static int maxActive; //控制一個pool最多有多少個狀態爲idle(空閒的)的jedis實例,默認值也是8。 private static int maxIdle; //等待可用鏈接的最大時間,單位毫秒,默認值爲-1,表示永不超時。若是超過等待時間,則直接拋出JedisConnectionException; private static int maxWait; private static int timeOut; //在borrow一個jedis實例時,是否提早進行validate操做;若是爲true,則獲得的jedis實例均是可用的; private static boolean testOnBorrow; public static Jedis jedis;//非切片額客戶端鏈接 public static JedisPool jedisPool;//非切片鏈接池 public static ShardedJedis shardedJedis;//切片額客戶端鏈接 public static ShardedJedisPool shardedJedisPool;//切片鏈接池 /** * 初始化非切片池 */ private static void initialPool() { // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxActive); config.setMaxIdle(maxIdle); config.setMaxWaitMillis(maxWait); config.setTestOnBorrow(testOnBorrow); jedisPool = new JedisPool(config, addr, port); } /** * 初始化切片池 */ private static void initialShardedPool() { // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(maxActive); config.setMaxIdle(maxIdle); config.setMaxWaitMillis(maxWait); config.setTestOnBorrow(testOnBorrow); // slave連接 List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(addr, port, auth)); // 構造池 shardedJedisPool = new ShardedJedisPool(config, shards); } public static void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub initialPool(); initialShardedPool(); try { shardedJedis = shardedJedisPool.getResource(); } catch (Exception e) { System.out.println("鏈接shardedJedisPool失敗!"); } try { jedis = jedisPool.getResource(); } catch (Exception e) { System.out.println("鏈接jedisPool失敗!"); } } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getAuth() { return auth; } public void setAuth(String auth) { this.auth = auth; } 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; } public int getMaxWait() { return maxWait; } public void setMaxWait(int maxWait) { this.maxWait = maxWait; } public int getTimeOut() { return timeOut; } public void setTimeOut(int timeOut) { this.timeOut = timeOut; } public boolean isTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } }
在set方法中會飄黃,不影響使用。web
三、在spring-servlet.xml配置文件中,加入如下代碼:redis
<context:component-scan base-package="cn.com.taiji.sample.config.web" /> <context:property-placeholder location="file:#{systemProperties['webapp.sample']}/WEB-INF/conf/redis.properties" /> <bean id="redisUtil" class="cn.com.taiji.sample.utils.RedisUtil"> <property name="addr"><value>${redis.addr}</value></property> <property name="port"><value>${redis.port}</value></property> <property name="auth"><value>${redis.auth}</value></property> <property name="maxIdle"><value>${redis.maxIdle}</value></property> <property name="maxActive"><value>${redis.maxActive}</value></property> <property name="maxWait"><value>${redis.maxWait}</value></property> <property name="timeOut"><value>${redis.timeOut}</value></property> <property name="testOnBorrow"><value>${redis.testOnBorrow}</value></property> </bean>
<context:property-placeholder location="" />指的是redis.properties的文件路徑。到此,redis的spring配置就已經完成了。下一篇將介紹redis在java項目中的使用。