1. 程式人生 > >JAVA之Jedis 對 Redis客戶端分散式與節點叢集兩者的區別

JAVA之Jedis 對 Redis客戶端分散式與節點叢集兩者的區別

公司專案需要做web端和安卓端:web端使用ehcache做快取,安卓端使用redis來存放token和使用者登入後產生的資訊(相當於session的功能);因為專案屬於雲平臺,資料會比較多,所以單機存放壓力有點大,所以現在構建的專案暫時先做橫向的redis擴充套件;即用多臺伺服器存放資料,為了防止資料丟失,再搭建redis主從的方式。後面有需要在搭建伺服器端redisclusd叢集。

1.客戶端分散式redis:

啥叫橫向擴充套件的redis呢?就是分散式來存資料:即使用者的資料,根據前臺生成鍵的不同,隨機存到不同的伺服器上,存的資料只有一份。當然為了防止資料丟失,可以為每臺主伺服器,另外搭建從伺服器。

模擬搭建:下載redis2.6或2.8吧:分成若干個目錄,改配置檔案:埠務必不同,然後最好製作一個指令碼檔案,到時直接執行指令碼檔案,就可以啟動所有的redis伺服器。

然後程式碼咋寫呢?在單個埠的時候我們常會建立JedisPool,Jedis這樣的,那麼多埠的時候就使用ShardedJedisPool,ShardedJedis;其他程式碼幾乎不變,主要是物件改變了,而且多個介面的時候,需要把介面組合起來;

單介面下的程式碼():

public class RedisPool {
	 private static JedisPool pool = null;  
     
	    /** 
	     * 構建redis連線池 
	     *  
	     * @param ip 
	     * @param port 
	     * @return JedisPool 
	     */  
	    public static JedisPool getPool() {  
	        if (pool == null) {  
	            JedisPoolConfig config = new JedisPoolConfig();  
	            //控制一個pool可分配多少個jedis例項,通過pool.getResource()來獲取;  
	            //如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis例項,則此時pool的狀態為exhausted(耗盡)。  
	            config.setMaxActive(500);  
	            //控制一個pool最多有多少個狀態為idle(空閒的)的jedis例項。  
	            config.setMaxIdle(5);  
	            //表示當borrow(引入)一個jedis例項時,最大的等待時間,如果超過等待時間,則直接丟擲JedisConnectionException;  
	            config.setMaxWait(1000 * 100);  
	            //在borrow一個jedis例項時,是否提前進行validate操作;如果為true,則得到的jedis例項均是可用的;  
	            config.setTestOnBorrow(true);  
	            pool = new JedisPool(config, "localhost",6379);  
	        }  
	        return pool;  
	    }  
	      
	 
	    public static void set(String key, String value) {
	    	 
	        Jedis jedis = null;
	        JedisPool pool = null;
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            jedis.set(key, value);
	        } catch (Exception e) {
	            //釋放redis物件
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	    }
	      
	    /** 
	     * 獲取資料 
	     *  
	     * @param key 
	     * @return 
	     */  
	    @SuppressWarnings("deprecation")
		public static String get(String key){  
	        String value = null;  
	          
	        JedisPool pool = null;  
	        Jedis jedis = null;  
	        try {  
	            pool = getPool();  
	            jedis = pool.getResource();  
	            value = jedis.get(key);  
	        } catch (Exception e) {  
	            //釋放redis物件  
	            pool.returnBrokenResource(jedis);  
	            e.printStackTrace();  
	        } finally {  
	            //返還到連線池  
	            close(pool,jedis);  
	        }  
	          
	        return value;  
	    }  
	    
