1,redis采用的是1主2从模式,读写分离,服务端版本为3.0。
2,Java客户端使用的是jedis2.5.2
上代码-----------------------
1,封装的配置文件对象
public class JedisPoolConfigBean implements Serializable{
/**
* 序列化值
*/
private static final long serialVersionUID = 1L;
//最大空闲连接数
private int maxIdle;
//最大连接数
private int maxTotal;
//设置最大阻塞时间,记住是毫秒数milliseconds
private long maxWaitMillis;
//在获取连接的时候检查有效性, 默认false
private boolean testOnBorrow;
//Redis主机IP
private List<String> redisHostIP;
//Redis主机Port
private List<String> redisHostport;
//超时时间
private int timeout;
//对象池 进出规则
private boolean lifo;
set/get方法省略
}
2,封装的jedispool单列对象
private static MSJedisPool mSJedisPool ;
private MSJedisPool(){
}
private JedisPool masterPool ;
private JedisPool[] slavePool ;
private MSJedisPool(JedisPoolConfig jedisPoolConfig ,String masterIp,String masterPort ,List<String> slaveIpList,List<String> slavePortList){
masterPool = new JedisPool(jedisPoolConfig,masterIp,Integer.parseInt(masterPort)) ;
int len=slaveIpList.size() ;
slavePool = new JedisPool[len] ;
for(int i=0 ; i<len ; i++ ){
slavePool[i] = new JedisPool(jedisPoolConfig,slaveIpList.get(i),Integer.parseInt(slavePortList.get(i)) );
}
}
public static MSJedisPool getInstance(JedisPoolConfig jedisPoolConfig ,String masterIp,String masterPort ,List<String> slaveIpList,List<String> slavePortList){
if(null==mSJedisPool){
synchronized(MSJedisPool.class){
mSJedisPool = new MSJedisPool( jedisPoolConfig , masterIp, masterPort , slaveIpList, slavePortList) ;
}
}
return mSJedisPool ;
}
set/get方法省略
}
3,工厂类
public class MSRedisFactory {
public static Logger log = Logger.getLogger(MSRedisFactory.class) ;
private static JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
public static MSRedis produce(JedisPoolConfigBean jedisMaster ,JedisPoolConfigBean jedisSlave){
//设置最大空闲数
jedisPoolConfig.setMaxIdle(jedisMaster.getMaxIdle());
//设置最大连接数
jedisPoolConfig.setMaxTotal(jedisMaster.getMaxTotal());
//设置超时时间
jedisPoolConfig.setMaxWaitMillis(jedisMaster.getMaxWaitMillis());
//设置返回对象是否是有效
jedisPoolConfig.setTestOnReturn(jedisMaster.isTestOnBorrow());
jedisPoolConfig.setLifo(jedisMaster.isLifo());
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(1000);
MSJedisPool msJedisPool = MSJedisPool.getInstance(jedisPoolConfig, jedisMaster.getRedisHostIP().get(0), jedisMaster.getRedisHostport().get(0),
jedisSlave.getRedisHostIP(), jedisSlave.getRedisHostport()) ;
return new MSRedis(msJedisPool) ;
}
}
4,实际执行set,get方法类
private MSJedisPool mSJedisPool ;
private Random random = new Random() ;
public MSRedis(){
}
public MSRedis(MSJedisPool mSJedisPool){
this.mSJedisPool = mSJedisPool;
}
/** 实际命令的代理执行者 */
private interface CmdProxy{
Object execute(final Jedis jedis);
}
// 封装的实际execute操作步骤
//主redis释放pool
private Object masterExecute(CmdProxy cmd){
JedisPool pool = mSJedisPool.getMasterPool() ;
Jedis jedis = pool.getResource() ;
try{
return cmd.execute(jedis);
} catch(Exception e){
pool.returnBrokenResource(jedis);
log.error(e.getMessage());
throw new RuntimeException(e);
} finally{
pool.returnResource(jedis);
}
}
private Object slaveExecute(CmdProxy cmd){
JedisPool[] pool = mSJedisPool.getSlavePool() ;
int index = random.nextInt(pool.length) ;
JedisPool slavePool = pool[index] ;
Jedis jedis = slavePool.getResource() ;
try{
return cmd.execute(jedis);
} catch(Exception e){
slavePool.returnBrokenResource(jedis);
log.error(e.getMessage());
throw new RuntimeException(e);
} finally{
slavePool.returnResource(jedis);
}
}
/**
* set 方法
* @param key
* @param value
* @return
*/
public String set(final String key, final String value){
return (String) this.masterExecute(new CmdProxy(){
@Override
public Object execute(Jedis jedis)
{
return jedis.set(key, value);
}
});
}
/**
* get 方法
* @param key
* @return
*/
public String get(final String key){
return (String) this.slaveExecute(new CmdProxy(){
@Override
public Object execute(Jedis jedis)
{
return jedis.get(key);
}
});
}
需要的方法自己继续封装,这里由于是有2台从,没有采用轮询方式去分配访问,而是采用随机的方式