Redis sentry mode
The principle of a sentinel mode
illustrate:
- Sentinel has three roles: monitoring, failover and notification
- How does Sentinel judge whether Redis is healthy?
① Send a ping command every 1 second. If there is no response for a certain period of time, it will be considered as subjective offline.
② If more than half of sentinels think that the instance is subjectively offline, it will be judged as customer offline. - Failover steps
① Select a slave as the new master, execute salve no one
② Let all nodes execute salve of the new master node
③ Modify the configuration of the failed node to salve
Second sentinel cluster construction
- Add the following content under the sentinel.conf file:
#端口
port 27001
sentinel announce-ip 192.168.158.101
# mymaster 主节点名称,主节点ip 2:选举master的quorum值
sentinel monitor mymaster 192.168.158.101 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
Three using RedisTemplate to achieve
- configuration dependencies
<dependency>
<groupId>org.springframework.book</groupId>
<artifactId>spring-boot-starter-data-redis</artifacId>
</dependency>
- Configure setinel cluster configuration
srping:
redis:
setinel:
master:mymaster
nodes:
- 192.168.150.101:27001
- 192.168.150.101:27002
- 192.168.150.101:27003
- Configure read-write separation
@Bean
public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
@Override
public void customize(LettuceClientConfiguration.LettuceClientConfigurationbuilder clientConfugurationBuilder){
clientConfugurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
}
}