JAVA之Jedis 对 Redis客户端分布式与节点集群两者的区别

本文介绍了一种在项目中使用分布式Redis实现缓存扩展的方法,包括如何配置和使用ShardedJedisPool进行多节点数据存储及操作的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

公司项目需要做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);
		}
}




 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值