有關laravel5.2和redis_cluster配置的介紹

巴扎黑
發布: 2023-03-15 10:28:02
原創
1966 人瀏覽過


摘要:laravel中redis叢集的應用這部分我想分享下laravel5.2中redis叢集的設定(官網也有redis叢集的設定講解,但是5.2版還是有點不足,只​​是說了將cluster配置項設為true,但光這樣一個選項不能代表,一個新手直接可用redis集...

laravel中redis集群的應用

這部分我想分享下laravel5.2中redis集群的配置(官網也有redis集群的配置講解,但是5.2版還是有點不足,只​​是說了將cluster配置項設為true,但光這樣一個選項不能代表,一個新手直接可用redis集群,這部分還包括predis客戶端的事,所以後面我也會分享下關於predis的源碼分析)。建置:Easy Building Redis-cluster (輕鬆搭建reids叢集)

系統軟體清單:

    laravel版本:5.2
  • redis版本:>=3.0下載最新版即可
  • #predis:>=1.0下載位址

設定檔:config/database.php

'redis' => [    'cluster' => env('REDIS_CLUSTER', true),
    **'options'=>['cluster'=>'redis']**,  //官网没说这个,这是必须的!!后面会讲为什么这么配?
    'default' => [        'host' => env('REDIS_HOST', '127.0.0.1'), //任选一个master节点
        'port' => env('REDIS_PORT',6379),        'database' => 0,        'timeout'=>15,        'read_write_timeout'=>1800 //redis客户端连接以后的读写超时时间(默认是60s)
    ],    'extra'=>[                      
        'host'=>env('REDIS_EXTRA_HOST','127.0.0.1'),  //任意一个集群中的节点即可
        'port'=>env('REDIS_EXTRA_PORT',7001)
    ]
 ]
登入後複製

ok,配完上面的步驟,redis叢集就可以用了. 
具體使用redis集群的應用場景根據業務需求有很多種,比如集群存session等.
app('request')->session()->put('key','value ');就存到叢集中了.

predis對redis叢集的底層實作

ok,想要了解設定檔中的參數,還是得看來源程式碼,當然也是predis,上程式碼.

Illuminate\Support\ServiceProvider\RedisServiceProvider;    public function register()
    {        $this->app->singleton('redis', function ($app) {            return new Database($app['config']['database.redis']);
        });
    }
 

 Illuminate\Redis\Database;    
     public function __construct(array $servers = [])
     {
        $cluster = Arr::pull($servers, 'cluster');   //获取'cluster'的键值
       
        $options = (array) Arr::pull($servers, 'options');       //options 就是database.php中'options'的键值,是一个数组(但官网没有提到,是个坑.)
        if ($cluster) {  
            $this->clients = $this->createAggregateClient($servers, $options);   //集群模式'cluster=true'
        } else {       
             $this->clients = $this->createSingleClients($servers, $options);   //单机模式 'cluster=false'
        }
    }   protected function createAggregateClient(array $servers, array $options = [])
    {                
        return ['default' => new Client(array_values($servers), $options)];   //predis的Client类
    }
    


----------


注意:这里提醒一下各参数的值:
此时$servers=[
    [      'host' => env('REDIS_HOST', '127.0.0.1'),      'port' => env('REDIS_PORT',6379),      'database' => 0,      'timeout'=>15,      'read_write_timeout'=>1800
    ],
    [      'host'=>env('REDIS_EXTRA_HOST','127.0.0.1'),      'port'=>env('REDIS_EXTRA_PORT',7001)
    ]
]
$options = ['cluster'=>'redis']

其实到这儿,就可以解释在database.php中增加options选项,而且是必选项,因为底层代码需要判断数据切片的方式.
除了看源码,
predis的包文档也做了解释.https://packagist.org/packages/predis/predis-------
登入後複製

接下來我們來看看這些底層要初始化的類別吧.

  Predis\Client;      public function __construct($parameters = null, $options = null)
    {        $this->options = $this->createOptions($options ?: array());        #$this->connection = $this->createConnection($parameters ?: array());
        #$this->profile = $this->options->profile;
    }    
    protected function createOptions($options)
    {        if (is_array($options)) {            return new Options($options);  //如你所见,实例化Options类
        }        if ($options instanceof OptionsInterface) {            return $options;
        }        throw new \InvalidArgumentException('Invalid type for client options.');
    }   
    public function __construct(array $options = array())
    {        $this->input = $options;        $this->options = array();        $this->handlers = $this->getHandlers();
    }
登入後複製

$this->connection = $this-> ;createConnection($parameters ?: array())

Predis\Client 文件

    protected function createConnection($parameters)
    {
       # if ($parameters instanceof ConnectionInterface) {
       #     return $parameters;
       # }

       # if ($parameters instanceof ParametersInterface || is_string($parameters)) {
       #     return $this->options->connections->create($parameters);
       # }

       # if (is_array($parameters)) {
       #     if (!isset($parameters[0])) {
       #         return $this->options->connections->create($parameters);
       #     }            $options = $this->options;

       #     if ($options->defined('aggregate')) {
       #         $initializer = $this->getConnectionInitializerWrapper($options->aggregate);
       #         $connection = $initializer($parameters, $options);
       #     } else {
       #         if ($options->defined('replication') && $replication = $options->replication) {
       #             $connection = $replication;
       #         } else {                
                    $connection = $options->cluster; //
       #         }                $options->connections->aggregate($connection, $parameters);
       #     }

            return $connection;
       # }

       # if (is_callable($parameters)) {
       #     $initializer = $this->getConnectionInitializerWrapper($parameters);
       #     $connection = $initializer($this->options);

       #     return $connection;
       # }

       # throw new \InvalidArgumentException('Invalid type for connection parameters.');
    }
    
Predis\Configuration\Options;

    protected function getHandlers()
    {
        return array(            'cluster' => 'Predis\Configuration\ClusterOption',            'connections' => 'Predis\Configuration\ConnectionFactoryOption',
            #'exceptions' => 'Predis\Configuration\ExceptionsOption',
            #'prefix' => 'Predis\Configuration\PrefixOption',
            #'profile' => 'Predis\Configuration\ProfileOption',
            #'replication' => 'Predis\Configuration\ReplicationOption',
        );
    }

    public function __get($option)
    {
        #if (isset($this->options[$option]) || array_key_exists($option, $this->options)) {
        #    return $this->options[$option];
        #}         if (isset($this->input[$option]) || array_key_exists($option, $this->input)) {            $value = $this->input[$option];
            unset($this->input[$option]);

        #    if (is_object($value) && method_exists($value, '__invoke'){
        #        $value = $value($this, $option);
        #    }            if (isset($this->handlers[$option])) {                $handler = $this->handlers[$option];                $handler = new $handler(); //会实例化Predis\Configuration\ClusterOption类
                $value = $handler->filter($this, $value);
            }

            return $this->options[$option] = $value;
        }

       # if (isset($this->handlers[$option])) {
       #     return $this->options[$option] = $this->getDefault($option);
       # }

       # return;
    }
    
