Sentinel Mode for Redis High Availability

1.1 Overview

The method of master-slave switching technology is: when the master server is down, it is necessary to manually switch a slave server to the master server, which requires manual intervention, which is time-consuming and labor-intensive, and also causes the service to be unavailable for a period of time. This is not a recommended way, and more often, we prioritize sentinel mode.

1.2 Sentinel Mode

Sentinel mode is a special mode that first Redisprovides the command of the sentinel. The sentinel is an independent process. As a process, it will run independently. RedisThe principle is that Sentinel monitors multiple Redisinstances running by sending commands and waiting for the server to respond.
insert image description here
Sentinels here serve two purposes:

  • By sending a command, the Redisserver returns to monitor its running status, including the master server and the slave server.
  • When the sentinel detects the masterdowntime, it will automatically slaveswitch to it master, and then notify other slave servers through the publish-subscribe mode, modify the configuration file, and let them switch the host.

However, when a sentinel process Redismonitors the server, there may be problems. For this reason, we can use multiple sentinels for monitoring. Monitoring will also be performed between each sentinel, thus forming a multi-sentry mode.

Describe the failover (failover)process in words. Assuming that the main server is down, the sentinel 1detects the result first, and the system does not proceed immediately. failoverIt is just that the sentinel 1subjectively believes that the main server is unavailable, and this phenomenon becomes a subjective offline. When the following sentinels also detect that the main server is unavailable and the number reaches a certain value, then a vote will be held between the sentinels, and the result of the vote will be initiated by a sentinel to failoveroperate. After the switch is successful, each sentinel will switch the host from the server they monitor through the publish-subscribe model. This process is called objective offline. This way everything is transparent to the client.

1.3 RedisConfigure Sentinel Mode

Configure 3a sentinel and a 1master - 2slave Redisserver to demonstrate this process.

Service type Is it the master server IP address port
Say it again Yes 192.168.11.128 6379
Say it again no 192.168.11.129 6379
Say it again no 192.168.11.130 6379
Sentinel - 192.168.11.128 26379
Sentinel - 192.168.11.129 26379
Sentinel - 192.168.11.130 26379

insert image description here
First configure the master-slave server of Redis, modify the redis.conf file as follows

# 使得Redis服务器可以跨网络访问
bind 0.0.0.0
# 设置密码
requirepass "123456"
# 指定主服务器,注意:有关slaveof的配置只是配置从服务器,主服务器不需要配置
slaveof 192.168.11.128 6379
# 主服务器密码,注意:有关slaveof的配置只是配置从服务器,主服务器不需要配置
masterauth 123456

slaveofThe above content is mainly to configure the Redis server, and the slave server has one more configuration and password than the master server .

Configure 3a sentinel, and the configuration of each sentinel is the same. RedisThere is a sentinel.conffile in the installation directory, copyone for modification

# 禁止保护模式
protected-mode no
# 配置监听的主服务器,这里sentinel monitor代表监控,mymaster代表服务器的名称,可以自定义,192.168.11.128代表监控的主服务器,6379代表端口,2代表只有两个或两个以上的哨兵认为主服务器不可用的时候,才会进行failover操作。
sentinel monitor mymaster 192.168.11.128 6379 2
# sentinel author-pass定义服务的密码,mymaster是服务名称,123456是Redis服务器密码
# sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster 123456

The above turns off the protected mode for easy testing.

With the above modifications, we can enter Redisthe directory of the installation directory srcand start the server and sentinel with the following commands

# 启动Redis服务器进程
./redis-server ../redis.conf
# 启动哨兵进程
./redis-sentinel ../sentinel.conf

Pay attention to the order of startup. (192.168.11.128)The first is the service process of the host Redis, then the service process of the slave is started, and finally 3the service process of a sentinel is started.

JavaSentinel mode used in 1.4

/**
 * 测试Redis哨兵模式
 * @author liu
 */
