Redis获取方式
属性文件
#redis监控配置
master.ip=10.2.4.12
master.port=6379
slave.ip=10.2.4.13,10.2.4.14
slave.port=6379
maxActive=50
maxIdle=5
maxWait=-1
监控timer
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* RedisMonitor schedule
*/
public class RedisMonitor {
private static Timer timer = new Timer();
private static String master_ip = "";
private static int master_port = 0;
private static String slave_ip = "";
private static int slave_port = 0;
private static int maxActive = 0;
private static int maxIdle = 0;
private static int maxWait = 0;
private static JedisPool jedisWritePool;
static {
InputStream is = RedisMonitor.class
.getResourceAsStream("/monconfig.properties");
Properties props = new Properties();
try {
props.load(is);
master_ip = props.getProperty("master.ip");
master_port = Integer.parseInt(props.getProperty("master.port"));
slave_ip = props.getProperty("slave.ip");
slave_port = Integer.parseInt(props.getProperty("slave.port"));
maxActive = Integer.parseInt(props.getProperty("maxActive"));
maxIdle = Integer.parseInt(props.getProperty("maxIdle"));
maxWait = Integer.parseInt(props.getProperty("maxWait"));
} catch (Exception e) {
System.out.println("monconfig redisconfig error");
}
if (null == master_ip || "".equals(master_ip) || 0 == master_port
|| 0 == slave_port || 0 == maxActive || 0 == maxIdle
|| 0 == maxWait || null == slave_ip || "".equals(slave_ip)) {
System.out.println("monconfig redisconfig error");
} else {
checkRedisMaster();
try {
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println(start());
}
}, 5000, 5000);
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
}
public static boolean start() {
//优先检测master 然后再坚持slave
return checkSlave();
}
/**
* 启动写redis服务
*/
public static boolean checkRedisMaster() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(false);
try {
if (jedisWritePool == null) {
jedisWritePool = new JedisPool(config,master_ip,master_port);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 获取写Jedis实例
*
*/
public static Jedis getWriteJedis() {
try {
if (jedisWritePool != null) {
Jedis resource = jedisWritePool.getResource();
return resource;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 查看redis的状态信息
* @return
*/
public static String getRedisStatus() {
Jedis jedis = null;
try {
jedis = getWriteJedis() ;
return jedis.info() ;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
returnWriteResource(jedis) ;
}
}
/**
* 释放jedis资源
*/
public static void returnWriteResource(final Jedis jedis) {
if (jedis != null) {
jedisWritePool.returnResource(jedis);
}
}
public static boolean checkSlave(){
String redisInfo = getRedisStatus() ;
try{
String[] array = redisInfo.split("# ") ;
String replication = array[6];
String[] rArray = replication.split("\n") ;
Map m = new HashMap() ;
for (String item : rArray) {
int index = item.indexOf(":") ;
if (index != -1) {
String[] value = item.split(":") ;
m.put(value[0].trim(), value[1].trim()) ;
}
}
//slave与配置的个数一致则正常 否则 失败
if(Integer.parseInt(String.valueOf(m.get("connected_slaves"))) == slave_ip.split(",").length){
System.out.println("redis_slave_total:"+slave_ip.split(",").length +" connected_slaves:"+String.valueOf(m.get("connected_slaves")));
return true;
}else{
System.out.println("redis_slave_total:"+slave_ip.split(",").length +" connected_slaves:"+String.valueOf(m.get("connected_slaves")));
return false;
}
}catch (Exception e) {
System.out.println("redis master down");
return false;
}
}
}