Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy redis distributed clusters

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Redis cluster architecture diagram

The above figure shows the nodes of the redis cluster in blue.

Nodes use the ping command to test whether the connection is normal, there is no main distinction between nodes, and when connected to any node for operation, it may be forwarded to other nodes.

1. Fault-tolerant mechanism of Redis

The nodes will regularly send ping commands to each other to test the health status of the nodes. When the nodes receive the ping command, they will return a pong string.

Voting mechanism: if a node A sends ping to node B and does not get a pong return, it will notify other nodes to send ping to B again, if more than half of the nodes in the cluster do not receive the pong of node B. Then I think node B is dead. Generally, a backup node is provided for each node, and if it is hung up, it will be switched to the backup node.

2. Redis cluster storage principle.

Redis performs a hash operation for each stored key to generate a hash value of [0-16384] (first

The crc algorithm takes the remainder of 16384.

In the case of clusters, the intervals of [0-16384] are split and put into different redis.

3. Persistence of Redis

Snapshotting: regularly save the data in Redis memory to the hard disk

AOF: save all command operations to aof, AOP synchronization frequency is very high, even if the data is lost, the granularity is very small, but it will have an impact on performance.

Second, the construction of cluster environment

The redis cluster management tool redis-trib.rb depends on the ruby environment. First, you need to install the ruby environment.

Install ruby

Yum install rubyyum install rubygems

Install the interface program for ruby and redis

Copy redis-3.0.0.gem to / usr/local

Execute:

Gem install / usr/local/redis-3.0.0.gem

Third, create Redis cluster

On one server, different redis servers can be represented by different port numbers.

The Redis cluster requires at least three servers, and each server needs a backup server, so a minimum of six servers are needed. The port plan is as follows:

Main server: 192.168.100.66: 7001: 7002: 7003

From server: 192.168.100.66: 7004: 7005: 7006

Create a folder in / usr/local to store server programs

Mkdir 7001 7002 7003 7004 7005 7006

If you want redis to support the cluster, you need to modify the cluster-enabled yes of the redis.config configuration file

In this example, we distinguish different redis services by port, so we also need to modify the port of redis.config to the corresponding port.

After modifying the configuration file, copy the bin of the redis installation directory to each of the above directories.

Enter 7001/bin/ 7002/bin.

Start the service. / redis-server. / redis.conf

View the redis process: ps-aux | grep redis indicates that the startup is successful as shown in the following figure

Create a cluster:

Copy the redis-3.0.0/src/redis-trib.rb of the previously extracted folder to the redis-cluster directory

Running

. / redis-trib.rb create-- replicas 1 192.168.100.66 7001 192.168.100.66 Vera 7002 192.168.100.66 Vera 7003 192.168.100.66 Vera 7004 192.168.100.66 Vera 7005 192.168.100.66 Vera 7006

If you execute the Times with the following error:

[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

The solution is to delete the generated configuration file nodes.conf. If not, the node created now includes the node information of the old cluster. You need to delete the persistence file of redis and restart redis, such as appendonly.aof and dump.rdb.

If successful, the final input is as follows:

Query cluster information:

Description:

. / redis-cli-c-h 192.168.101.3-p 7001, where-c indicates connecting to redis,-h in cluster mode, specifies ip address, and-p specifies port number

Cluster nodes queries cluster node information

Cluster info queries cluster status information

Hash slot reallocation

Step 1: connect to the cluster

. / redis-trib.rb reshard 192.168.101.3 virtual 7001 (connect any of the available nodes in the cluster)

Step 2: enter the number of slots to allocate

Enter 500 to allocate 500 slots

Step 3: enter the node id of the receiving slot

Here we are going to allocate the slot to 7007. Check the 7007 node through cluster nodes that the id is 15b809eadae88955e36bcdbb8144f61bbbaf38fb.

Input: 15b809eadae88955e36bcdbb8144f61bbbaf38fb

Step 4: enter the source node id

Enter all here

Step 5: enter yes to start moving the slot to the target node id

Add slave node

After the cluster is created successfully, you can add a node to the cluster. Here is to add a slave slave node.

Add 7008 slave nodes and take 7008 as the 7007 slave node.

. / redis-trib.rb add-node-- slave-- master-id Primary Node id add Node's ip and Port Cluster already exist Node ip and Port

Execute the following command:

. / redis-trib.rb add-node-- slave-- master-id cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 192.168.101.3 master-id cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 7008 192.168.101.3

Cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 is the id of 7007 nodes, which can be viewed through cluster nodes.

Note: if the original configuration information of this node in the cluster has been generated in the configuration file specified by cluster-config-file (if cluster-config-file is not specified, the default is nodes.conf), an error may be reported:

[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

The solution is to delete the generated configuration file nodes.conf, delete it and then execute the. / redis-trib.rb add-node instruction

Take a look at the nodes in the cluster. The 7008 slave node you just added is 7007:

Delete a node:

. / redis-trib.rb del-node 127.0.0.1:7005 4b45eb75c8b428fbd77ab979b85080146a9bc017

Deleting a node that already occupies a hash slot will fail. The error is as follows:

[ERR] Node 127.0.0.1:7005 is not empty! Reshard data away and try again.

The hash slots occupied by this node need to be allocated (see the hash slot reallocation section).

Test:

Maven: redis.clients jedis 2.7.0 junit junit 4.12 test org.springframework spring-test 4.3.10.RELEASE test

General test:

@ Testpublic void redisClusterTest1 () {JedisPoolConfig config=new JedisPoolConfig (); config.setMaxTotal (30); config.setMaxIdle (2); Set jedisNode=new HashSet (); jedisNode.add (new HostAndPort ("192.168.100.66", 7001)); jedisNode.add (new HostAndPort ("192.168.100.66", 7002)); jedisNode.add ("192.168.100.66", 7003)) JedisNode.add (new HostAndPort ("192.168.100.66", 7004); jedisNode.add (new HostAndPort ("192.168.100.66", 7005)); jedisNode.add (new HostAndPort ("192.168.100.66", 7006)); JedisCluster jc=new JedisCluster (jedisNode,config); jc.set ("name", "Lao Wang"); String value=jc.get ("name"); System.out.println (value);}

Spring Test:

Configuration file:

Test class:

@ RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration ({"classpath:spring-config.xml"}) public class RedisClusterTest {@ Autowired private JedisCluster jedisCluster; @ Test public void redisClusterTest2 () {jedisCluster.set ("username", "Xiao Ming la la"); String name=jedisCluster.get ("username"); System.out.println (name);}}

These are the details of the redis distributed cluster building introduction, please pay more attention to other related articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report