高可用高并发集群配置
1.简介
高可用 24小时对外提供服务
高并发 同一时间段能处理的请求数
2.中心化和去中心化
中心化
意思是所有的节点都要有一个主节点
缺点:中心挂了,服务就挂了
中心处理数据的能力有限,不能把节点性能发挥到最大
特点:就是一个路由作用

去中心化
特点:去掉路由,我自己来路由

以上通俗的就是
中心化:几个经过认证的嘉宾在‘讲话’,所有其他人在听。
去中心化:每个人都可以‘讲话’,每个人都可以选择听或者讲。
3.Redis集群的执行流程分析
哈希槽说明
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。
当你往Redis Cluster中加入一个Key时,会根据crc16(key) mod 16384计算这个key应该分布到哪个hash slot中,一个hash slot中会有很多key和value。你可以理解成表的分区,使用单节点时的redis时只有一个表,所有的key都放在这个表里;改用Redis Cluster以后会自动为你生成16384个分区表,你insert数据时会根据上面的简单算法来决定你的key应该存在哪个分区,每个分区里有很多key。
执行流程分析
假如redis集群里面能存放90个key,那么redis集群把90key平分到3个主机
redis对每个主机里面30个存储位置都编号,当应用连接到主机1上面时,应该发送一个写的命令
主机使用crc16算出槽号
如果槽号在1-30 可以直接操作主机1
如果槽号在31-60那么redis会转发到主机2
如果应该再发一个命令set age 22
那么主机2使用crc16再算槽号再转发
4.Redis集群的搭建
文档
原理:
去中心化
集群规则

搭建过程
新建文件夹

准备一个服务端程序

准备6个redis的配置文件


Redis-1
bind 0.0.0.0 69行
port 7000 92行
daemonize yes 136行
打开aof 持久化
appendonly yes 672行
开启集群
cluster-enabled yes 814行
集群的配置文件,该文件自动生成
cluster-config-file nodes-7000.conf 822行
集群的超时时间
cluster-node-timeout 5000 828行
Redis-2
daemonize yes
bind 0.0.0.0
port 7001
打开aof 持久化
appendonly yes
开启集群
cluster-enabled yes
集群的配置文件,该文件自动生成
cluster-config-file nodes-7001.conf
集群的超时时间
cluster-node-timeout 5000
Redis-3
daemonize yes
bind 0.0.0.0
port 7002
打开aof 持久化
appendonly yes
开启集群
cluster-enabled yes
集群的配置文件,该文件自动生成
cluster-config-file nodes-7002.conf
集群的超时时间
cluster-node-timeout 5000
Redis-4
daemonize yes
bind 0.0.0.0
port 7003
打开aof 持久化
appendonly yes
开启集群
cluster-enabled yes
集群的配置文件,该文件自动生成
cluster-config-file nodes-7004.conf
集群的超时时间
cluster-node-timeout 5000
Redis-5
daemonize yes
bind 0.0.0.0
port 7004
打开aof 持久化
appendonly yes
开启集群
cluster-enabled yes
集群的配置文件,该文件自动生成
cluster-config-file nodes-7005.conf
集群的超时时间
cluster-node-timeout 5000
Redis-6
daemonize yes
bind 0.0.0.0
port 7005
打开aof 持久化
appendonly yes
开启集群
cluster-enabled yes
集群的配置文件,该文件自动生成
cluster-config-file nodes-7006.conf
集群的超时时间
cluster-node-timeout 5000
同时启动所有的redis

使用脚本创建集群(分配槽)
找到集群脚本,在src/src/redis-trib.rb 要安装Ruby的环境【不推荐
使用docker 下载redis-trib的镜像运行【推荐】
安装Docker
yum install docker
启动docker
systemctl start docker
A: 下载镜像
docker pull inem0o/redis-trib
docker run -it —net host inem0o/redis-trib create —replicas 1
192.168.120.129:7000 192.168.120.129:7001
192.168.120.129:7002 192.168.120.129:7003
192.168.120.129:7004 192.168.120.129:7005
-it是为了可以输入
—net host 是为了上docker容器能连接上本地的宿主机


3.4.7测试集群环境

-c 表示连接集群



到此集群搭建完成
去中心化去中心化
网友评论