High availability test of Redis cluster (including the use of Jedis client)

Use test of Redis cluster (use of Jedis client)
1. It
is recommended to upgrade the Jedis client to the latest version (currently 2.7.3), which has better support for the 3.0.x cluster.
https://github.com/xetorthio/jedis
http://mvnrepository.com/artifact/redis.clients/jedis

2.
Link Redis cluster directly in Java code:
// Database link pool configuration
JedisPoolConfig config = new JedisPoolConfig() ;
config.setMaxTotal(100);
config.setMaxIdle(50);
config.setMinIdle(20);
config.setMaxWaitMillis(6 * 1000);
config.setTestOnBorrow(true);
// Node collection of Redis cluster
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("192.168.1.111", 7111));
jedisClusterNodes.add(new HostAndPort("192.168.1.112", 7112));
jedisClusterNodes.add(new HostAndPort("192.168.1.113", 7113));
jedisClusterNodes.add(new HostAndPort("192.168.1.114", 7114));
jedisClusterNodes.add(new HostAndPort("192.168.1.115", 7115)) ;
jedisClusterNodes.add(new HostAndPort("192.168.1.116", 7116));
// Create a cluster link object according to the node
//JedisCluster jedisCluster = newJedisCluster(jedisClusterNodes);
// Node, timeout, maximum number of redirects, link Pool
JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, 2000, 100, config);
int num = 1000;
String key = "wusc";
String value = "";
for (int i=1; i <= num; i++){

/ / Save data
jedisCluster.set(key+i,"WuShuicheng"+i);

// Get data
value= jedisCluster.get(key+i);

http://log.info(key+i + "=" + value);

// delete data

//jedisCluster.del(key+i);

//value = jedisCluster.get(key+i);

//http ://log.info(key+i + "=" + value);
}
3. Spring configuration Jedis link Redis3.0 cluster configuration:

<!-- Jedis link pool configuration, note: Jedis version is recommended to upgrade to the latest ( The current latest version is 2.7.2) -->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

<property name="maxTotal" value="100"/>

<property name="maxIdle " value="20"/>

<property name="minIdle" value="10"/>

<property name="blockWhenExhausted" value="true"></property>

<property name="maxWaitMillis" value="3000" />

<property name="testOnBorrow" value="false" />

<property name="testOnReturn" value="false" />

<property name="testWhileIdle" value="true" />

<property name="minEvictableIdleTimeMillis" value="60000" />

<property name="timeBetweenEvictionRunsMillis" value="30000" />

<property name="numTestsPerEvictionRun" value="-1" />

</bean>

<!-- JedisCluster -->

<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">

<constructor-arg index="0">

<set>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg index="0" value="192.168.1.111"/>

<constructor-arg index="1" value="7111"type="int" />

</bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg index="0" value="192.168.1.112"/>

<constructor-arg index="1" value="7112"type="int" />

</bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg index="0" value="192.168.1.113"/>

<constructor-arg index="1" value="7113"type="int" />

</bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg index="0" value="192.168.1.114"/>

<constructor-arg index="1" value="7114"type="int" />

</bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg index="0" value="192.168.1.115"/>

<constructor-arg index="1" value="7115"type="int" />

</bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg index="0" value="192.168.1.116"/>

<constructor-arg index="1" value="7116"type="int" />

</bean>

</set>

</constructor-arg>

<constructor-arg index="1" value="2000"type="int"></constructor-arg>
<constructor-arg index="2"value="100" type="int"></constructor-arg>

<constructor-arg index="3" ref="jedisPoolConfig"></constructor-arg>

</bean>

对应的Java调用代码样例
JedisCluster jedisCluster = (JedisCluster)context.getBean("jedisCluster");
int num = 1000;
String key = "wusc";
String value = "";
for (int i=1; i <= num;i++){ value= jedisCluster.get(key+i); // get data jedisCluster.set(key+i,"WuShuicheng"+i);

// save data





http://log.info(key+i + "=" + value);

// delete data//

jedisCluster.del(key+i);
}

High availability
test of Redis cluster 1. Redis cluster features
1. Cluster architecture Features:
(1) All redis nodes are interconnected with each other (PING-PONG mechanism), and the binary protocol is used internally to optimize transmission speed and bandwidth;
(2) The fail of a node takes effect only when more than half of the nodes in the cluster fail to detect;
(3) ) The client is directly connected to the redis node, and no intermediate proxy layer is required. The client does not need to connect to all nodes in the cluster, just connect to any available node in the cluster;
(4) redis-cluster maps all physical nodes to [0-16383] slots (hash slots), and the cluster is responsible for maintaining the
nodes <->slot<->value .

2. Cluster election fault tolerance:
(1) The node failure election process involves the participation of all masters in the cluster. If more than half of the master nodes communicate with the currently detected master node and the detection times out (cluster-node-timeout), the current master node is considered to be hung up;
(2): When is the entire cluster unavailable (cluster_state: fail)?
A: If any master in the cluster hangs, and the current master has no slave. The cluster enters the fail state, which can also be understood as entering the fail state when the cluster's slot map [0-16383] is incomplete. ps : redis-3.0.0.rc1 adds the cluster-require-full-coverage parameter, which is turned off by default, and fails to open the cluster compatibility part;
B: If more than half of the masters in the cluster fail, no matter whether the slave cluster enters the fail state. ps: When the cluster is unavailable, all operations on the cluster are unavailable, and the ((error) CLUSTERDOWN The cluster is down) error is received.

