redis3.0 Cluster actual combat 3-java programming combat

Source: Internet
Author: User
Tags redis cluster

This article mainly describes the use of Jedis for redis-cluster operations

Jedis

Jedis is a redis-recommended Java Redis Client, the GitHub address is, Https://github.com/xetorthio/jedis, this article uses Jedis for Redis cluster operations.
Jedis support for Redis cluster since 2.3.0, but look at the official released documentation (https://github.com/xetorthio/jedis/ Releases) in 2.3.0, each version will fix some cluster-related bugs, basically after the 2.6.3 version, only started without cluster related bugs, so it is recommended that the Jedis version is at least greater than 2.6.3. The latest version is 2.7.2.

Maven:

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.7.2</version><type>jar</type><scope>compile</scope></dependency>
Hello World

Jedis's official recommended HelloWorld:

Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();//Jedis Cluster will attempt to discover cluster nodes automaticallyjedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));JedisCluster jc = new JedisCluster(jedisClusterNodes);jc.set("foo", "bar");String value = jc.get("foo");

Actual use:

private void genClusterNode() {    clusterNodes = new HashSet<HostAndPort>();    clusterNodes.add(new HostAndPort("192.168.137.10", 7000));    clusterNodes.add(new HostAndPort("192.168.137.10", 7001));    clusterNodes.add(new HostAndPort("192.168.137.10", 7002));    clusterNodes.add(new HostAndPort("192.168.137.10", 7003));    clusterNodes.add(new HostAndPort("192.168.137.10", 7004));    clusterNodes.add(new HostAndPort("192.168.137.10", 7005));}private void genJedisConfig() {    config = new JedisPoolConfig();    config.setMaxTotal(1000);    config.setMaxIdle(100);    config.setTestOnBorrow(true);}public void clusterInit() {    genClusterNode();    genJedisConfig();    jedisCluster = new JedisCluster(clusterNodes, 5000, config);}private void clusterSetKey(String key, String value) {    jedisCluster.set(key, value);}
Interpretation

The Jediscluster is a class of Jedis encapsulated for Redis cluster operations.
There are two member variables in Jediscluster: Maxredirections and Connectionhandler.

maxRedirections:     - 重试次数,在执行失败后,进行的重试次数,默认是5,connectionHandler:    - cluster里面的连接管理者    - 包括JedisClusterInfoCache,里面会缓存nodes和slots的信息,同时对于各个nodes和slots会缓存一个JedisPool

Therefore, when invoking the Rediscluster object, you can use the Jediscluster object directly, such as when calling Jediscluster.set:

- JedisCluster会从connectionHandler基于key找出对应的SLOTS和NODES,并找出对应的JedisPool对象- 从JedisPool对象中找出可用的Jedis实例,执行Jedis对应的set操作

At the same time, in the new Jediscluster, you can also pass the timeout parameter, where timeout indicates the time-out of reading data from Redis-server, the default is 2s, when the amount of data is large, you can consider increasing the timeout.

redis3.0 Cluster actual combat 3-java programming combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.