public class TestSentinels {
    
    
    @SuppressWarnings("resource")
    @Test
    public void testSentinel() {
    
    
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setMinIdle(5);
        // 哨兵信息
        Set<String> sentinels = new HashSet<>(Arrays.asList("192.168.11.128:26379",
                "192.168.11.129:26379","192.168.11.130:26379"));
        // 创建连接池
        JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,jedisPoolConfig,"123456");
        // 获取客户端
        Jedis jedis = pool.getResource();
        // 执行两个命令
        jedis.set("mykey", "myvalue");
        String value = jedis.get("mykey");
        System.out.println(value);
    }
}

The above is used Jedisfor use, and it can also be used Springfor configuration RedisTemplate.

        <bean id = "poolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <!-- 最大空闲数 -->
            <property name="maxIdle" value="50"></property>
            <!-- 最大连接数 -->
            <property name="maxTotal" value="100"></property>
            <!-- 最大等待时间 -->
            <property name="maxWaitMillis" value="20000"></property>
        </bean>
        
        <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
            <constructor-arg name="sentinelConfig" ref="sentinelConfig"></constructor-arg>
            <property name="password" value="123456"></property>
        </bean>
        
        <!-- JDK序列化器 -->
        <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
        
        <!-- String序列化器 -->
        <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        
        <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
            <property name="connectionFactory" ref="connectionFactory"></property>
            <property name="keySerializer" ref="stringRedisSerializer"></property>
            <property name="defaultSerializer" ref="stringRedisSerializer"></property>
            <property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
        </bean>
        
        <!-- 哨兵配置 -->
        <bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
            <!-- 服务名称 -->
            <property name="master">
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <property name="name" value="mymaster"></property>
                </bean>
            </property>
            <!-- 哨兵服务IP和端口 -->
            <property name="sentinels">
                <set>
                    <bean class="org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name="host" value="192.168.11.128"></constructor-arg>
                        <constructor-arg name="port" value="26379"></constructor-arg>
                    </bean>
                    <bean class="org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name="host" value="192.168.11.129"></constructor-arg>
                        <constructor-arg name="port" value="26379"></constructor-arg>
                    </bean>
                    <bean class="org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name="host" value="192.168.11.130"></constructor-arg>
                        <constructor-arg name="port" value="26379"></constructor-arg>
                    </bean>
                </set>
            </property>
        </bean>

1.5 Other Configuration Items of Sentinel Mode

configuration item Parameter Type effect
port integer Start the sentinel process port
dir folder directory Sentinel process service temporary folder, the default is /tmpto ensure that it has writable permissions
sentinel down-after-milliseconds <service name><milliseconds (integer)> When the sentinel is monitoring the Redisservice, when the Redisservice cannot answer within a default number of milliseconds, the subjective offline time considered by a single sentinel, the default is30000(30秒)
sentinel parallel-syncs <service name><number of servers (integer)> Specifies how many Redisservices can synchronize the new host. Generally speaking, the smaller the number, the longer the synchronization time, and the larger the number, the higher the network resource requirements.
sentinel failover-timeout <service name><milliseconds (integer)> Specifies the number of milliseconds allowed for failover. After this time, the failover is considered to have failed. The default is 3minutes.
sentinel notification-script <service name><script path> Specifies the alarm script to be invoked when the instance pointed to by the monitored instance is sentineldetected to be abnormal. redisThis configuration item is optional and more commonly used

sentinel down-after-millisecondsThe configuration item is just a sentinel who will think that the host is unavailable after the sentinel still does not get a response after the specified time. Not so for other Sentinels. The sentinel will record this message. When the sentinel that has the subjective offline reaches sentinel monitorthe configured number, it will initiate a vote and proceed failover. At this time, the sentinel will rewrite Redisthe sentinel configuration file to adapt to the needs of the new scene.

Guess you like

Origin blog.csdn.net/weixin_42039228/article/details/123653696