百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

Redis哨兵高可用架构(redis哨兵keepalive)

mhr18 2025-07-24 20:01 13 浏览 0 评论

Redis哨兵架构

sentinel哨兵是特殊的redis服务,不提供读写服务,主要用来监控redis实例节点。

在哨兵架构下client端第一次从哨兵找出redis的主节点,后继的读写操作直接访问redis的主节点,不会每次都通过sentinel代理访问redis的主节点,当redis的主节点发生了变化,哨兵会第一时间感知到,并且将redis的主节点通知给client端(redis的client端一般都实现了订阅功能,订阅sentinel发布的节点变动消息)。

Redis集群搭建

Redis主节点配置

创建主节点目录(/opt/redis-master),复制redis.conf到该目录下,redis.conf配置项修改

#后台启动
daemonize yes
#关闭保护模式,开启的话,只有本机才可以访问redis
protected-mode no  
# 需要注释掉bind
#bind 127.0.0.1(bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可)
pidfile /var/run/redis_6379.pid
logfile "6379.log"

启动Redis主节点

/usr/local/redis-6.2.14/src/redis-server redis.conf

Redis从节点1配置

创建从节点目录(/opt/redis-slave-1),复制redis.conf到该目录下,redis.conf配置项修改

#后台启动
daemonize yes
#关闭保护模式,开启的话,只有本机才可以访问redis
protected-mode no  
# 需要注释掉bind
#bind 127.0.0.1(bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可)
port 6380
pidfile /var/run/redis_6380.pid  # 把pid进程号写入pidfile配置的文件
logfile "6380.log"

#配置主从复制
replicaof 192.168.56.200 6379   # 从本机6379的redis实例复制数据,Redis 5.0之前使用slaveof
replica-read-only yes  # 配置从节点只读

启动Redis从节点

/usr/local/redis-6.2.14/src/redis-server redis.conf

Redis从节点2配置

创建从节点目录(/opt/redis-slave-2),复制redis.conf到该目录下,redis.conf配置项修改

#后台启动
daemonize yes
#关闭保护模式,开启的话,只有本机才可以访问redis
protected-mode no  
# 需要注释掉bind
#bind 127.0.0.1(bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可)
port 6381
pidfile /var/run/redis_6381.pid  # 把pid进程号写入pidfile配置的文件
logfile "6380.log"

#配置主从复制
replicaof 192.168.56.200 6379   # 从本机6379的redis实例复制数据,Redis 5.0之前使用slaveof
replica-read-only yes  # 配置从节点只读

启动Redis从节点

/usr/local/redis-6.2.14/src/redis-server redis.conf

哨兵集群搭建

创建sentinel集群目录(/opt/redis-sentinel-1、/opt/redis-sentinel-2、/opt/redis-sentinel-3),分配拷贝sentinel.conf文件到这三个文件里。

redis-sentinel-1

创建data文件目录,修改sentinel-1 的sentinel.conf配置文件

# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379
# By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis-sentinel.pid when
# daemonized.
daemonize yes
# When running daemonized, Redis Sentinel writes a pid file in
# /var/run/redis-sentinel.pid by default. You can specify a custom pid file
# location here.
pidfile ./redis-sentinel-26379.pid
# Specify the log file name. Also the empty string can be used to force
# Sentinel to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "26379.log"
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir "/opt/redis-sentinel-1/data"
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
# quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),master才算真正失效
sentinel monitor mymaster 192.168.56.200 6379 2 # mymaster这个名字随便取,客户端访问时会用到

启动sentinel哨兵实例

/usr/local/redis-6.2.14/src/redis-sentinel sentinel.conf

查看sentinel的info信息

/usr/local/redis-6.2.14/src/redis-cli -p 26379 
127.0.0.1:26379> info

redis-sentinel-2

创建data文件目录,修改sentinel-1 的sentinel.conf配置文件

# port <sentinel-port>
# The port that this sentinel instance will run on
port 26380
# By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis-sentinel.pid when
# daemonized.
daemonize yes
# When running daemonized, Redis Sentinel writes a pid file in
# /var/run/redis-sentinel.pid by default. You can specify a custom pid file
# location here.
pidfile ./redis-sentinel-26380.pid
# Specify the log file name. Also the empty string can be used to force
# Sentinel to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "26380.log"
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir "/opt/redis-sentinel-2/data"
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
# quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),master才算真正失效
sentinel monitor mymaster 192.168.56.200 6379 2 # mymaster这个名字随便取,客户端访问时会用到

