jedis针对三种redis工作模式的连接方式
2016-07-14 17:31
477 查看
这里说的三种工作模式是指:
1、单机模式
2、分片模式
3、集群模式(since 3.0)
说明图详见以下:
使用jedis连接模式一:
//初始化连接private Jedis jedis=new Jedis("192.168.0.100", 6379);
jedis.set("a","b");
jedis.hmset(key, hash);
……
使用jedis连接模式二:
GenericObjectPoolConfig config=new GenericObjectPoolConfig();
config.setMaxIdle(32);
config.setMinIdle(12);
config.setTestOnBorrow(true);
config.setTestOnReturn(rtrue);
config.setTestWhileIdle(true);
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
for (int i = 0; i < shareds.size(); i++) {
shards.add(new JedisShardInfo("192.168.0.100", 6379, 200));
}
// 构造池
ShardedJedisPool shardedJedisPool= new ShardedJedisPool(config, shards);
ShardedJedis jedis=shardedJedisPool.getResource();
jedis.set("a","b");
jedis.hmset(key, hash);
……
使用jedis连接模式三:
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
for (int i = 0; i < shareds.size(); i++) {
jedisClusterNodes.add(new HostAndPort("192.168.0.100",6379));
}
// 构造池
BinaryJedisCluster cluster= new BinaryJedisCluster(jedisClusterNodes);
cluster.set("a","b");
cluster.hmset(key, hash);
……
BinaryJedisCluster.java是为了让jedis支持byte数组形式value重写的一个类,参考网上文档,记不得来源了。以下是代码(部分):
public class BinaryJedisCluster extends JedisCluster {
public String set(final String key, final byte[] value) {
return new JedisClusterCommand<String>(connectionHandler, timeout,
maxRedirections) {
public String execute(Jedis connection) {
return connection.set(SafeEncoder.encode(key), value);
}
}.run(key);
}
public byte[] getBytes(final String key) {
return new JedisClusterCommand<byte[]>(connectionHandler, timeout,
maxRedirections) {
public byte[] execute(Jedis connection) {
return connection.get(SafeEncoder.encode(key));
}
}.run(key);
}
public Set<byte[]> zrevrangeByScoreBytes(final String key, final double max,
final double min, final int offset, final int count) {
return new JedisClusterCommand<Set<byte[]>>(connectionHandler, timeout,
maxRedirections) {
public Set<byte[]> execute(Jedis connection) {
return connection.zrevrangeByScore(SafeEncoder.encode(key),
max, min, offset, count);
}
}.run(key);
}
public Set<byte[]> zrevrangeByScoreBytes(final String key, final String max,
final String min, final int offset, final int count) {
return new JedisClusterCommand<Set<byte[]>>(connectionHandler, timeout,
maxRedirections) {
public Set<byte[]> execute(Jedis connection) {
return connection.zrevrangeByScore(SafeEncoder.encode(key),
SafeEncoder.encode(max), SafeEncoder.encode(min), offset, count);
}
}.run(key);
}
public Long linsert(final String key, final LIST_POSITION where,
final byte[] pivot, final byte[] value) {
return new JedisClusterCommand<Long>(connectionHandler, timeout,
maxRedirections) {
public Long execute(Jedis connection) {
return connection.linsert(SafeEncoder.encode(key), where,
pivot, value);
}
}.run(key);
}
public Long lpushx(final String key, final byte[]... string) {
return new JedisClusterCommand<Long>(connectionHandler, timeout,
maxRedirections) {
public Long execute(Jedis connection) {
return connection.lpushx(SafeEncoder.encode(key), string);
}
}.run(key);
}
public Long rpushx(final String key, final byte[]... string) {
return new JedisClusterCommand<Long>(connectionHandler, timeout,
maxRedirections) {
public Long execute(Jedis connection) {
return connection.rpushx(SafeEncoder.encode(key), string);
}
}.run(key);
}
public List<byte[]> blpopBytes(final String arg) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler,
timeout, maxRedirections) {
public List<byte[]> execute(Jedis connection) {
return connection.blpop(SafeEncoder.encode(arg));
}
}.run(null);
}
public List<byte[]> brpopBytes(final String arg) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler,
timeout, maxRedirections) {
public List<byte[]> execute(Jedis connection) {
return connection.brpop(SafeEncoder.encode(arg));
}
}.run(null);
}
}
编译后jedis jar包下载地址:http://pan.baidu.com/s/1mgIPYDa
源码地址:http://pan.baidu.com/s/1jGIobBo
1、单机模式
2、分片模式
3、集群模式(since 3.0)
说明图详见以下:
使用jedis连接模式一:
//初始化连接private Jedis jedis=new Jedis("192.168.0.100", 6379);
jedis.set("a","b");
jedis.hmset(key, hash);
……
使用jedis连接模式二:
GenericObjectPoolConfig config=new GenericObjectPoolConfig();
config.setMaxIdle(32);
config.setMinIdle(12);
config.setTestOnBorrow(true);
config.setTestOnReturn(rtrue);
config.setTestWhileIdle(true);
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
for (int i = 0; i < shareds.size(); i++) {
shards.add(new JedisShardInfo("192.168.0.100", 6379, 200));
}
// 构造池
ShardedJedisPool shardedJedisPool= new ShardedJedisPool(config, shards);
ShardedJedis jedis=shardedJedisPool.getResource();
jedis.set("a","b");
jedis.hmset(key, hash);
……
使用jedis连接模式三:
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
for (int i = 0; i < shareds.size(); i++) {
jedisClusterNodes.add(new HostAndPort("192.168.0.100",6379));
}
// 构造池
BinaryJedisCluster cluster= new BinaryJedisCluster(jedisClusterNodes);
cluster.set("a","b");
cluster.hmset(key, hash);
……
BinaryJedisCluster.java是为了让jedis支持byte数组形式value重写的一个类,参考网上文档,记不得来源了。以下是代码(部分):
public class BinaryJedisCluster extends JedisCluster {
public String set(final String key, final byte[] value) {
return new JedisClusterCommand<String>(connectionHandler, timeout,
maxRedirections) {
public String execute(Jedis connection) {
return connection.set(SafeEncoder.encode(key), value);
}
}.run(key);
}
public byte[] getBytes(final String key) {
return new JedisClusterCommand<byte[]>(connectionHandler, timeout,
maxRedirections) {
public byte[] execute(Jedis connection) {
return connection.get(SafeEncoder.encode(key));
}
}.run(key);
}
public Set<byte[]> zrevrangeByScoreBytes(final String key, final double max,
final double min, final int offset, final int count) {
return new JedisClusterCommand<Set<byte[]>>(connectionHandler, timeout,
maxRedirections) {
public Set<byte[]> execute(Jedis connection) {
return connection.zrevrangeByScore(SafeEncoder.encode(key),
max, min, offset, count);
}
}.run(key);
}
public Set<byte[]> zrevrangeByScoreBytes(final String key, final String max,
final String min, final int offset, final int count) {
return new JedisClusterCommand<Set<byte[]>>(connectionHandler, timeout,
maxRedirections) {
public Set<byte[]> execute(Jedis connection) {
return connection.zrevrangeByScore(SafeEncoder.encode(key),
SafeEncoder.encode(max), SafeEncoder.encode(min), offset, count);
}
}.run(key);
}
public Long linsert(final String key, final LIST_POSITION where,
final byte[] pivot, final byte[] value) {
return new JedisClusterCommand<Long>(connectionHandler, timeout,
maxRedirections) {
public Long execute(Jedis connection) {
return connection.linsert(SafeEncoder.encode(key), where,
pivot, value);
}
}.run(key);
}
public Long lpushx(final String key, final byte[]... string) {
return new JedisClusterCommand<Long>(connectionHandler, timeout,
maxRedirections) {
public Long execute(Jedis connection) {
return connection.lpushx(SafeEncoder.encode(key), string);
}
}.run(key);
}
public Long rpushx(final String key, final byte[]... string) {
return new JedisClusterCommand<Long>(connectionHandler, timeout,
maxRedirections) {
public Long execute(Jedis connection) {
return connection.rpushx(SafeEncoder.encode(key), string);
}
}.run(key);
}
public List<byte[]> blpopBytes(final String arg) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler,
timeout, maxRedirections) {
public List<byte[]> execute(Jedis connection) {
return connection.blpop(SafeEncoder.encode(arg));
}
}.run(null);
}
public List<byte[]> brpopBytes(final String arg) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler,
timeout, maxRedirections) {
public List<byte[]> execute(Jedis connection) {
return connection.brpop(SafeEncoder.encode(arg));
}
}.run(null);
}
}
编译后jedis jar包下载地址:http://pan.baidu.com/s/1mgIPYDa
源码地址:http://pan.baidu.com/s/1jGIobBo
相关文章推荐
- Redis学习笔记(十一)Redis-benchmark为Redis性能测试工具
- spark近实时流式计算开源平台
- ed exception is redis.clients.jedis.exceptions.JedisConnectionException: Unknown reply: O 异常处理记录
- Redis中的五种数据类型
- 查看错误信息
- Redis存储Java对象
- python实现redis客户端单例+hbase客户端单例
- Codis作者黄东旭细说分布式Redis架构设计和踩过的那些坑们
- redis 写入的时候报错
- redis 入mysql库
- redis集群一致性哈希--哈希槽
- Redis配置文件 翻译 V3.2版本
- redis
- composer install 报错(Install or enable PHP's redis extension)
- Redis命令总结
- springmvc mybatis redis mysql maven搭建基本开发框架 (二)
- redis配置及和spring的各种结合
- Redis学习手册(事务)
- 缓存技术Redis在C#中的使用及Redis的封装
- Redis集群部署与测试