- 1、meet:sender向receiver发出 , 请求receiver加入sender的集群(加入集群时的协议)
- 2、ping:节点检测其他节点是否在线
- 3、pong:receiver收到meet或ping后的回复信息;在failover后 , 新的Master也会广播pong
- 4、fail:节点A判断节点B下线后 , A节点广播B的fail信息 , 其他收到节点会将B节点标记为下线
- 5、节点A收到publish命令 , 执行该命令 , 并向集群广播publish命令 , 收到publish命令的节点都会执行相同的publish命令
通过gossip协议 , cluster可以提供集群间状态同步更新、自动选举 , 故障转移(failover)等重要的集群功能.
2.3、Redis的hash槽
redis-cluster把所有的物理节点映射到[0-16383]个slot上,基本上采用平均分配和连续分配的方式 。
比如部署架构图中有5个主节点 , 这样在RedisCluster创建时 , slot槽可按如下分配:
Redis1 0-3270, Redis2 3271-6542, Redis3 6543-9814, Redis4 9815-13087, Redis5 13088-16383cluster负责维护节点和slot槽的对应关系 value------>slot-------->节点
当需要在Redis集群中放置一个key-value时 , redis先对key使用crc16算法算出一个结果 , 然后把结果对16384求余数 , 这样每个key都会对应一个编号在0-16383之间的哈希槽 , redis会根据节点数量大致均等的将哈希槽映射到不同的节点 。
2.5、RedisCluster的优点
- 1、高性能:Redis的性能和单机版时同级别的 , 但是存在多个主节点 , 可以做负载均衡 , 读写分离等 。
- 2、高可用:Redis分片集群的同时 , 对每个节点都可以采用主从复制保障每个节点的高可用 。
- 3、故障转移 , Redis也实现了一个类似Raft的方式 , 保证整个集群的可用性 。failover故障自动转移 。
- 4、易扩展:向Redis增加 , 移除节点是透明的 , 无需停机 。水平和垂直方向都非常容易扩展 。
- 5、Redis官方实现:部署RedisCluster不需要其他代理或者工具 , 使用Redis-Cli即可 。是Redis官方开发的 , 与单机版几乎完全兼容 。
生产环境中的Redis服务器最少三台主服务器 , 三台从服务器 。这里由于条件有限 , 在同一台机器上处理 , 也就是实现伪分布式集群 。
下载Redis
下载地址:https://download.redis.io/releases/redis-5.0.0.tar.gz
安在Redis单机版
请查看<> 中 , 地址为:https://www.toutiao.com/article/7140648630402204168
准备工作
# 创建redis集群文件夹 mkdir -p /opt/redis/redis_cluster # 复制一份编译好的Redis命令 cp -r /opt/redis/baseredis /opt/redis/redis_cluster/redis1 # 复制配置文件 cp /opt/redis/redis-5.0.0/redis.conf /opt/redis/redis_cluster/redis1/redis.conf # 复制六份 , 分别修改配置文件 cp -r redis1 redis2 ; cp -r redis1 redis3 ; cp -r redis1 redis4 ; cp -r redis1 redis5 ; cp -r redis1 redis6redis.conf配置文件修改
##################################.NETWORK ##################################### # 注释掉bind 127.0.0.1 , 不然ip地址只能使用127.0.0.1访问 # bind 127.0.0.1 # 端口号(需要修改) port 7001 tcp-backlog 511 timeout 0 tcp-keepalive 300 ################################# GENERAL ##################################### # 开启后台启动(修改为yes) daemonize yes supervised no # 进程ID(修改为对应端口号的pid) pidfile /var/run/redis_7001.pid loglevel notice logfile "" databases 16 always-show-logo yes ################################ SNAPSHOTTING ################################ save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbchecksum yes dbfilename dump.rdb dir ./ ################################# REPLICATION ################################# # 主从复制 # replicaof # 连接主服务器认证密码(修改为集群的密码 , 每台机器都一样) masterauth abcAbc123. replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-disable-tcp-nodelay no # By default the priority is 100. replica-priority 100 ################################## SECURITY ################################### # 认证密码(修改为集群的密码 , 每台机器都一样) requirepass abcAbc123. ############################# LAZY FREEING #################################### lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no ############################## AppEND ONLY MODE ############################### # 是否开启AOF(修改为yes, 开启aof) appendonly yes # 开启aof后的文件名 appendfilename "appendonly.aof" # appendfsync always appendfsync everysec # appendfsync no no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes ################################ LUA SCRIPTING ############################### # Set it to 0 or a negative value for unlimited execution without warnings. lua-time-limit 5000 ################################ REDIS CLUSTER ############################### # 是否开启集群(修改为yes , 开启集群功能) cluster-enabled yes # 集群节点的配置文件(修改为对应端口号的集群端口号对用节点) cluster-config-file nodes-7001.conf # 节点间通信超时时间 cluster-node-timeout 15000 ################################## SLOW LOG ################################### slowlog-max-len 128 ################################ LATENCY MONITOR ############################## latency-monitor-threshold 0 ############################# EVENT NOTIFICATION ############################## notify-keyspace-events "" ############################### ADVANCED CONFIG ############################### hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Redis 的大 Key 对持久化有什么影响?
- 手机防窥膜的原理是什么?真的会影响视力吗?
- 碘酒消毒的原理是什么?
- 三大指纹识别原理 指纹识别技术
- 玄空风水原理和方法 大玄空风水
- 图解涡轮增压器工作原理 涡轮增压器工作原理
- 跳绳减肥原理是什么
- 瘦身舞蹈瘦腿的原理是什么?
- 详解反渗透膜(RO 反渗透膜原理工作原理)
- 精索静脉曲张手术原理
