In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to build a horizontal cluster in Redis, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Overview:
Before the redis3.0 version, only master-slave replication (vertical cluster) was supported. Vertical clustering solves the read pressure, but the write operation can only be done on the master database. Redis3.0 version includes 3.0 to support horizontal clustering. This article attempts to introduce horizontal clustering of redis and how to build it in a real environment.
two。 The principle of implementation:
There are 16384 hash slots in the Redis cluster, and each key is part of the hash slot, so as long as you calculate the hash slot of key, you will know which of the 16384 key corresponds to. The calculation principle is that the CRC16 module 16384 of key is taken, and each node in the Redis cluster is responsible for the subset of the hash slot, dividing the hash slot into several slices as many nodes have. For example, there are three node clusters in A _ Magi B ~ C, and the allocation of hash slots is roughly as follows:
Node A contains hash slots from 0 to 5500.
Node B contains hash slots from 5501 to 11000.
Node C contains hash slots from 11001 to 16383.
For example, when my key is calculated by name, the hash slot is 100100, which is in node A, so the natural redis stores this value on the redis server of node A, and calculates the key hash slot first, and then goes to the corresponding node to obtain data, thus realizing the cluster for both reading and writing.
3. Set up horizontal clusters
Instance base environment: centos7 system, redis version 5.0.6 (current latest version)
Example: create 6 redis servers, 3 master libraries and 3 slave libraries, that is, each master database is assigned a slave database. If the master database fails, the slave database can be upgraded to master database by failover.
1. Download and install redis
# download redis installation package to / usr/local/src directory for each version of cd / usr/local/src# download address: http://download.redis.io/releaseswget http://download.redis.io/releases/redis-5.0.6.tar.gz# decompress tar zxf redis-5.0.6.tar.gz# into redis-5.0.6 source directory cd redis-5.0.6/# compile redismake
Create six new directories to store six redis services and corresponding configuration files
Cd / datamkdir redis1 redis2 redis3 redis4 redis5 redis6
Copy the compiled redis-server and redis.conf files to the six new redis directories
Cd / usr/local/src/redis-5.0.6cp redis.conf / data/redis1cp redis.conf / data/redis2cp redis.conf / data/redis3cp redis.conf / data/redis4cp redis.conf / data/redis5cp redis.conf / data/redis6cd / usr/local/src/redis-5.0.6/srccp redis-server / data/redis1cp redis-server / data/redis2cp redis-server / data/redis3cp redis-server / data/redis4cp redis-server / data/redis5cp redis-server / data/redis6
Configure the redis.conf of each of the six servers
The parameters to be modified are as follows:
Port the external port of each server here I assign each server as follows: 6001 6002 6003 6004 6005 6006
Cluster-enabled yes opens this configuration with a value of yes, and specifies that redis is started for the cluster node.
Cluster-config-file nodes.conf specifies the node profile
Daemonize yes allows each redis to start in guardian mode (background startup)
Modify the configuration files of each redis server separately
Cd / data/redis1vim redis.conf
The redis1 server redis.conf configuration is modified as follows
Port 6001daemonize yespidfile / var/run/redis_6001.pidcluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000
The redis2 server redis.conf configuration is modified as follows
Port 6002daemonize yespidfile / var/run/redis_6002.pidcluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000
Redis3,redis4,redis5,redis6 modifies the corresponding configuration file and so on
After configuring six servers, start six redis servers separately
Cd / data/redis1./redis-server redis.confcd / data/redis2./redis-server redis.confcd / data/redis3./redis-server redis.confcd / data/redis4./redis-server redis.confcd / data/redis5./redis-server redis.confcd / data/redis6./redis-server redis.conf
At this time, all six of our services are started, and the next step is to create a cluster of these six servers.
Create a cluster using redis-cli
Cd / usr/local/src/redis-5.0.6/src/./redis-cli-- cluster create\ 127.0.0.1 cluster create 6001\ 127.0.0.1 cluster create 6002\ 127.0.0.1 cluster-replicas 6003\ 127.0.0.1 cluster create 6005\ 127.0.0.1 cluster-replicas 1
At this point, the console prompts for yes, and enter yes to enter enter.
The following description shows that the creation is successful!
All 16384 slots covered will be prompted after the cluster is created.
Next, use the command line tool to test:
[root@VM_0_10_centos src] #. / redis-cli-c-p 6001127.0.0.1 Redirected to slot 6001 > set name liuhaizhuang- > Redirected to slot [5798] located at 127.0.0.1:6002OK127.0.0.1:6002 > get name "liuhaizhuang" 127.0.0.1 set name liuhaizhuang- >
We found that the data saved from the 6001 server is automatically allocated to the 6002 server, indicating that the horizontal cluster has been built!
two。 Description of cluster configuration parameters
# if it is yes, enable Redis Cluster support, if it is no, then redis will start independently
Cluster-enabled
# this file is a file that automatically persists the cluster configuration every time the redis cluster node changes, and the user cannot edit it.
Cluster-config-file
# the maximum time that a Redis cluster node is not available (if it is within the set time, it will not be treated as a failure)
Cluster-node-timeout
# if set to 0, the slave server will always attempt to fail over the master server, regardless of how long the link between the master server and the slave server remains disconnected, if the value is positive, the maximum disconnection time is calculated as the node timeout value multiplied by the factor provided by this option
Cluster-slave-validity-factor
# the minimum number of slaves that a host will keep connected
Cluster-migration-barrier
# if set to yes, by default, if a node does not cover a certain proportion of key space, the cluster will stop accepting writes. If this option is set to no, the cluster will provide queries even if only requests about a subset of key can be processed.
Cluster-require-full-coverage
The above is how to build a horizontal cluster in Redis. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.