当前位置: 首页 > news >正文

php方案 Redis Sentinel故障转移

<?php/** * Redis Sentinel 完整实现 - 自动故障转移 */classRedisSentinelClient{private$sentinels;private$masterName;private$redis;private$timeout=2;publicfunction__construct($sentinels,$masterName='mymaster'){$this->sentinels=$sentinels;// [['host' => '127.0.0.1', 'port' => 26379], ...]$this->masterName=$masterName;$this->connect();}/** * 连接到当前主节点 */privatefunctionconnect(){$master=$this->getMaster();if(!$master){thrownewException('无法获取主节点信息');}$this->redis=newRedis();$this->redis->connect($master['ip'],$master['port'],$this->timeout);}/** * 从 Sentinel 获取主节点信息 */privatefunctiongetMaster(){foreach($this->sentinelsas$sentinel){try{$s=newRedisSentinel($sentinel['host'],$sentinel['port'],$this->timeout);$master=$s->getMasterAddrByName($this->masterName);if($master&&count($master)>=2){return['ip'=>$master[0],'port'=>$master[1]];}}catch(Exception$e){continue;// 尝试下一个 Sentinel}}returnnull;}/** * 执行 Redis 命令(带自动重连) */publicfunctionexecute($method,$args=[]){$maxRetries=3;$attempt=0;while($attempt<$maxRetries){try{returncall_user_func_array([$this->redis,$method],$args);}catch(RedisException$e){$attempt++;if($attempt>=$maxRetries){throw$e;}// 重新连接到新的主节点sleep(1);$this->connect();}}}/** * 魔术方法:代理所有 Redis 方法 */publicfunction__call($method,$args){return$this->execute($method,$args);}/** * 获取集群状态 */publicfunctiongetClusterInfo(){foreach($this->sentinelsas$sentinel){try{$s=newRedisSentinel($sentinel['host'],$sentinel['port']);return['master'=>$s->master($this->masterName),'slaves'=>$s->slaves($this->masterName),'sentinels'=>$s->sentinels($this->masterName)];}catch(Exception$e){continue;}}returnnull;}}// ============ 使用示例 ============// 配置 Sentinel 节点$sentinels=[['host'=>'127.0.0.1','port'=>26379],['host'=>'127.0.0.1','port'=>26380],['host'=>'127.0.0.1','port'=>26381]];// 创建客户端$redis=newRedisSentinelClient($sentinels,'mymaster');// 使用 Redis(自动故障转移)try{$redis->set('key','value');echo$redis->get('key')."\n";// 获取集群信息$info=$redis->getClusterInfo();print_r($info);}catch(Exception$e){echo"错误: ".$e->getMessage()."\n";}

sentinel.conf - 最短配置

port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000

http://www.jsqmd.com/news/399526/

相关文章:

  • 【Demo】✋ 数字手势识别 Html
  • 【全局敏感性分析】对使用SWAT的高参数化模型,PAWN与Sobol敏感性分析方法的比较研究附Matlab代码
  • [特殊字符] 龙魂系统第三层:边界、自检、护栏机制
  • django.基于大数据+Hadoop的大数据的电力消耗智能分析与预测平台
  • django基于大数据+Hadoop+Python的软件漏洞风险预警管理系统
  • django基于大数据+Hadoop+大数据的保险行业客户数据分析与可视化
  • django基于人脸识别的门禁管理系统
  • django基于基于大数据爬虫+Hadoop+Python的情人节鲜花销售分析预测可视化平台
  • django.基于基于大数据+Hadoop+深度学习方法的田间杂草识别系统
  • django基于大数据+Hadoop+Python的旅游景点门票预约与在线支付系统的设计与实
  • django基于基于大数据+Hadoop+Spark的青少年饮食习惯数据分析与可视化平台
  • django基于基于大数据+Hadoop+深度学习的海洋生物识别系统的设计与实现dj
  • 05_虚拟机中间件部署_ubuntu 系统 安装 Redis 7.0.15
  • 平面平铺
  • 冬奥智慧气象APP界面视觉设计与交互设计
  • 职工养老保险待遇领取地
  • AI for Code Reviewer All In One
  • 2026 全链路 GEO 优化服务商 - 品牌2025
  • day019
  • Photoshop - Photoshop 工具栏(70)以快速蒙版/标准模式编辑
  • Photoshop - Photoshop 工具栏(69)前景色和背景色
  • 宣城三维治理模型:视频孪生向镜像孪生跃迁的首个城市级实践样板---跨摄像连续表达 × 三维轨迹建模 × 空间级主动调度体系
  • 自然语言生成在AI原生应用中的5个实战案例
  • BISHI66 子数列求积
  • 宣城镜像孪生示范城:超越视频孪生的三维空间计算城市样板——跨摄像连续表达 × 三角测量厘米级定位 × 趋势级风险推演体系
  • AI原生用户画像:如何用强化学习优化推荐系统效果
  • SpringBoot整合Junit与Mybatis实战
  • 中央气象局-MICAPS4客户端
  • SciPy 稀疏矩阵
  • 题解 NOISG2026 Prelim