前提条件:搭建好了redis哨兵集群模式
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
spring: redis: sentinel: master: mymaster nodes: - 127.0.0.1:27001 - 127.0.0.1:27001 - 127.0.0.1:27001
@Bean public LettuceClientConfigurationBuilderCustomizer configurationBuilderCustomizer(){ return configBuilder -> configBuilder.readFrom(ReadFrom.REPLICA_PREFERRED); }
这里的ReadFrom是配置Redis的读取策略,是一个枚举类,有以下选择
至此,配置已经完成,再使用redisTemplate时读会从slave节点,写会从master节点