redis installation: https://www.cnblogs.com/knightdreams6/p/11270400.html
Cluster Setup:
Two virtual machines to simulate six nodes, a machine three nodes, create 3 master, 3 salve environment
using redis redis-5.0.5 version.
Two virtual machines are CentOS7, an ip (192.168.3.222), another (192.168.3.223)
1. On the first machine 192.168.3.222
Into / usr / local / bin in, mkdir redis-cluster to create a cluster folder, and then create three sub-folders in the folder named (non-essential) according to the port number for the 7000,7001,7002
2. Copy redis.conf to 7000, 7001, 7002 three folders
3. Modify the three profiles, modify the following
port 7000 // port 7000,7002,7003
the bind native ip // ip default ip to 127.0.0.1 need to switch to another node machine accessible or can not access the corresponding port when you create a cluster, you can not create a cluster
daemonize yes // redis background
pidfile /var/run/redis_7000.pid // pidfile file corresponds 7000,7001,7002
cluster-Enabled yes // Enable the cluster to comment # remove
cluster-config-file nodes_7000.conf // cluster configuration profiles first start automatically generating 7000,7001,7002
Cluster-Node-request timeout timeout 15000 // default 15 seconds, may be provided on their own
appendonly yes
4. Start three redis
In the / usr / local / bin directory
repeat-repeat-server cluster / 7000 / redis.conf
repeat-repeat-server cluster / 7001 / redis.conf
repeat-repeat-server cluster / 7002 / redis.conf
Use ps -ef | grep redis can check whether to start
[root@knight01 local]# ps -ef|grep redis
root 42050 1 0 16:49 ? 00:00:01 redis-server 192.168.3.222:7000 [cluster]
root 42055 1 0 16:49 ? 00:00:01 redis-server 192.168.3.222:7001 [cluster]
root 42060 1 0 16:49 ? 00:00:02 redis-server 192.168.3.222:7002 [cluster]
root 42435 40487 0 17:21 pts/1 00:00:00 grep --color=auto redis
Ip address 196.168.3.222 which is accessible to other machines, but also bind value ip behind redis.conf
5. Yet another virtual machine on the steps above to create a file named 700,370,047,005 folder, and then modify the configuration file and start
6. Install ruby environment (cluster command requires this)
// will be more of a CentOS-SCLo-scl-rh.repo source directory under /etc/yum.repos.d/
yum install CentOS-Release-scl-rh
// can be installed directly yum
yum install rh-ruby23 - the y-
// necessary step
scl enable rh-ruby23 bash
// View installed version of
Ruby -v
// redis required for installation of the
gem install redis
7. Create a cluster
Run the following command in / usr / local / bin
redis-cli --cluster create 192.168.3.222:7000 192.168.3.222:7001 192.168.3.222:7002 192.168.3.223:7003 192.168.3.223:7004 192.168.3.223:7005 --cluster-replicas 1
Then enter yes, the connection is successful
8. Briefly about principle
redis cluster in the design, to take into account to the center, to the middleware, that is, each node in the cluster are equal relationship are peers, each node keeps their data and entire the state of the cluster. Each node and all other nodes are connected, and these connections remain active, thus ensuring that we only need to connect any node in the cluster, you can get the data to other nodes.
Redis cluster and does not use the traditional consistent hashing to distribute data, instead of using another known 哈希槽 (hash slot)
way to distribution. redis cluster 16384 slot allocated by default, when we set a key, will use CRC16
algorithms to obtain modulo belongs slot
, then the node key assigned to the hash slot interval, the specific algorithm is:CRC16(key) % 16384。所以我们在测试的时候看到set 和 get 的时候,直接跳转到了7000端口的节点。
Redis 集群会把数据存在一个 master 节点,然后在这个 master 和其对应的salve 之间进行数据同步。当读取数据时,也根据一致性哈希算法到对应的 master 节点获取数据。只有当一个master 挂掉之后,才会启动一个对应的 salve 节点,充当 master 。
需要注意的是:必须要3个或以上
的主节点,否则在创建集群时会失败,并且当存活的主节点数小于总节点数的一半时,整个集群就无法提供服务了。
--------------------------------------------------------------------------------------------------------------------------------------- 分割线 ----------------------------------------------------------------------------------------------------------------------------------------------
springBoot配置redis集群 我使用的是 yaml格式,properties格式的自行修改
spring:
redis:
cluster:
nodes: 192.168.3.222:7000,192.168.3.222:7001,192.168.3.222:7002,192.168.3.223:7003,192.168.3.223:7004,192.168.3.223:7005
## 连接超时时间(毫秒)
timeout: 5000
lettuce:
pool:
max-active: 8
# 连接池阻塞等待时间(负值表示没有限制)
max-wait: -1ms
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中最小空闲连接
min-idle:
有任何问题可联系 [email protected]
该博文的编写参考自[ https://www.cnblogs.com/wuxl360/p/5920330.html ]