Sentinel mode of Redis and the use of RedisTemplate

Redis sentry mode

The principle of a sentinel modeinsert image description here

illustrate:

  1. Sentinel has three roles: monitoring, failover and notification
  2. 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.
  3. 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

  1. 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

  1. configuration dependencies
<dependency>
	<groupId>org.springframework.book</groupId>
	<artifactId>spring-boot-starter-data-redis</artifacId>
</dependency>
  1. 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
  1. Configure read-write separation
@Bean
public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
    
    
	@Override
	public void customize(LettuceClientConfiguration.LettuceClientConfigurationbuilder clientConfugurationBuilder){
    
    
		clientConfugurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
	}
}

Guess you like

Origin blog.csdn.net/hcyxsh/article/details/131351779