Redis master-slave replication, sentinel mode, cluster mode construction and springboot integration


Preface

Redis has three modes: master-slave synchronization/replication, sentinel mode, and Cluster.

Master-slave replication : Master-slave replication is the basis of high-availability Redis. Both Sentinel and Cluster achieve high availability based on master-slave replication. Master-slave replication mainly implements multi-machine backup of data, as well as load balancing and simple fault recovery for read operations.

Defects: Failure recovery cannot be automated, write operations cannot be load balanced, and storage capacity is limited by a single machine.

Sentinel : Based on master-slave replication, Sentinel implements automated fault recovery.

Defects: Write operations cannot be load balanced, storage capacity is limited by a single machine, and Sentinel cannot perform automatic failover of slave nodes; in a read-write separation scenario, slave node failure will cause the read service to be unavailable, and additional monitoring of the slave nodes is required. , switching operation.

Cluster : Through clustering, Redis solves the problem that write operations cannot be load balanced and storage capacity is limited by a single machine, and implements a relatively complete high-availability solution.

The installation of redis will not be described in detail here. Students who need it can refer to the previous article [Installation and Configuration of Redis under Centos7]

1. Redis master-slave replication (read-write separation)

Environmental preparation

HOSTNAME IP
redis(main) 192.168.92.100
redis (prepared) 192.168.92.101
redis (prepared) 192.168.92.102

1. Overview of master-slave replication

  Master-slave replication refers to copying data from one Redis server to other Redis servers. The former is called the master node (Master), and the latter is called the slave node (Slave); data replication is one-way, and can only be from the master node to the slave node.

  By default, each Redis server is a master node; and a master node can have multiple slave nodes (or no slave nodes), but a slave node can only have one master node.

2. Master-slave replication

Data redundancy: Master-slave replication implements hot backup of data, which is a data redundancy method in addition to persistence.

Fault recovery: When a problem occurs on the master node, the slave node can provide services to achieve rapid fault recovery; it is actually a kind of service redundancy.

Load balancing: Based on master-slave replication, combined with read-write separation, the master node can provide write services and the slave nodes can provide read services (that is, when writing Redis data, the application connects to the master node, and when reading Redis data, the application connects to the slave node) , to share the server load; especially in scenarios where there is less writing and more reading, sharing the read load through multiple slave nodes can greatly increase the concurrency of the Redis server.

Cornerstone of high availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters. Therefore, master-slave replication is the basis of Redis high availability.

3.Redis master-slave replication process

  If a Slave machine process is started, it will send a "sync command" command to the Master machine to request a synchronous connection.

  Whether it is the first connection or reconnection, the Master machine will start a background process to save the data snapshot to the data file (perform RDB operation). At the same time, the Master will also record all commands to modify the data and cache them in the data file.

  After the background process completes the cache operation, the Master machine will send the data file to the Slave machine. The Slave machine will save the data file to the hard disk and then load it into the memory. Then the Master machine will combine all operations to modify the data. Sent to the Slave machine. If the Slave fails and causes downtime, it will automatically reconnect after returning to normal.

  After the Master machine receives the connection from the Slave machine, it sends its complete data file to the Slave machine. If the Master receives synchronization requests from multiple slaves at the same time, the Master will start a process in the background to save the data file. Then send it to all slave machines to ensure that all slave machines are normal.

4. Set up Redis master-slave replication

1. All three virtual machines enter the Redis server
Insert image description here

  • info replicationView master-slave replication information

role:master #The role of the current machine is master or slave
connected)salaces:0 #Represents that the slave node is currently 0

