jredis中实现Fail tolerance

redis支持master-slave模式,可以设置一个master,多个slave;采用读写分离的策略,master负责写数据,多个slave负责读数据,slave的数据是复制master上的,所以多个slave之间的数据是可以保持一致的,即便down了之后,也会通过加载dump数据的方式,重新加载会原来的缓存数据。

Fail-Tolerance
当前项目需要保证一个client连接到slave服务上读取数据,如果slave down 了之后,可以在不影响应用的情况下,自动切换到另外一个可用的slave

因为jredis不支持设置多个slave服务器,所以写了一个小工具类,目的是为了探测如果的当前正在使用的slave没有heartbeat之后,立马可以切换到一个可用的slave;这样可以保证one of slave down掉之后,不用missing-load也可及时切换到另外一个slave上读数据。

public class RedisFailToleranceUtil {

	private static JRedisService jredisService =  null;

	private static ConnectionSpec defaultConnectionSpec = null;
	
	private static int current = 1;
	
	private static HashMap<String,ConnectionSpec> serverPools = new HashMap<String,ConnectionSpec>();
	
	static{
		ConnectionSpec connectionSpec1 = DefaultConnectionSpec.newSpec("192.168.1.238", 6380, 0,null);
		ConnectionSpec connectionSpec2 = DefaultConnectionSpec.newSpec("192.168.1.238", 6381, 0,null);
		serverPools.put("1", connectionSpec1);
		serverPools.put("2", connectionSpec2);
	}
	
	private String next(){
		if(current>serverPools.size()){
			current=1;
		}
		int nextIndex = current;
		current++;
		return nextIndex+"";
	}
	
	private ConnectionSpec getConnectionSpec(){

		if(defaultConnectionSpec != null){
			return defaultConnectionSpec;
		}
		
		jredisService = null;
		/**
		 * we are working multiple servers
		 * try different servers,util we fetch the first available server pool
		 */
		HashMap<String,ConnectionSpec> tryServers = new HashMap<String,ConnectionSpec>(serverPools);
		if(serverPools.size() == 1){
			return (ConnectionSpec)serverPools.get("1");
		}
		
		while(!tryServers.isEmpty()){
			ConnectionSpec connectionSpec = tryServers.get(this.next());
			
			if(isConnect(connectionSpec)){
				return connectionSpec;
			}
			
			tryServers.remove(connectionSpec);
			if(tryServers.isEmpty()){
				break;
			}
		}
		
		return null;
	}
	
	/**
	 * try whether the server is available
	 * @param connectionSpec 
	 * @return true or false
	 */
	private boolean isConnect(ConnectionSpec connectionSpec){
		if(connectionSpec == null){
			return false;
		}
		
		JRedis jredis = new JRedisClient(connectionSpec.getAddress().getHostAddress(), connectionSpec.getPort());
		try{
			jredis.ping();
			jredis.quit();
		}catch(Exception e){
			return false;
		}
		return true;
	}
	
	public void initialize(){
		defaultConnectionSpec = this.getConnectionSpec();
		if(jredisService == null){
			synchronized (RedisFailToleranceUtil.class) {
				jredisService = new JRedisService(defaultConnectionSpec,100);
			}
		}
	}
	
	
	public String getS(String key){
		this.initialize();
		String value = null;
		try {
			value = DefaultCodec.toStr(jredisService.get(key));
		} catch (Exception e) {
			defaultConnectionSpec = null;
			this.initialize();
		}
		
		return value;
	}
	
	public static void main(String args[]){
		RedisFailToleranceUtil redis = new RedisFailToleranceUtil();
		redis.getS("name");
		
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		redis.getS("name");
		
		try {
			Thread.sleep(30000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		redis.getS("name");
	}
}

你可能感兴趣的:(redis,thread,应用服务器)