当前位置:网站首页>Redis cluster mode

Redis cluster mode

2022-08-02 00:59:00 It's ninety-nine, not wine

1.为什么使用Redis集群模式

redis单机版,After a single machine failure occurs,导致redis无法使用,如果程序使用redis,Indirectly cause program errors.

2.redis的集群模式    

  1. 主从复制模式

  2. 哨兵模式

  3. clustered mode

2.1.主从复制模式

2.1.1.What is master-slave replication mode

一主多从模式.一个主节点,多个从节点,Then the master node can be responsible:读操作,写操作. 从节点只能负责读操作,Cannot be responsible for write operations. In this way, the read pressure can be distributed from the master node to the slave node,To reduce the pressure on the master node.When the master node finishes executing the write command,The data will be synchronized to the slave node.

原则:配从不配主

2.1.2.How to configure master-slave replication mode

        (1)准备条件

一主二从-----3台----Open three virtual machines--To save virtual machines,Open three on one hostredis服务.让7001作为主节点,7002,7003child node,preparing a directory,将redis.cofCopy this file to your newly created directory

        (2)修改配置文件

                1.修改端口号为7001,7002,7003

                2.修改rdb文件的名称

                3.如果你的aofturned on,就要关闭

        (3)Drag the modified files into your newly created directory,Better to rename it

        (4)启动Redis服务

        (5)Log in to the customer service

        (6)查看一下主从关系,命令是:info replication You will find that both are master nodes

        (6)设置从节点 命令:slaveof 主节点的ip 主节点的端口号,设置好之后,查看一下主从关系

up 存活  down 死亡  salveof on one--关闭子节点

 

         (7)The master node can read and write.But slave nodes can only read

2.2.哨兵模式

2.2.1.Why use sentry mode

由于主从模式,After the master node is single,The slave node will not automatically ascend. Add a sentry service,The Sentinel monitors all the timemaster,如果master挂了,Sentinels will elect a master node from among the slave nodes【哨兵投票机制】默认是sentinel.cof文件.

 2.2.2.How to use Sentinel Mode

        (1)修改配置文件--sentinel.cof

         (2)启动哨兵

redis-sentinel sentinel.conf

         (3)测试,关闭主节点,See if the child node will become the master node

shutdown  关闭Redis

 2.3.clustered mode

 2.3.1.Why use clustered mode

Regardless of the above master-slave or sentinel mode,Neither can solve the problem of single-node write operations.If the concurrency of write operations is relatively high at this time.It is possible to experiment with clustering mode【去中心化模式】

2.3.2.The principle of clustering

redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value时,redis 先对 key 使用 crc16 The algorithm computes an integer result,然后把结果对 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.

3.2.3.How to use clustered mode

        (1)前提

Prepare three master nodes,三个从节点

6001 6002 6003 6004 6005 6006

创建一个目录,Put our configuration file into the directory

        (2)设置配置文件  

Redis-1 防火墙--6379

bind 0.0.0.0

port 6001

daemonize yes

dbfilename dump6001.rdb

# 打开aof 持久化

appendonly yes

appendonly appendonly6001.aof

# 开启集群

cluster-enabled yes

# 集群的配置文件,该文件自动生成

cluster-config-file nodes-6001.conf

# 集群的超时时间

cluster-node-timeout 15000

 

 

         (3)Start these sixredis服务

         (4)分配槽

redis-cli --cluster create --cluster-replicas 1 192.168.118.110:7000 192.168.118.110:7001 192.168.118.110:7002 192.168.118.111:7003 192.168.118.111:7004 192.168.118.111:7005

注意: Make sure that each node has no data.  

        (5)进入redis

redis-cli -c-h 192.168.223.155 -p 6001  

加上-c Enter cluster mode  不加-cEnter single click mode

        (6)添加数据

Find the added data in6003的接口,是因为输入key 的时候,进行了crc16计算,Integers will be calculated 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点 ,Write operations can be spread across different nodes,Reduced pressure on a single master node.

4.java连接redis

4.1.redisWhich languages ​​are supported to operate

 

 

 4.2.使用jedis

 

 

 

               

 

 

原网站

版权声明
本文为[It's ninety-nine, not wine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020009185326.html