Predis\Configuration\ClusterOption文件

    public function filter(OptionsInterface $options, $value)
    {        if (is_string($value)) {            $value = $this->createByDescription($options, $value);
        }

       # if (!$value instanceof ClusterInterface) {
       #     throw new \InvalidArgumentException(
       #         "An instance of type 'Predis\Connection\Aggregate\ClusterInterface' was expected."
       #     );
       # }

        return $value;
    }
       
       
        protected function createByDescription(OptionsInterface $options, $id)
    {
        switch ($id) {
         * Abstraction for a cluster of aggregate connections to various Redis servers
 * implementing client-side sharding based on pluggable distribution strategies.
          # case 'predis':
          # case 'predis-cluster':
          #      return new PredisCluster(); 
          //这个模式是客户端通过CRC16算法在客户端进行数据切片,
          显然这种模式的集群是脆弱的,如果一个master节点挂了,
          那其备节点若也挂了,那么获取数据就成问题了;
          再有这种模式扩展性很差,维护成本高,
          因此这个模式不推荐.当然用最新predis不存在这个问题.
          我这边predis,1.0算比较老了.

            case 'redis':
            case 'redis-cluster':
                return new RedisCluster($options->connections);            //这种模式是基于服务端的数据切片,相较于第一种模式,优点也显而易见,维护成本低,扩展性好等.
            default:
                return;
        }
    } 
    
        public function __get($option)
    {
        #if (isset($this->options[$option]) || array_key_exists($option, $this->options)) {
        #    return $this->options[$option];
        #}

        # if (isset($this->input[$option]) || array_key_exists($option, $this->input)) {
        #    $value = $this->input[$option];
        #    unset($this->input[$option]);

        #    if (is_object($value) && method_exists($value, '__invoke'){
        #        $value = $value($this, $option);
        #    }

        #    if (isset($this->handlers[$option])) {
        #       $handler = $this->handlers[$option];
        #        $handler = new $handler(); 
        #        $value = $handler->filter($this, $value);
        #    }

        #    return $this->options[$option] = $value;
        #}         if (isset($this->handlers[$option])) {  //$options='connections'       
            return $this->options[$option] = $this->getDefault($option);
       # }

       # return;
    }  
    
    public function getDefault($option)
    {        if (isset($this->handlers[$option])) {            $handler = $this->handlers[$option]; //$handler = 'Predis\Configuration\ConnectionFactoryOption';
            $handler = new $handler();

            return $handler->getDefault($this);
        }
    }
    
 Predis\Configuration\ConnectionFactoryOption文件
    public function getDefault(OptionsInterface $options)
    {
        return new Factory(); //最后实例化了一个'工厂'类
    }
登入後複製

$this->profile = $this->options->profile;


Predis\Configuration\ProfileOption文件


        public function __get($option)
    {
        #if (isset($this->options[$option]) || array_key_exists($option, $this->options)) {
        #    return $this->options[$option];
        #}

        # if (isset($this->input[$option]) || array_key_exists($option, $this->input)) {
        #    $value = $this->input[$option];
        #    unset($this->input[$option]);

        #    if (is_object($value) && method_exists($value, '__invoke'){
        #        $value = $value($this, $option);
        #    }

        #    if (isset($this->handlers[$option])) {
        #       $handler = $this->handlers[$option];
        #        $handler = new $handler(); 
        #        $value = $handler->filter($this, $value);
        #    }

        #    return $this->options[$option] = $value;
        #}         if (isset($this->handlers[$option])) {  //$options='profile'       
            return $this->options[$option] = $this->getDefault($option);
       # }

       # return;
    }  
    
        public function getDefault($option)
    {        if (isset($this->handlers[$option])) {            $handler = $this->handlers[$option]; //$handler = 'Predis\Configuration\ProfileOption';
            $handler = new $handler();

            return $handler->getDefault($this);
        }
    }
    
 Predis\Configuration\ProfileOption文件
     public function getDefault(OptionsInterface $options)
    {        $profile = Factory::getDefault(); //实例化了Predis\Profile\RedisVersion300类
        $this->setProcessors($options, $profile);

        return $profile;  
    }
登入後複製

以上是有關laravel5.2和redis_cluster配置的介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!