Insert image description here
2. Enter our slave node (101/102) to modify the configuration
PS: This is the configuration of the slave node. In the Master node, you only need to enable the password of the master-slave configuration in the following configuration, masterauth XXX, to prepare for the subsequent sentinel construction. .

  1. Search Rep to find replicaof
    .Insert image description here

  2. Modify replocaofiandmasterauth

    Insert image description here

  3. Change the read operation from the node replica-read-onlyto yes to achieve read and write separation

    ![Insert image description here](https://img-blog.csdnimg.cn/f5c0c227918f4e62947dc96dd7157a00.png

  4. Save and exit, delete the backup file on the slave node machine, follow the same steps as above to configure our second slave node 102

Insert image description here

  1. Restart the redis service and configure our second slave node 102 as above.

Insert image description here

  1. View the 101 slave nodesinfo replication
    Insert image description here

  2. Check the PS of 100 master nodes info replication
    Insert image description here
    : When the master node goes down, the slave node will not become the master node.

2. Redis Sentinel Mode (Sentinel)

  Problems with master-slave replication: The master node is down. How to ensure availability and continue reading and writing?

  In Redis master-slave replication, after our Redis Master node goes down, the slave node Slave cannot automatically become the master node, so it cannot provide users with write requests and can only perform reading. In order to solve this problem, we introduced Redis sentinel mechanism.
  Through the sentinel mechanism, the sentinel will monitor all nodes. When the master node goes down, the sentinel will let one of the slave nodes replace the master node and take over its write operations, thus becoming a new master-slave mode.
Insert image description here

Environmental preparation

HOSTNAME IP
redis(main) 192.168.92.100
redis (prepared) 192.168.92.101
redis (prepared) 192.168.92.102

1. Configure the Redis sentinel mechanism

1. Enter the Redis installation directory
cd /usr/local/redis/redis-5.0.0
sentinel.conf which is the core configuration file of our sentinel mechanism.
Insert image description here
2. Copy the sentinel core configuration file to our /usr/local/redis3.
cp sentinel.conf /usr/local/redis
Insert image description here
Edit our sentinel core configuration file
vim sentinel.conf
Insert image description here
Insert image description hereInsert image description here

The specific configuration information is as follows:

  • Just copy and modify parameters directly
# Base
#开启,方便其他节点器访问
protected-mode no  
#哨兵默认端口号
port 26379
#后台启动模式
daemonize yes
#哨兵机制默认进程号
pidfile /var/run/redis-sentinel.pid
#设置哨兵模式的日志文件路劲
logfile /usr/local/redis/sentinel/redis-sentinel.log
#哨兵模式的工作目录
dir /usr/local/redis/sentinel

# core  核心配置
# my-master:自定义名称           192.168.92.100 6379:主节点ip+端口    2:哨兵数量
sentinel monitor my-master 192.168.92.100 6379 2
#设置主节点密码
sentinel auth-pass my-master 123456
#被哨兵任务失效的毫秒时间段,默认为30秒,单位毫秒
sentinel down-after-milliseconds my-master 30000
#当从节点后变为主节点后,进行配置其他的,多少个从节点进行并行复制     1:一次接一个复制     2:一次复制2个
sentinel parallel-syncs my-master 1
#主备的切换时间,哨兵进行故障转移如果超时,则会交给哨兵集群的另一个节点处理进行故障转移
sentinel failover-timeout my-master 180000

2. Since it is a sentinel cluster, the sentinel configuration files of the three redis nodes are exactly the same.

You can make a copy directly, or you can use the following method to copy remotely

PS: Remotely transfer configuration files
scp sentinel.conf [email protected]:/usr/local/redis
scp sentinel.conf [email protected]:/usr/local/redis
Insert image description here

3. Start Sentinel

Start command: redis-sentinel sentinel.conf
where sentinel.conf is the sentinel core configuration file path
Insert image description here

PS: There are no folders and files, because we have pre-defined directories, we can just create them, and then start again. Check the
mkdir /usr/local/redis/sentinel -p
Insert image description here
ps -ef|grep redisredis process
Insert image description here
tail -f redis-sentinel.logand dynamically check the end of the sentinel log file.
Insert image description here
When this message appears, it means that the configuration is successful, and
then the slave node server also All need to start the sentinel configuration file

4. Test the sentinel mechanism

  1. Turn on all master-slave replication architecture nodes, currently 100 is the master node
    Insert image description here
  2. Shut down the master node and check the log.
    Insert image description here
    Insert image description here
    You can see that 101 has become master, and the downed 100 node has also been eliminated.
    Insert image description here
    3. Start the original 100 master node
    Insert image description here
    and check the log. After 100 becomes alive, the previous master node will not be restored.
    Insert image description here
    Insert image description here

5. Solve the problem of out-of-synchronization after the original Master is restored

  I believe careful students will find that after the original Master (100) is restored to Slave), its synchronization status is not OK and the status is master_link_status:dowm. Why is this?
