jedis針對三種redis工作模式的串連方式

來源:互聯網
上載者:User

標籤:

這裡說的三種工作模式是指:

    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

jedis針對三種redis工作模式的串連方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.