jedis针对三种redis工作模式的连接方式

这里说的三种工作模式是指:

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

时间: 2024-12-26 17:56:10

jedis针对三种redis工作模式的连接方式的相关文章

LR12中针对WebServices协议的三种脚本开发模式

Loadrunner 脚本开发实战-webservice 协议 第一种: 使用:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 点击ok,生成函数: web_service_call(         "StepName=getWeatherbyCityName_101",  //步骤的名称        "SOAPMethod=WeatherWebService|WeatherWebServi

PDO 中支持三种错误处理模式:

PDO 支持三种错误处理模式: 静默模式,警告模式,异常模式 静默模式是默认的,需要修改的话,通过设置 PDO 对象的属性完成. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT)? $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING)?? $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT)?

Hive三种不同的数据导出的方式

转自:http://blog.chinaunix.net/uid-27177626-id-4653808.html Hive三种不同的数据导出的方式,根据导出的地方不一样,将这些方法分为三类:(1)导出到本地文件系统 (2)导出到HDFS (3)导出到hive的另一个表中 导出到本地文件系统 执行: hive> insert overwrite local directory '/root/student' > select * from student; 通过insert overwrite

三种基本网络加载图片方式

代码片段(6) [全屏查看所有代码] 1. [代码]普通加载网络方式     ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 public class NormalLoadPictrue {   

三种POST和GET的提交方式

向服务器提交数据有两种方式,post和get.两者的区别主要有三点,安全性.长度限制.数据结构.其中get请求安全性相比较而言较差,数据长度受浏览器地址栏限制,没有方法体.两种都是较为重要的数据提交方式.现简单介绍一下三种post和get的提交方式.无论是哪种方法实现post和get,get 的访问路径都要携带数据,而post提交是把数据放在方法体中. 普通方法实现get/post提交: 严格遵照Http协议进行数据传输.在安卓开发环境下,由于主线程不能进行网络访问,因此需要在开启一个子线程向服

Android三种基本的加载网络图片方式(转)

Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

【Unity3d游戏开发】浅谈UGUI中的Canvas以及三种画布渲染模式

阅读目录 一.Canvas简介 二.Canvas画布参数与应用 回到顶部 一.Canvas简介 Canvas画布是承载所有UI元素的区域.Canvas实际上是一个游戏对象上绑定了Canvas组件.所有的UI元素都必须是Canvas的自对象.如果场景中没有画布,那么我们创建任何一个UI元素,都会自动创建画布,并且将新元素置于其下. 回到顶部 二.Canvas画布参数与应用 1.创建画布 当你创建任何一个UI元素的时候,都会自动创建画布.也可以主动创建一张画布:点击GameObject->UI->

Redis三种集群模式-哨兵机制

Redis哨兵机制,一主二从 注:Redis哨兵切换,建议一主多从 一.一主二从 教程步骤:https://www.cnblogs.com/pinghengxing/p/11139997.html 二.哨兵配置(sentinel.conf) 哨兵机制也分单台以及集群,在此我们只构建哨兵集群      cd /usr/software/redis/redis-ms/ 1.创建哨兵目录 /usr/software/redis/redis-ms/sentinel/26001 /usr/software

初识LVS(三)——DR工作模式实际环境中的应用

背景介绍 LVS的NAT模型所有的请求和响应报文会经过director,此时director就成为了性能瓶颈,而为了解决这个问题LVS也默认采用的是DR模型(下图),请求报文(蓝色)向director发出请求,响应报文(红色)由RS服务器直接返还给用户不经由director,通常RS的网关都是指向IDC机房的出口路由,这样性能比通过director要好很多.VIP为公网IP地址,RIP和DIP需要连接在同一物理交换设备上,只要中间没有隔路由器,即使不是同一网段也可以,大多数情况RIP和DIP都位