import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
public class RedisDistributedFactory {
public static Map<Integer, JedisSentinelPool> redisPoolMap = new HashMap<Integer, JedisSentinelPool>();
/**
* 初始化分布式锁
*/
public static void init(String xmlFilePath) {
try {
File xmlFile =new File(xmlFilePath);
InputStream xmlInputStream=new FileInputStream(xmlFile);
if (RedisConfig.redisGroupList == null) {
RedisConfig.getRedisCluster(xmlInputStream);
}
List<RedisCluster> clustersList = RedisConfig.redisGroupList;
if (clustersList != null && clustersList.size() > 0) {
for (int i = 0; i < clustersList.size(); i++) {
RedisCluster redisCluster = clustersList.get(i);
JedisSentinelPool jedisSentinelPool = getJedisSentinelPool(redisCluster);
// 初始化jedis连接池并测试
Jedis jedis = jedisSentinelPool.getResource();
jedisSentinelPool.returnResourceObject(jedis);
// 将连接池存入全局map
redisPoolMap.put(i, jedisSentinelPool);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 初始化分布式锁
*/
public static void init(InputStream xmlInputStream) {
try {
if (RedisConfig.redisGroupList == null) {
RedisConfig.getRedisCluster(xmlInputStream);
}
List<RedisCluster> clustersList = RedisConfig.redisGroupList;
if (clustersList != null && clustersList.size() > 0) {
for (int i = 0; i < clustersList.size(); i++) {
RedisCluster redisCluster = clustersList.get(i);
JedisSentinelPool jedisSentinelPool = getJedisSentinelPool(redisCluster);
// 初始化jedis连接池并测试
Jedis jedis = jedisSentinelPool.getResource();
jedisSentinelPool.returnResourceObject(jedis);
// 将连接池存入全局map
redisPoolMap.put(i, jedisSentinelPool);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 获取jedis连接池对象
*
* @return
*/
private static JedisSentinelPool getJedisSentinelPool(RedisCluster redisCluster)
throws Exception {
String masterHost= redisCluster.getMasterHost();
String slaveHost= redisCluster.getSlaveHost();
String port= redisCluster.getPort();
String masterName= redisCluster.getMasterName();
String password = redisCluster.getPassword();
//jedis连接池配置
JedisSentinelPool jedisSentinelPool = null;
try {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(10000);
jedisPoolConfig.setMinIdle(20);
jedisPoolConfig.setMaxWaitMillis(1000 * 5);
jedisPoolConfig.setMinEvictableIdleTimeMillis(1800000);
jedisPoolConfig.setTestWhileIdle(true);
jedisPoolConfig.setTestOnBorrow(false);
jedisPoolConfig.setTestOnReturn(false);
//sentinel的连接超时
int timeout= 3000;
Set<String> sentinels = new HashSet<String>();
if (!port.contains(",")) {
sentinels.add(masterHost+":"+ port);
String []slaveHostArray= slaveHost.split(",");
for(int i=0;i<slaveHostArray.length;i++){
String slaveHostOne = slaveHostArray[i];
sentinels.add(slaveHostOne+":"+ port);
}
} else {
String [] portArray= port.split(",");
for(int i=0;i<portArray.length;i++){
String portOne = portArray[i];
sentinels.add(masterHost+":"+ portOne);
}
}
if(password!=null && (!password.equals(""))){
//设置密码
jedisSentinelPool = new JedisSentinelPool(masterName,sentinels,jedisPoolConfig,timeout,password);
}
else{
//无密码
jedisSentinelPool = new JedisSentinelPool(masterName,sentinels,jedisPoolConfig,timeout);
}
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
return jedisSentinelPool;
}
/**
* 获得分布式redis对象
*
* @param selectDB
* @param Key
* @return
*/
public static RedisDistributedUtil getRedisDistributedUtils(String key, int selectDB) {
return new RedisDistributedUtil(key,selectDB);
}
/**
* 获得分布式redis锁对象
*
* @param selectDB
* @param Key
* @return
*/
public static RedisDistributedLock getRedisDistributedLock(
String key) {
return new RedisDistributedLock(key);
}
public static void main(String[] args) {
Set<String> sentinels = new HashSet<String>();
sentinels.add("127.0.0.1:26379");
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("mymaster",sentinels);
Jedis resource = jedisSentinelPool.getResource();
resource.set("name", "bitch");
System.out.println(resource.get("name"));
jedisSentinelPool.close();
// Jedis Jedis = new Jedis("192.168.0.108", 26379);
// List<String> sentinelGetMasterAddrByName = Jedis.sentinelGetMasterAddrByName("mymaster");
//
// Jedis.set("name", "bitch");
// System.out.println(Jedis.get("name"));
// Jedis.close();
}
}