	    /**
	     * 
	     *author:cwy
	     *說明:
	     *引數:
	     * @param jedis
	     */
	    public static void close(JedisPool pool,Jedis jedis) {
	        try {
	        	if (jedis != null)
	        	{
	            pool.returnResource(jedis);
	        	}
	        } catch (Exception e) {
	            if (jedis.isConnected()) {
	                jedis.quit();
	                jedis.disconnect();
	            }
	        }
	    }
	    
	    
	 /**
	  * 
	  *author:cwy
	  *說明:設定雜湊鍵的段的值
	  *引數:
	  * @param key
	  * @param field
	  * @param value
	  */
	    public static void hset(String key, String field, String value) {
	        Jedis jedis = null;
	        JedisPool pool = null; 
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            jedis.hset(key, field, value);
	        } catch (Exception e) {
	            //釋放redis物件
	           pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	    }
	    
	    /**
	     * 獲取雜湊鍵中的某個段的值
	     * 
	     * @param key
	     * @return
	     */
	    public static String hget(String key, String field) {
	 
	        String value = null;
	        Jedis jedis = null;
	        JedisPool pool = null; 
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            value = jedis.hget(key, field);
	        } catch (Exception e) {
	            //釋放redis物件
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	 
	        return value;
	    }
	    
	    /**
	     * 
	     *author:cwy
	     *說明:刪除某個雜湊鍵中的某個段值
	     *引數:
	     * @param key
	     * @param field
	     */
	    public static void hdel(String key, String field) {
	    	 
	        Jedis jedis = null;
	        JedisPool pool = null; 
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            jedis.hdel(key, field);
	        } catch (Exception e) {
	            //釋放redis物件
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	    }
	    
	    /**
	     * 
	     *author:cwy
	     *說明:刪除鍵
	     *引數:
	     * @param key
	     */
	    public static void del(String key) {
	    	 
	        Jedis jedis = null;
	        JedisPool pool = null; 
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            jedis.del(key);
	        } catch (Exception e) {
	            //釋放redis物件
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	    }
	    
	    /**
	     * 
	     *author:cwy
	     *說明:為鍵設定時間
	     *引數:
	     * @param key
	     * @param seconds
	     */
	    public static void expire(String key, int seconds) {
	    	  Jedis jedis = null;
		        JedisPool pool = null; 
			if (seconds <= 0) { 
				return;
			}
			pool = getPool();
            jedis = pool.getResource();
			
			jedis.expire(key, seconds);
			  close(pool,jedis);
		}
}
多埠使用的程式碼:
public class ShareRedisPool {
	 private static ShardedJedisPool pool = null;  
     
	    /** 
	     * 構建redis連線池 
	     *  
	     * @param ip 
	     * @param port 
	     * @return JedisPool 
	     */  
	    public static ShardedJedisPool getPool() {  
	        if (pool == null) {  
	            JedisPoolConfig config = new JedisPoolConfig();  
	            //控制一個pool可分配多少個jedis例項,通過pool.getResource()來獲取;  
	            //如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis例項,則此時pool的狀態為exhausted(耗盡)。  
	            config.setMaxActive(500);  
	            //控制一個pool最多有多少個狀態為idle(空閒的)的jedis例項。  
	            config.setMaxIdle(5);  
	            //表示當borrow(引入)一個jedis例項時,最大的等待時間,如果超過等待時間,則直接丟擲JedisConnectionException;  
	            config.setMaxWait(1000 * 100);  
	            //在borrow一個jedis例項時,是否提前進行validate操作;如果為true,則得到的jedis例項均是可用的;  
	            config.setTestOnBorrow(true);  
	            
	            String host = "127.0.0.1";
	            JedisShardInfo shardInfo1 = new JedisShardInfo(host, 6379, 500);
	           // shardInfo1.setPassword("test123");
	            JedisShardInfo shardInfo2 = new JedisShardInfo(host, 6380, 500);
	           // shardInfo2.setPassword("test123");
	            JedisShardInfo shardInfo3 = new JedisShardInfo(host, 6381, 500);
	           // shardInfo3.setPassword("test123");
	            
	            List<JedisShardInfo> infoList = Arrays.asList(shardInfo1, shardInfo2, shardInfo3);
	            pool = new ShardedJedisPool(config, infoList);
	        }  
	        return pool;  
	    }  
	      
	 
	    public static void set(String key, String value) {
	    	 
	    	ShardedJedis jedis = null;
	        ShardedJedisPool pool = null;
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            jedis.set(key, value);
	        } catch (Exception e) {
	            //釋放redis物件
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	    }
	      