启动sentinel哨兵实例

/usr/local/redis-6.2.14/src/redis-sentine2 sentinel.conf

查看sentinel的info信息

/usr/local/redis-6.2.14/src/redis-cli -p 26379 
127.0.0.1:26379> info

redis-sentinel-3

创建data文件目录,修改sentinel-1 的sentinel.conf配置文件

# port <sentinel-port>
# The port that this sentinel instance will run on
port 26381
# By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis-sentinel.pid when
# daemonized.
daemonize yes
# When running daemonized, Redis Sentinel writes a pid file in
# /var/run/redis-sentinel.pid by default. You can specify a custom pid file
# location here.
pidfile ./redis-sentinel-26381.pid
# Specify the log file name. Also the empty string can be used to force
# Sentinel to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "26381.log"
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir "/opt/redis-sentinel-3/data"
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
# quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),master才算真正失效
sentinel monitor mymaster 192.168.56.200 6379 2 # mymaster这个名字随便取,客户端访问时会用到

启动sentinel哨兵实例

/usr/local/redis-6.2.14/src/redis-sentinel sentinel.conf

查看sentinel的info信息

/usr/local/redis-6.2.14/src/redis-cli -p 26379 
127.0.0.1:26379> info

sentinel集群启动完毕后,会讲哨兵集群的元素信息写入所有sentinel的配置文件里去(追加在文件的最下面),如下所示:

sentinel known-replica mymaster 192.168.56.200 6381 #代表redis主节点的从节点信息
sentinel known-replica mymaster 192.168.56.200 6380
#代表redis主节点的从节点信息
sentinel known-sentinel mymaster 192.168.56.200 26381 a9e0b5cfa1fc7705e133f8024c3bbbcfbcc73f64 #代表感知到的其他哨兵节点
sentinel known-sentinel mymaster 192.168.56.200 26379 5bc744b420082eb3588741824265d13f1329f51b #代表感知到的其他哨兵

当redis主节点如果挂了,哨兵集群会重新选举出新的redis主节点,同时会修改所有sentinel节点配置文件的集群元数据信息,同时还会修改sentinel文件里之前配置的mymaster对应的端口。

Spring Boot连接哨兵

引入maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

Spring Boot 核心配置

application.yml

server:
  prot: 8080

spring:
  redis:
    database: 0
    timeout: 3000
    sentinel:    #哨兵模式
      master: mymaster #主服务器所在集群名称
      nodes: 192.168.56.200:26379,192.168.56.200:26380,192.168.56.200:26381
    lettuce:
      pool:
        max-idle: 50
        min-idle: 10
        max-active: 100
        max-wait: 1000

测试代码

 @RestController
public class JedisSentinelController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 测试节点挂了哨兵重新选举新的master节点,客户端是否能动态感知到
     * 新的master选举出来后,哨兵会把消息发布出去,客户端实际上是实现了一个消息监听机制,
     * 当哨兵把新master的消息发布出去,客户端会立马感知到新master的信息,从而动态切换访问的masterip
     */
    @GetMapping("/testSentinel")
    public void testSentinel() {
        int i = 1;
        while (true) {
            try {
                stringRedisTemplate.opsForValue().set("warrior:" + i, i + "");
                System.out.println("设置key:warrior:" + i);
                Thread.sleep(2000);
                i++;
            } catch (Exception e) {
                System.out.println("错误:" + e.getMessage());
            }
        }
    }
}

相关推荐

软考架构师-案例分析之Redis(软考架构师真题)

软考架构师考试中,Redis的知识考了很多回,从最近几年来看,案例分析经常考,有的时候单独考,有的时候和其他知识点一起考。Redis过往的考试中,考过的知识如下:1、Redis特点,涉及数据类型、持久...

揭秘:视频播放网站如何精准记录用户观看进度