Just change redis.confthe masterauth being modified to your own password, mine is 123456.
Insert image description here

Generally, the solution for master data to be unable to be synchronized to slave is as follows:

  1. For network communication issues, ensure that they can ping each other and communicate within the intranet.
  2. Turn off the firewall and develop the corresponding port (it is recommended to permanently turn off the firewall for virtual machines, and for cloud servers, intranet interoperability needs to be ensured).
  3. Unify all passwords and do not miss a node that is not set.

6.SpringBoot integrates Redis Sentinel

1. Configuration information

Insert image description here

server:
  port: 8088

spring:
  datasource:  # 数据源的相关配置
    url: jdbc:mysql://127.0.0.1:3306/smart?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    password: 123456
  redis:
    # Redis 单机单实例
 #   database: 0
 #   host: 192.168.92.100
 #   port: 6379
 #   password: 123456
    # Redis 哨兵模式
    database: 1
    password: 123456
    sentinel:
      master: my-master
      nodes: 192.168.92.100:26379,192.168.92.101:26379,192.168.92.102:26379

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2.Test results:

Insert image description here

3. Redis cluster mode (Redis-Cluster)

  Earlier we learned about master-slave replication and sentinels. They can improve read concurrency, but the capacity of a single master is limited and there will be a bottleneck when the data reaches a certain level. At this time, it can be horizontally expanded to form a cluster with multiple master-slave.
  So at the beginning of this section, let’s learn about redis-cluster: it can support multiple master-.slave, support massive data, and achieve high availability and high concurrency.
  Sentinel mode is actually a kind of cluster. It can improve the concurrency of read requests, but there may be some problems in fault tolerance. For example, when masterf synchronizes data to slave, this is actually asynchronous replication. At this time, masterf hangs, then slave. The data on it is not as new as the master. Data synchronization takes time, and 1-2 seconds of data will be lost. After the master is restored and converted to a slave, the new data is lost.

Features

  1. Each node knows the relationship between each other and its own role. Of course, they also know that they exist in a cluster environment. They can interact and communicate with each other, such as ping pong. Then these relationships will be saved in a certain configuration file, which is included in each node. We will configure this when building.
  2. If the client wants to establish a connection with the cluster, it only needs to establish a relationship with one of them.
  3. If a node hangs up, it is also detected by more than half of the nodes. After the objective goes offline, the master-slave switch is the same as what we mentioned before in the sentry mode.
  4. There are many slots in Redis, which can also be called slot nodes, used to store data. I will ignore this for now and will talk about it later.

To build a Redis cluster with cluster fault tolerance
  , you need at least 3 nodes as masters to form a highly available cluster. In addition, each masteri needs to be equipped with a slave, so the entire cluster requires 6 nodes. This is also the most classic Redis cluster. It can be called three masters and three slaves, which has better fault tolerance. Therefore, 6 virtual machines are required when setting up. Please prepare 6 virtual machines each. You can build them through cloning. You can clone them using a single instance of Redis.

  • The cluster can also be built on a single server, which is called a pseudo-cluster, but the production environment must be real, so it is recommended to use 6 servers.
  • Be sure to close Redis after cloning.

Insert image description here

Environmental preparation

It is best to find a redis that has not been configured before and configure it again. Do not reuse the configuration files in the previous two sections.
After Vmware configures one, you can clone the system.

HOSTNAME IP
103 redis 192.168.92.103
104 redis 192.168.92.104
105 redis 192.168.92.105
106 redis 192.168.92.106
107 redis 192.168.92.107
108 redis 192.168.92.108

Insert image description here

1. Operating our first Cluster server 103

  1. Enter the redis.conf directory and edit the file
