Reprinted: https: //juejin.im/post/5ad54d76f265da23970759d3
Author: SnailClimb
Summarize here redis clusters needed to build the future also we hope to be able to help you.
Here are the main pen used Centos7
The installation of a redis
Redis is c language development.
Install redis need c compiler environment language. If you do not need to be online to install gcc: yum install gcc-c ++
The first step : Get source package: wget http://download.redis.io/releases/redis-3.0.0.tar.gz
Step two : Unpack Redis: tar zxvf Redis-3.0.0.tar.gz
Step Three : compiler. Redis into the source directory ( cd redis-3.0.0 ). Execution make
Step four : Install. make install PREFIX = / usr / local / redis
PREFIX parameter specifies the installation directory of redis. General software is installed to / usr directory
Redis successfully installed in such a case our usr / local / redis directory.
Step five : Set the background to start:
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis/bin/
(Copy /root/redis-3.0.0/redis.conf to the next / usr / local / redis / bin directory)
Modify the configuration file: the daemonize behind the parameter to yes
Test start: [root @ localhost bin] # ./redis-server redis.conf
View redis process: [root @ localhost bin] # PS the AUX | grep redis
Redis build two clusters
2.1 redis clusters (redis-cluster) principle
redis prior to version 3.0 does not support clusters, prior to version 3.0 if you want to build redis clusters need middleware to find the stored value and the value of the corresponding node.
After redis version 3.0 cluster architecture diagram:
So this is how to achieve it? ? ?
Redis cluster built 16384 hash slot, when a key-value to be placed in the cluster Redis, Redis crc16 algorithm key using the first calculated result, and the results of the remainder number of 16384, such that each key will correspond to a in No. 0-16383 hash groove between, redis will be substantially equal to the hash slot mapped to different nodes according to the number of nodes.
The following figure should be more easy to understand. (Source: http: //www.cnblogs.com/liyasong/p/redis_jiqun.html)
redis clusters voting mechanism
redis redis server cluster inevitably there will be more than one server hang. Whether the server cluster redis between nodes can be connected to each other via a ping-pong determination. If more than half of the nodes to ping a node when there is no response, it considers the cluster node is down.
The above is what we often say, for fault tolerance born redis clusters voting mechanism .
2.2 redis集群(redis-cluster)的搭建
redis集群搭建起来很简单,我们这里用一台虚拟机模拟搭建包含6个redis服务器的集群,实际工作中与使用多台服务器搭建是一个操作。
我们上面已经装好了一个redis实例,现在我们需要把它复制6份并修改相应端口。
第一步: 新建redis-cluster文件夹
第二步:复制redis实例
[root@Snailclimb local]# cp redis/bin redis-cluster/redis1
如果你复制过去的redis实例有dump.rdb文件的话最好也要删除。
第三步:修改配置文件
修改bin目录下的redis.conf配置文件
第四步:继续复制5个redis实例
我们用上面的redis实例复制5个redis实例,然后把他们的配置文件的端口号改为7002-7006
第五步 :新建一个执行脚本:
[root@Snailclimb redis-cluster]# vim start-all.sh
脚本内容如下:
为脚本赋予执行权限:
[root@Snailclimb redis-cluster]# chmod u+x start-all.sh
同时启动6个redis实例:
[root@Snailclimb redis-cluster]# ./start-all.sh
第六步:将redis-trib.rb复制到redis-cluster目录下面:
并为脚本赋予执行权限:[root@Snailclimb redis-cluster]# chmod u+x redis-trib.rb
第七步:安装ruby和ruby运行环境
yum install ruby
yum install rubygems
gem install redis-3.0.0.gem
Step eight: Use ruby script to build clusters:
[root@Snailclimb redis-cluster]#./redis-trib.rb create --replicas 1 192.168.25.155:7001 192.168.25.155:7002 192.168.25.155:7003 192.168.25.155:7004 192.168.25.155:7005 192.168.25.155:7006
View cluster:
Note: Port correct errors or did not comment before the cluster-enabled yes will result in removing the cluster set up to fail. Overall, redis Cluster Setup is very simple.
Such a complete redis clusters have been set up is completed. . .
Redis three stand-alone version of the test using the cluster
Add Maven dependence:
Stand-alone test redis:
Use connection pooling test stand-alone redis:
Testing Cluster Edition redis:
How to achieve a single and clustered in four JavaWeb seamlessly switch between projects
How can we achieve a single redis they want to use in the project on a stand-alone cluster redis redis would like to use and do not modify the code with the project redis clusters it? ? ?
Create the appropriate classes and interfaces
interface:
Cluster Edition uses:
Stand-alone use:
applicationContext-redis.xml
Test code:
In this project we do not need to modify the actual code to implement the relevant switch and stand-alone version of the cluster. .