Redis 哨兵使用以及在 Laravel 中的配置

主从配置(master-slave)

  • 复制 redis 配置文件以开启多个 slave

sudo cp /etc/redis.conf /etc/redis-6381.conf

sudo cp /etc/redis.conf /etc/redis-6382.conf

  • 编辑 slave 配置文件,主要修改参数

port 6381

pidfile "/var/run/redis-6381.pid"

logfile "/var/log/redis/redis-6381.log"

slaveof 11.11.11.11 6381

masterauth "123456" # 主从都保持一样的密码,且 master 的配置也需要这一行,在执行切换 master 的时候好像不会去添加这一行
  • /usr/bin/redis-server /etc/redis.conf 通过配置启动 redis

哨兵配置(sentinel)

  • 复制哨兵配置,这儿开启3个哨兵

sudo cp /etc/redis-sentinel.conf /etc/redis-sentinel-26381.conf

sudo cp /etc/redis-sentinel.conf /etc/redis-sentinel-26382.conf

  • 编辑哨兵配置文件,主要修改参数如下,根据具体情况配置

port 26381

pidfile "/var/run/redis-sentinel-26381.pid"

logfile "/var/log/redis/redis-sentinel-26381.log"

sentinel monitor mymaster 11.11.11.11 6379 2 #主节点别名为mymaster,后面是ip和端口,2代表判断主节点失败至少需要2个sentinel节点同意

sentinel auth-pass mymaster 123456

sentinel down-after-milliseconds mymaster 30000 #主节点故障30秒后启用新的主节点

sentinel parallel-syncs mymaster 1 #故障转移时最多可以有1个从节点同时对主节点进行数据同步,数字越大,用时越短,存在网络和 IO 开销

sentinel failover-timeout mymaster 180000 #故障转移超时时间180s:a 如果转移超时失败,下次转移时时间为之前的2倍;b 从节点变主节点时,从节点执行 slaveof no one 命令一直失败的话,当时间超过180S时,则故障转移失败;c 从节点复制新主节点时间超过180S转移失败
  • /usr/bin/redis-sentinel /etc/redis-sentinel.conf 通过配置启动哨兵

laravel 哨兵配置


'default' => [
            'tcp://11.11.11.11:26379',
            'tcp://11.11.11.11:26381',
            'tcp://11.11.11.11:26382',    //这3个都是sentinel节点的地址
            'options' => [
                'replication' => 'sentinel',
                'service'     => env('REDIS_SENTINEL_SERVICE', 'mymaster'),    //sentinel
                'parameters'  => [
                    'password' => env('REDIS_PASSWORD', null),    //redis的密码,没有时写null
                    'database' => 0,
                ],
            ],
        ]
本作品采用《CC 协议》,转载必须注明作者和本文链接
Persevere,Vtr!
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 14

高可用必备 :+1:

4年前 评论
若相惜 (楼主) 4年前
游离不2

这是 laravel 什么版本,用的是 phpredis 还是 predis?

3年前 评论
若相惜 (楼主) 3年前
游离不2 (作者) 3年前
若相惜 (楼主) 3年前
xuzhongjian521 3年前

Redis常规操作测试没有问题,cache和queue中redis驱动需要做什么调整吗?为啥必须要用predis而不能用phpredis呢?

测试结果

  1. 我把cache.php中stores.redis.connection的值由cache改成了default,一切正常
  2. Redis正常操作没有问题
  3. queue测试没有问题!
    以上测试都是本地功能性测试,暂时没有发现问题!
1年前 评论
若相惜 (楼主) 1年前
若相惜 (楼主) 1年前
勇敢的心 (作者) 1年前
若相惜 (楼主) 1年前

想问下博主env里面怎么配置的?

1年前 评论
若相惜 (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!