您的位置:首页 > 数据库 > Redis

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

2016-12-11 01:25 477 查看
公司项目需要做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);
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