cd /usr/local/redis/
vim redis.conf 
  1. Modify the configuration as follows:
    Insert image description here
    Insert image description here
#开启集群模式
cluster-enabled yes
#每一个节点需要有一个配置文件,需要6份。每个节点处于集群的角色都需要告知其他所有节点,彼此知道,这个文件用于存储集群模式下的集群状态等信息,这个文件是由ris自己维护,我们不要处理
cluster-config-file nodes-6379.conf
#超时时间,超时则认为master?宕机,随后主备切换
cluster-node-timeout 15000
#开启AOF
appendonly yes
  1. Save the configuration file, restart redis, and check if the cluster appears in the service, indicating that the configuration is successful.
    /etc/init.d/redis_init_script start
    Insert image description here

2. Start 6 redis instances

  1. Start the other 5 stations and follow the above steps in sequence.
  2. If an error occurs during the startup process, delete files such as rdb and clear them.
    cd working/
    rm dump.rdb
  • Here you can use xhell to operate multiple windows at the same time
    Insert image description here

3. Create a cluster

(Old versions of redis need to be built using the redis-trib.rb script in the redis installation directory)

1. Operate our 103 (master) node

#####
#集群命令说明
redis-cli --cluster help
#注意1:如果你使用的是redis3.x版本,需要使用redis-trib.rb来构建集群,最新版使用C语言来构建了,这个要注意
#注意2:以下为新版的redis构建方式
#####
#创建集群,主节点和从节点比例为1,1-3为主,4-6为从,1和4,2和5,3和6分别对应为主从关系,这也是最经典用的最多的集群模式
redis-cli --cluster create ip1:port1 ip2:port2 ip3:port3 ip4:port4 ip5:port5 ip6:port6 --cluster-replicas 1


##我的集群供参考---------------------------------------------------------------------------------------
#-a 为当前redis服务密码
redis-cli -a 123456 --cluster create 192.168.92.103:6379 192.168.92.104:6379 192.168.92.105:6379 192.168.92.106:6379 192.168.92.107:6379 192.168.92.108:6379 --cluster-replicas 1

##查看集群信息,任意一个节点都行 a 为当前redis服务密码
redis-cli -a 123456 --cluster 192.168.19.108:6379

Insert image description here
When the green area appears, it means that the construction of our three masters and three slaves is completed.

Sots : Slots, used to load data, available on the master node but not on the slave node

2. View cluster information

Any node executes:
redis-cli -a 123456 --cluster 192.168.19.108:6379
Insert image description here

4. slot node (following the consistent hash principle)

The slot slot node exists in the master node of the Redis cluster, but there is no slot node in the slave node.

  1. slot slot nodes are evenly distributed on the master node
    Insert image description here
  2. slot slot node is used to save data
    Insert image description here

View cluster information.
You can view it on any cluster server node.
redis-cli -c -a xxxx密码 -h 192.168.92.142 -p 6379
cluster infoView cluster configuration information
cluster nodes. View node information
Insert image description here
test .
Insert image description here

5.springboot integrates redis cluster

1. Configuration information

Insert image description here

server:
  port: 8088

spring:
  datasource:                                           # 数据源的相关配置
    url: jdbc:mysql://127.0.0.1:3306/smart?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    password: 123456
  redis:
    # Redis 单机单实例
    #database: 0
    #host: 192.168.92.100
    #port: 6379
    #password: 123456

    # Redis 哨兵模式
    #database: 1
    #password: 123456
    #sentinel:
    #  master: my-master
    #  nodes: 192.168.92.100:26379,192.168.92.101:26379,192.168.92.102:26379

    # Redis 集群模式
    password: 123456
    cluster:
      nodes: 192.168.92.103:6379,192.168.92.104:6379,192.168.92.105:6379,192.168.92.106:6379,192.168.92.107:6379,192.168.92.108:6379

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2.Test results

Insert image description here
Insert image description here
You can see that bbb is not on node 104, and the data is stored on node 103.

Guess you like

Origin blog.csdn.net/qq_38055805/article/details/128581067