redis哨兵集群配置
Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器。具体原理和配置参考《【带你重拾Redis】Redis 哨兵集群实现高可用》
springboot配置Redis主从服务
接下来学了一下springboot配置redis的主从服务器。根据一主二从三哨兵的原则来搭建一个分布式的缓存服务。
引入spring-boot-starter-data-redis:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Spring Boot Redis 的默认客户端是:Lettuce,我们再来看下 LettuceConnectionFactory 的自动配置源码:
org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration
如源码所示,我们可以知道 Redis 连接自动配置的优先顺序是:
Redis Sentinel(哨兵) > Redis Cluster(集群) > Standalone(单机)
哨兵模式优先极是最高的,再来看下 getSentinelConfig 方法源码:
master、sentinels 是必须参数,password、SentinelPassword 是可选的,database 默认是第 0 个数据库。
配置参数源码:
org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel
所以,我们只需要提供 Redis Sentinel 的基本配置参数即可。
application.yml 配置如下:
spring:
profiles:
active: sentinel
---
spring:
profiles: standalone
redis:
host: 192.168.1.110
port: 6379
password: redis2020
database: 1
---
spring:
profiles: sentinel
redis:
password: redis2020
sentinel:
master: mymaster
nodes:
- 192.168.1.110:26379
- 192.168.1.111:26379
- 192.168.1.112:26379
集群配置
spring:
profiles: sentinel
redis:
password: redis2020
cluster:
nodes:
- 192.168.1.8:9001
- 192.168.1.8:9002
- 192.168.1.8:9003
代码示例
https://github/andanyoung/springboot/tree/master/redis-cluster
更多推荐
springboot配置Redis哨兵主从服务 以及 Redis 集群
发布评论