Second, the client cluster command
Cluster
cluster info: print the information of the
cluster cluster nodes: list all the nodes (nodes) currently known to the cluster, as well as the related information of these nodes.
Node
cluster meet<ip> <port> : Add the node specified by ip and port to the cluster and make it a part of the cluster.
cluster forget<node_id>: Removes the node specified by node_id from the cluster.
clusterreplicate <node_id>: Set the current node as the slave node of the node specified by node_id.
clustersaveconfig: Save the configuration file of the node to the hard disk.
Slot
cluster addslots<slot> [slot ...]: Assign one or more slots to the current node.
clusterdelslots <slot> [slot ...]: Removes one or more slot assignments to the current node.
clusterflushslots: Removes all slots assigned to the current node, making the current node a node with no slots assigned.
cluster setslot<slot> node <node_id>: Assign the slot to the node specified by node_id. If the slot has been assigned to another node, let the other node delete the slot>, and then assign it.
cluster setslot<slot> migrating <node_id>: Migrate the slot of this node to the node specified by node_id.
cluster setslot<slot> importing <node_id>: Import the slot from the node specified by node_id to this node.
cluster setslot<slot> stable: Cancel the import or migration of the slot.
key
cluster keyslot<key>: Calculates the slot on which the key key should be placed.
clustercountkeysinslot <slot>: Returns the number of key-value pairs currently contained in the slot.
clustergetkeysinslot <slot> <count> : Returns the keys in count slots.

3. Cluster high availability test
1. To rebuild the cluster, steps:
(1) Shut down each node of the cluster;
(2) Delete nodes.conf, appendonly.aof, dump.rdb in the data directory of each node;
# rm -rf appendonly.aof| rm -rf dump.rdb | rm -rf nodes.conf
(3) Re-enable all nodes
192.168.1.111
# /usr/local/redis3/bin/redis-server/usr/local/ redis3/cluster/7111/redis-7111.conf
192.168.1.112
# /usr/local/redis3/bin/redis-server/usr/local/redis3/cluster/7112/redis-7112.conf
192.168.1.113
# /usr/ local/redis3/bin/redis-server/usr/local/redis3/cluster/7113/redis-7113.conf
192.168.1.114
# /usr/local/redis3/bin/redis-server/usr/local/redis3/cluster/ 7114/redis-7114.conf
192.168.1.115
# /usr/local/redis3/bin/redis-server/usr/local/redis3/cluster/7115/redis-7115.conf
192.168.1.116
# /usr/local/redis3/ bin/redis-server/usr/local/redis3/cluster/7116/redis-7116.conf
(4) Execute the cluster creation command (you only need to execute it once on one of the nodes)
# cd/usr/local/src/redis-3.0.3/src/
# cpredis-trib.rb /usr/local/bin/ redis- trib # redis-tribcreate --replicas 1192.168.1.114:7114
192.168.1.115:7115 192.168.1.116:7116 192.168.1.111:7111192.168.1.112:7112 192.168.1.113:713

View the status of each node in the current cluster
@edu-redis-01 7111]# /usr/local/redis3/bin/redis-cli -c -p 7111
127.0.0.1:7111> cluster nodes

3. Use the demo application to write 1000 key-value data to the cluster
using / usr/local/redis3/bin/redis-cli-c -p 711X command to log in to each node, use keys * to view all keys of each node

4. Run the demo application to get all the key value data
If there is a null value, stop

5, Simulate cluster node downtime (to achieve failover)
(1) The Jedis client operates the cluster data cyclically (simulates the user's continuous use of the system)
(2) Views the current state of the Redis cluster (for the next comparison of node state changes)
(3) Close One of the master nodes (7111)


(4) Observe the status changes of the master node and the corresponding slave node
Node status fail? Indicates that it is judging whether it has failed.

Node status fail indicates that the node has failed, and the corresponding slave node is promoted to master

(5) Then check the cluster status change# /usr/local/src/redis-3.0.3/src/redis-trib .rbcheck 192.168.1.116:7116
can be seen from the above, the 7114 node replaces 7111, and the slave becomes the master.
At this time, the demo application is executed to obtain all the key-value data, which is still normal, indicating that the slave has successfully replaced the master and the cluster is normal.

6. Restore the fail node
(1) Start 7111
# /usr/local/redis3/bin/redis-server/usr/local/redis3/cluster/7111/redis-7111.conf
(2) Check the cluster status
where 7111 becomes 7114

7. Observe the impact on clients during the switching of cluster nodes
. Several common exceptions encountered when JedisCluster links Redis cluster operations: (
1) Too many redirections
redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Clusters Redirections ?
Solution: When initializing JedisCluster, set maxRedirections of JedisCluster
//set of nodes in the cluster, timeout (default 2 seconds), maximum redirection times (default 5), link pool
newJedisCluster(jedisClusterNodes, 2000, 100,config);
(2) The cluster cannot be used
redis.clients.jedis.exceptions.JedisClusterException: CLUSTERDOWN The cluster is down
Cause: There will be a temporary flash during the cluster node state switch, and the client will restart You can try it out.
(3) Connection timeout
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
Solution: When initializing JedisCluster, set the timeout of JedisCluster (the default is two seconds); you can also modify the default value in the source code time.


7. Summary:
Advantages:

After the master node goes offline, the slave node will be automatically promoted to the master node, saving the cluster to continue to provide services;
after the fail node is restored, it will be automatically added to the cluster and become a slave node;
Disadvantages:

due to redis Replication uses an asynchronous mechanism, and during automatic failover, the cluster may lose write commands. However, redis performs these two operations (sending command recovery to the client, and copying the command to the slave node) almost at the same time, so in practice, the window for command loss is very small.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326364956&siteId=291194637