资讯专栏INFORMATION COLUMN

laravel redis 主从配置

ShevaKuilin / 798人阅读

摘要:关于主从服务器配置就略过了,以下基于你已经配置好好,然后在环境下如何使用。配置使用的是扩展主从从队列用到命令参考注意看文档需要将设置为使用具体参数含义查看日志可以看到使用的从服务器查看日志可以看到使用的主服务器

关于redis主从服务器配置就略过了,以下基于你已经配置好好,然后在laravel环境下如何使用。

配置

laravel 使用的是 predis 扩展
composer require predis/predis
vi config/database.php

"redis"=>[
   "cluster" => false,
    "default" => [
        "tcp://" . env("REDIS_DEFAULT_HOST") . ":" . env("REDIS_DEFAULT_PORT") . "?database=0&alias=master",//主
        "tcp://" . env("REDIS_DEFAULT_HOST_R") . ":" . env("REDIS_DEFAULT_PORT_R") . "?database=0&alias=slave_1",//从
        "tcp://" . env("REDIS_DEFAULT_HOST_R2") . ":" . env("REDIS_DEFAULT_PORT_R2") . "?database=0&alias=slave_2",//从
    ],
     "test" => [ 
                "host" => "127.0.0.1",
                "port" => 6379,
                "database" => 1,
            ],//队列用到multi命令
             "options" => [
                "replication" => true,
                "connections" =>[
                    "tcp" => "AppServicesRedis",
                ],
            ],
];

//master slave   vendor/predis/predis/src/Connection/Aggregate/SentinelReplication.php:189
public function add(NodeConnectionInterface $connection)
    {
        $alias = $connection->getParameters()->alias;

        if ($alias === "master") {
            $this->master = $connection;
        } else {
            $this->slaves[$alias ?: count($this->slaves)] = $connection;
        }

        $this->reset();
    }

vi app/services/redis.php
//参考 https://github.com/nrk/predis/blob/v1.1/examples/debuggable_connection.php
namespace AppServices;

use PredisCommandCommandInterface;
use PredisConnectionStreamConnection;

class Redis extends StreamConnection
{
    private $tstart = 0;
    private $debugBuffer = [];

    public function connect()
    {
        $this->tstart = microtime(true);

        parent::connect();
    }

    private function storeDebug(CommandInterface $command, $direction)
    {
        $firtsArg = $command->getArguments();
        $timestamp = (microtime(true) - $this->tstart) * 1000;
        $log = [];
        $log["cmd"] = $command->getId();
        $log["key"] = isset($firtsArg) ?  $firtsArg  : " ";
        $log["server"] = "$direction $this";
        $log["time"] = $timestamp;
        $data = ["server" => trim($log["server"]), "cmd" => $command->getId(), "key" => $log["key"],"time" => $timestamp, "msg" => ["host" => explode(":", trim($log["server"]))[0], "port" => explode(":", trim($log["server"]))[1]]]];
        openlog("syslog",LOG_PID|LOG_ODELAY,LOG_LOCAL7);
        syslog(LOG_INFO,json_encode($data));
        closelog();
        dump($log);
        $this->debugBuffer[] = $log;
    }

    public function writeRequest(CommandInterface $command)
    {
        parent::writeRequest($command);

//        $this->storeDebug($command, "->");
    }

    public function readResponse(CommandInterface $command)
    {
        $response = parent::readResponse($command);
        $this->storeDebug($command, "");

        return $response;
    }

    public function getDebugBuffer()
    {
        return $this->debugBuffer;
    }

    public static function debug()
    {
        $options = [
            "connections" =>[
                "tcp" => "AppServicesRedis",
            ],
        ];

        $client = new PredisClient(config("database.redis.default"), $options);
        $client->get("redis:test");
        print_r($client->getConnection());
    }
}
注意

看文档 https://github.com/nrk/predis 需要将 replication 设置为true

The basic configuration needed to use the client in replication mode requires one Redis server to be identified as the master (this can be done via connection parameters using the alias parameter set to master) and one or more servers acting as slaves:

使用
//具体参数含义 vendor/predis/predis/src/Connection/ParametersInterface.php:16
$redis = new PredisClient([
                "scheme" => "tcp",
                "host"   => "127.0.0.1",
                "port"   => 6379,
                "read_write_timeout" => 0,
            ]);
        $redis = Redis::connection("default");
        $key = "master:test";
        $redis->set($key, 666);//tail -f /var/log/messages 查看Redis日志可以看到使用的从服务器 REDIS_DEFAULT_HOST_R
        dump($redis->get($key));    //查看Redis日志可以看到使用的主服务器 REDIS_DEFAULT_HOST
               

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29455.html

相关文章

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

    摘要:主从配置复制配置文件以开启多个编辑配置文件,主要修改参数主从都保持一样的密码,且的配置也需要这一行,在执行切换的时候好像不会去添加这一行通过配置启动哨兵配置复制哨兵配置,这儿开启个哨兵编辑哨兵配置文件,主要修改参数如 主从配置(master-slave) 复制 redis 配置文件以开启多个 slave sudo cp /etc/redis.conf /etc/redis-6381.c...

    XanaHopper 评论0 收藏0
  • Redis 哨兵使用以及在 Laravel 中的配置

    摘要:主从配置复制配置文件以开启多个编辑配置文件,主要修改参数主从都保持一样的密码,且的配置也需要这一行,在执行切换的时候好像不会去添加这一行通过配置启动哨兵配置复制哨兵配置,这儿开启个哨兵编辑哨兵配置文件,主要修改参数如 主从配置(master-slave) 复制 redis 配置文件以开启多个 slave sudo cp /etc/redis.conf /etc/redis-6381.c...

    Vultr 评论0 收藏0
  • php面试总结

    摘要:由一层函数调用进入下一层函数调用的递推。此时,中的一个称为孤儿的类就会收留这个对象。禁止访问服务器拒绝请求服务器找不到请求的页面服务器内部错误坏的网关一般是网关服务器请求后端服务时,后端服务没有按照协议正确返回结果。 持续更新。。。。 php 1. 简述 php 中的 autoload Autoload 的加载机制,当通过 new 来实例化一个类时,PHP 会通过定义的 autol...

    skinner 评论0 收藏0
  • php面试总结

    摘要:由一层函数调用进入下一层函数调用的递推。此时,中的一个称为孤儿的类就会收留这个对象。禁止访问服务器拒绝请求服务器找不到请求的页面服务器内部错误坏的网关一般是网关服务器请求后端服务时,后端服务没有按照协议正确返回结果。 持续更新。。。。 php 1. 简述 php 中的 autoload Autoload 的加载机制,当通过 new 来实例化一个类时,PHP 会通过定义的 autol...

    greatwhole 评论0 收藏0
  • php面试总结

    摘要:由一层函数调用进入下一层函数调用的递推。此时,中的一个称为孤儿的类就会收留这个对象。禁止访问服务器拒绝请求服务器找不到请求的页面服务器内部错误坏的网关一般是网关服务器请求后端服务时,后端服务没有按照协议正确返回结果。 持续更新。。。。 php 1. 简述 php 中的 autoload Autoload 的加载机制,当通过 new 来实例化一个类时,PHP 会通过定义的 autol...

    wangbjun 评论0 收藏0

发表评论

0条评论

ShevaKuilin

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<