Dcoker Redis哨兵模式集群介绍与搭建 故障转移 分布式 Java客户端连接

介绍

Redis 哨兵模式(Sentinel)是 Redis 集群的高可用解决方案,它主要用于监控 Redis 主从复制架构中的主节点和从节点的状态,并提供故障转移和通知功能。通过 Redis 哨兵模式,可以保证 Redis 服务的高可用性和自动故障恢复。

  • 监控 (Monitoring):哨兵会监控主节点和从节点的健康状况。如果检测到某个节点故障(例如主节点不可达),哨兵会启动故障转移流程。
  • 故障转移 (Failover):当主节点出现故障时,哨兵会自动选举新的主节点,并将一个从节点提升为新的主节点。此时,所有的写操作都会发送到新的主节点。

docker-compose.yml

yaml 复制代码
version: "3.2"

services:
  r1:
    image: redis
    container_name: r1
    network_mode: "host"
    entrypoint: ["redis-server", "--port", "7001", "--requirepass", "yourpassword","--masterauth", "yourpassword"]
  
  r2:
    image: redis
    container_name: r2
    network_mode: "host"
    entrypoint: ["redis-server", "--port", "7002", "--slaveof", "103.116.247.215", "7001", "--requirepass", "yourpassword", "--masterauth", "yourpassword"]
  
  r3:
    image: redis
    container_name: r3
    network_mode: "host"
    entrypoint: ["redis-server", "--port", "7003", "--slaveof", "103.116.247.215", "7001", "--requirepass", "yourpassword", "--masterauth", "yourpassword"]
  
  s1:
    image: redis
    container_name: s1
    volumes:
      - /opt/s1:/etc/redis
    network_mode: "host"
    entrypoint: ["redis-sentinel", "/etc/redis/sentinel.conf", "--port", "27001"]
  
  s2:
    image: redis
    container_name: s2
    volumes:
      - /opt/s2:/etc/redis
    network_mode: "host"
    entrypoint: ["redis-sentinel", "/etc/redis/sentinel.conf", "--port", "27002"]
  
  s3:
    image: redis
    container_name: s3
    volumes:
      - /opt/s3:/etc/redis
    network_mode: "host"
    entrypoint: ["redis-sentinel", "/etc/redis/sentinel.conf", "--port", "27003"]

哨兵配置文件

powershell 复制代码
# 设置 Sentinel 公网 IP,其他 Sentinel 会通过此 IP 来发现该 Sentinel 节点
sentinel announce-ip 192.168.1.100

# 监控 Redis 主节点,配置主节点的 IP 地址 (192.168.1.100) 和端口 (7002)
# "app-name" 是监控的主节点名称,2 表示至少需要 2 个 Sentinel 来判断主节点下线
sentinel monitor app-name 192.168.1.100 7002 2

# 设置 Sentinel 检测主节点是否宕机的超时时间 (单位:毫秒)
# 如果 Sentinel 在 5000 毫秒内未收到主节点的回复,将认为主节点宕机
sentinel down-after-milliseconds app-name 5000

# 设置故障转移的超时时间 (单位:毫秒)
# 如果在 60000 毫秒(即 60 秒)内没有完成故障转移,Sentinel 会中止当前操作
sentinel failover-timeout app-name 60000

# 设置 Sentinel 与 Redis 主节点之间的密码认证
# 如果 Redis 主节点启用了密码保护,则 Sentinel 需要使用这个密码来连接并与主节点进行通信
sentinel auth-pass app-name yourpassword

构建

powershell 复制代码
 docker-compose up -d

SpringBoot读写分离

yaml

yaml 复制代码
spring:
  redis:
    sentinel:
      master: "hmaster" # 主节点名称
      nodes:
        - "123.6.247.215:27001" # Sentinel 1 地址
        - "123.6.247.215:27002" # Sentinel 2 地址
        - "123.6.247.215:27003" # Sentinel 3 地址
    password: "yourpassword"  # 如果 Redis 配置了密码

#方便查询关于Redis的日志
logging:
  level:
    io.lettuce.core: debug

配置读写分离

java 复制代码
@Configuration
public class RedisConfig {
    @Bean
    public LettuceClientConfigurationBuilderCustomizer lettuceclientConfigurationBuilderCustomizer(){
        return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED).commandTimeout(Duration.ofSeconds(5)); 
         // 设置连接超时,避免连接问题;
    }

}
  • MASTER: 从主节点读取
  • MASTER_PREFERRED: 优先从master节点读取,master不可用才读取replica
  • REPLICA: 从slave(replica)节点读取
  • REPLICA_PREFERRED: 优先从slave(replica)节点读取,所有的slave都不可用才读取master

接口

java 复制代码
@RestController
@RequiredArgsConstructor
public class TestController
{
    private final StringRedisTemplate stringRedisTemplate;
    @GetMapping("/t")
    public String test(){
        stringRedisTemplate.opsForValue().set("name", "dpc", Duration.ofSeconds(10L));
        return  "成功";
    }
    @GetMapping("/g")
    public String get(){
        String str =  stringRedisTemplate.opsForValue().get("name");
        return  str;
    }
    
}
相关推荐
人工智能那些事儿2 分钟前
神经网络:从基础到应用,开启智能时代的大门
java·人工智能·python·深度学习·神经网络·算法·机器学习
MiniFlyZt13 分钟前
分布式数据库TiDB:架构、核心特性与生产实践(分库分表)
java·数据库·分布式·spring cloud·微服务·tidb
zzx_nihao21 分钟前
Java29:Spring MVC
java·spring·mvc
JAVA坚守者25 分钟前
Java 服务器端 jar 包内 class 文件替换与配置文件修改高级技术指南
java·pycharm·jar
化作晚霞25 分钟前
JVM有什么调优参数?
java·开发语言·jvm
江沉晚呤时25 分钟前
在 Linux 上部署 .NET Core 应用并配置为开机自动启动
linux·服务器·mysql
musk121226 分钟前
qt 配置 mysql 驱动问题:Cannot load library qsqlmysql;QMYSQL driver not loaded
qt·mysql
callJJ28 分钟前
Floyd算法求解最短路径问题——从零开始的图论讲解(3)
java·算法·动态规划·图论·dijkstra算法·floyd算法·最短路径问题
浩哲Zhe33 分钟前
Java Web 之 Tomcat 100问
java·前端·tomcat
黄雪超37 分钟前
Flink介绍——实时计算核心论文之Kafka论文详解
大数据·flink·kafka