	    /** 
	     * 獲取資料 
	     *  
	     * @param key 
	     * @return 
	     */  
	    @SuppressWarnings("deprecation")
		public static String get(String key){  
	        String value = null;  
	          
	        ShardedJedisPool pool = null;  
	        ShardedJedis jedis = null;  
	        try {  
	            pool = getPool();  
	            jedis = pool.getResource();  
	            value = jedis.get(key);  
	        } catch (Exception e) {  
	            //釋放redis物件  
	            pool.returnBrokenResource(jedis);  
	            e.printStackTrace();  
	        } finally {  
	            //返還到連線池  
	            close(pool,jedis);  
	        }  
	          
	        return value;  
	    }  
	    
	    /**
	     * 
	     *author:cwy
	     *說明:
	     *引數:
	     * @param jedis
	     */
	    public static void close(ShardedJedisPool pool,ShardedJedis jedis) {
	        try {
	        	if (jedis != null)
	        	{
	            pool.returnResource(jedis);
	        	}
	        } catch (Exception e) {
	          
	                jedis.disconnect();
	            
	        }
	    }
	    
	    public static void  main(String args[])
	    {
	    	set("two","nima");
	    	System.out.print(get("two"));
	    }
	    
	 /**
	  * 
	  *author:cwy
	  *說明:設定雜湊鍵的段的值
	  *引數:
	  * @param key
	  * @param field
	  * @param value
	  */
	    public static void hset(String key, String field, String value) {
	    	ShardedJedis jedis = null;
	        ShardedJedisPool pool = null; 
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            jedis.hset(key, field, value);
	        } catch (Exception e) {
	            //釋放redis物件
	           pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	    }
	    
	    /**
	     * 獲取雜湊鍵中的某個段的值
	     * 
	     * @param key
	     * @return
	     */
	    public static String hget(String key, String field) {
	 
	        String value = null;
	        ShardedJedis jedis = null;
	        ShardedJedisPool pool = null; 
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            value = jedis.hget(key, field);
	        } catch (Exception e) {
	            //釋放redis物件
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	 
	        return value;
	    }
	    
	    /**
	     * 
	     *author:cwy
	     *說明:刪除某個雜湊鍵中的某個段值
	     *引數:
	     * @param key
	     * @param field
	     */
	    public static void hdel(String key, String field) {
	    	 
	    	ShardedJedis jedis = null;
	    	ShardedJedisPool pool = null; 
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            jedis.hdel(key, field);
	        } catch (Exception e) {
	            //釋放redis物件
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	    }
	    
	    /**
	     * 
	     *author:cwy
	     *說明:刪除鍵
	     *引數:
	     * @param key
	     */
	    public static void del(String key) {
	    	 
	    	ShardedJedis jedis = null;
	    	ShardedJedisPool pool = null; 
	        try {
	        	pool = getPool();
	            jedis = pool.getResource();
	            jedis.del(key);
	        } catch (Exception e) {
	            //釋放redis物件
	            pool.returnBrokenResource(jedis);
	            e.printStackTrace();
	        } finally {
	            //返還到連線池
	            close(pool,jedis);
	        }
	    }
	    
	    /**
	     * 
	     *author:cwy
	     *說明:為鍵設定時間
	     *引數:
	     * @param key
	     * @param seconds
	     */
	    public static void expire(String key, int seconds) {
	    	ShardedJedis jedis = null;
	    	ShardedJedisPool pool = null; 
			if (seconds <= 0) { 
				return;
			}
			pool = getPool();
         jedis = pool.getResource();
			
			jedis.expire(key, seconds);
			  close(pool,jedis);
		}
}