在互联网蓬勃发展的当下,视频内容已毫无争议地成为人们获取信息、享受娱乐休闲时光的核心方式。据权威数据统计,全球每天有数十亿小时的视频被观看,视频流量在网络总流量中的占比逐年攀升,预计在未来几年内将超过...

量子级一致性!Flink+Redis全局状态管理

百万级实时计算任务如何实现亚毫秒级状态访问?本文揭秘Flink+Redis的量子纠缠态状态管理方案,将状态延迟降至0.3ms。引子:实时风控系统的量子跃迁//传统Flink状态管理(基于RocksD...

在 Mac 上运行 Redis 的 Docker 容器

在Mac上运行Redis的Docker容器,你可以按以下步骤操作,非常简单高效:一、前提要求已安装DockerDesktopforMac可通过终端验证Docker是否可用:d...

从 0 到 1:使用 Nginx + Lua 打造高性能 Web 网关

在大规模分布式架构中,Web网关扮演着重要角色,负责请求转发、负载均衡、限流、认证等功能。而Nginx+Lua结合可以提供:o高性能:Nginx是目前最流行的高性能Web服务器o动...

外贸独立站缓存设置黑科技:用错Redis比没缓存更致命

上周帮一个杭州卖家排查网站崩溃问题,发现这老铁把Redis缓存设置成128MB还开着持久化,服务器内存直接炸得比春节红包还彻底——"你这哪是缓存啊,根本是DDoS攻击自己!"最近Clo...

Spring Boot3 整合 Redis,这些缓存注解你真的会用吗?

你在开发SpringBoot3项目时,有没有遇到过这样的困扰?随着项目功能不断增加,数据量逐渐庞大,接口响应速度变得越来越慢,用户体验直线下降。好不容易找到优化方向——引入Redis缓存...

MySQL处理并发访问和高负载的关键技术和策略

MySQL处理并发访问和高负载的关键技术和策略主要包括以下几个方面:一、硬件优化1.CPU:提升CPU处理能力可以明显改善并发处理性能。根据数据库负载,考虑使用更多的CPU核心。2.内存:增加内存可以...

druid解决高并发的数据库(druid多数据源配置 spring boot)

处理高并发的时候可以解决我们java一个核心问题java核心问题就是并发问题解决并发一个是redis一个是线程池的方式现在出来是个druid好像现在解决高并发的方式进行更换数据库的方式操作场景插入频繁...

高并发方案最全详解(8大常见方案)

关注△mikechen△,十余年BAT架构经验倾囊相授!大家好,我是mikechen睿哥。高并发是大型架构的核心,下面我重点来详解常见8大高并发方案@mikechen文章来源:mikechen.cc分...

MySQL如何处理并发访问和高负载?(mysql如何处理并发访问和高负载访问)

MySQL在处理并发访问和高负载方面,采取了一系列关键技术和策略,以确保数据库系统在面对不断增长的并发需求时维持高效和稳定的性能。以下是对这些技术和策略的详细阐述,旨在全面解析MySQL如何处理并发访...

Redis高可用集群详解(redis高可用方案以及优缺点)

Redis集群与哨兵架构对比Redis哨兵架构在redis3.0以前的版本要实现集群一般是借助哨兵sentinel工具监控master节点状态,如果master节点异常,则会做主从切换,将某一台sla...

MCP协议重大升级!Spring AI联合阿里Higress,性能提升300%

引言:一场颠覆AI通信的技术革命2025年3月,MCP(ModelContextProtocol)协议迎来里程碑式升级——StreamableHTTP正式取代HTTP+SSE成为默认传输层。这一...

阿里三面被挂,幸获内推,历经5轮终于拿到口碑offer

作者:Java程序猿阿谷来源:https://www.jianshu.com/p/1c8271f03aa5每一个互联网人心中都有一个大厂梦,百度、阿里巴巴、腾讯是很多互联网人梦寐以求的地方,而我也不例...

来瞧瞧阿里一面都面些什么(笔试+机试)

絮叨说实话,能有机会面一下阿里对我来说帮助确实有蛮多,至少让我知道了自己的不足在哪,都说面试造火箭,上班拧螺丝。但就算是如此,为了生存,你也只有不停的学习,唯有光头,才能更强。哈哈起因2月28日在Bo...

取消回复欢迎 发表评论: