Redis Advanced - Redis Sentry

The address of the original text is updated, and the reading effect is better!

Redis Advance - Redis Sentinel | CoderMast Programming Mast https://www.codermast.com/database/redis/redis-advance-sentinel.html

think

After the slave node is down and recovers, it can find the master node to synchronize data, so what should the master node do if it is down?

#The role and working principle of the sentinel

#The role of the sentinel

Redis provides a Sentinel mechanism to realize automatic failure recovery of master-slave clusters. The structure and function of Sentinel are as follows:

  • Monitoring : Sentinel constantly checks that your master and slaves are working as expected
  • Automatic failure recovery : If the master fails, Sentinel will promote a slave to master. When the faulty instance recovers, the new master is also the master
  • Notification : Sentinel acts as a service discovery source for Redis clients, and when the cluster fails over, it will push the latest information to Redis clients

#Service status monitoring

Sentinel monitors the service status based on the heartbeat mechanism, and sends a ping command to each instance of the cluster every 1 second:

  • Subjective offline: If a Sentinel node finds that an instance does not respond within the specified time, it considers the instance to be offline subjectively.

  • Objective offline: If more than the specified number (quorum) sentinels think that the instance is offline subjectively, the instance will be objectively offline. The quorum value is preferably more than half of the number of Sentinel instances.

#Elect a new master

Once a master failure is found, sentinel needs to select one of the slaves as a new master. The selection basis is as follows:

  • First, it will judge the length of time that the slave node is disconnected from the master node. If it exceeds the specified value (down-after-milliseconds * 10), the slave node will be excluded

  • Then judge the slave-priority value of the slave node, the smaller the priority, the higher the priority, if it is 0, it will never participate in the election

  • If the slave-prority is the same, judge the offset value of the slave node. The larger the value, the newer the data and the higher the priority

  • The last is to judge the size of the running id of the slave node, the smaller the priority, the higher the priority

# implement failover

When one of the slaves is selected as the new master (for example, slave1), the failover steps are as follows:

  1. sentinel sends the slaveof no one command to the candidate slave1 node to make the node the master

  2. Sentinel sends the slaveof 192.168.150.101 7002 command to all other slaves to make these slaves become slave nodes of the new master and start synchronizing data from the new master.

  3. Finally, sentinel marks the failed node as a slave, and when the failed node recovers, it will automatically become the slave node of the new master

#Summary _

What are the three functions of Sentinel?

  • monitor
  • failover
  • notify

How does Sentinel judge whether a Redis instance is healthy?

  • Send a ping command every 1 second, if there is no communication for a certain period of time, it is considered as a subjective offline

  • If most sentinels think that the instance is offline subjectively, it is determined that the service is offline

What are the failover steps?

  • First select a slave as the new master, execute slaveof no one
  • Then let all nodes execute slaveof new master
  • Modify the faulty node configuration, add slaveof new master

#Build sentinel cluster

# cluster structure

Here we build a Sentinel cluster formed by three nodes to supervise the previous Redis master-slave cluster. As shown in the picture:

The information of the three sentinel instances is as follows:

node IP PORT
s1 192.168.150.101 27001
s2 192.168.150.101 27002
s3 192.168.150.101 27003

#Prepare instance and configuration

To start three instances on the same virtual machine, three different configuration files and directories must be prepared. The directory where the configuration files are located is also the working directory.

We create three folders named s1, s2, s3:

# 进入/tmp目录
cd /tmp
# 创建目录
mkdir s1 s2 s3

Then we create a sentinel.conf file in the s1 directory and add the following content:

port 27001
sentinel announce-ip 192.168.150.101
sentinel monitor mymaster 192.168.150.101 7001 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
dir "/tmp/s1"
  • port 27001: is the port of the current sentinel instance
  • sentinel monitor mymaster 192.168.150.101 7001 2: Specify master node information
    • mymaster: master node name, user-defined, arbitrary write
    • 192.168.150.101 7001: the ip and port of the master node
    • 2: quorum value when electing master

Then copy the s1/sentinel.conf file to the s2 and s3 directories (execute the following commands in the /tmp directory):

# 方式一:逐个拷贝
cp s1/sentinel.conf s2
cp s1/sentinel.conf s3
# 方式二:管道组合命令,一键拷贝
echo s2 s3 | xargs -t -n 1 cp s1/sentinel.conf

Modify the configuration files in the two folders s2 and s3, and change the ports to 27002 and 27003 respectively:

sed -i -e 's/27001/27002/g' -e 's/s1/s2/g' s2/sentinel.conf
sed -i -e 's/27001/27003/g' -e 's/s1/s3/g' s3/sentinel.conf

# start

In order to view the logs conveniently, we open three ssh windows, start three redis instances respectively, and start the command:

# 第1个
redis-sentinel s1/sentinel.conf
# 第2个
redis-sentinel s2/sentinel.conf
# 第3个
redis-sentinel s3/sentinel.conf

After startup:

# test

Try to shut down the master node 7001, check the sentinel log:

Check the log of 7003:

Check the log of 7002:

# RedisTemplate connection cluster

In the Redis master-slave cluster supervised by the Sentinel cluster, its nodes will change due to automatic failover, and the Redis client must sense this change and update the connection information in time. The bottom layer of Spring's RedisTemplate uses lettuce to realize node perception and automatic switching.

  1. import pom file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. configuration file

spring:
  redis:
    sentinel:
      master: mymaster
      nodes:
      - 192.168.127.101:27001
      - 192.168.127.101:27002
      - 192.168.127.101:27003
  1. Modify the configuration class to achieve read-write separation

This method is written in the project startup class.

// 常规写法
@Bean 
public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer(){
    return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
}
// 匿名内部类的简写
@Bean
public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer(){
    @Override
    public void customize(LettuceClientConfiguration.LettuceClientConfigurationbuilder clientConfigurationBuilder){
        clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
    }
}
  1. Controller Controller class

@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/set/{key}/{value}")
    public String setKey(@PathVariable("key") String key, @PathVariable("value") String value){
        stringRedisTemplate.opsForValue().set(key, value);
        return "success";
    }

    @GetMapping("/get/{key}")
    public String getKey(@PathVariable("key")String key) {
        String value = stringRedisTemplate.opsForValue().get(key);
        return value;
    }
}
  1. test access

Guess you like

Origin blog.csdn.net/qq_33685334/article/details/131426460