Laravel数据库读写分离配置的方法
网络编程 2021-07-04 23:05www.168986.cn编程入门
今天长沙网络推广就为大家分享一篇Laravel数据库读写分离配置的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧
配置范例
'mysql' => [ 'driver' => 'mysql', 'write' => [ 'host' => '192.168.1.180', ], 'read' => [ ['host' => '192.168.1.182'], ['host' => '192.168.1.179'], ], ... ]
或
'mysql' => [ 'driver' => 'mysql', 'write' => [ 'host' => '192.168.1.180', ], 'read' => [ 'host' => [ '192.168.1.182', '192.168.1.179' ], ], ... ]
扩展配置范例
'mysql' => [ 'driver' => 'mysql', 'write' => [ 'host' => '192.168.1.180', 'username' => 'write', 'password' => 'write', ], 'read' => [ [ 'host' => '192.168.1.182', 'username' => 'read1', 'password' => 'read1', ], [ 'host' => '192.168.1.179', 'username' => 'read2', 'password' => 'read2', ], ], ... ]
或者
'mysql' => [ 'driver' => 'mysql', 'write' => [ 'host' => '192.168.1.180', 'username' => 'write', 'password' => 'write', ], 'read' => [ 'host' => [ '192.168.1.179', '192.168.1.182', ], 'username' => 'read', 'password' => 'read', ], ... ]
公司数据库架构为一主多从,从库访问地址为唯一地址,该处方便负载均衡及扩展从库。所以最终线上采用的配置
'mysql' => [ 'driver' => 'mysql', 'write' => [ 'host' => '192.168.1.180', 'username' => 'write', 'password' => 'write', ], 'read' => [ 'host' => '192.168.1.179' 'username' => 'read', 'password' => 'read', ], ... ]
代码分析
授人以鱼不如授人以渔,之所以配置如此灵活的原因,以及如何查找到这些配置方式。主要通过查找代码,分析代码;相关代码都在狼蚁网站SEO优化粘出,这里就不做解释了,代码能说明一切;
路径vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php
代码
class ConnectionFactory { ... / Get the read configuration for a read / write connection. @param array $config @return array / protected function getReadConfig(array $config) { $readConfig = $this->getReadWriteConfig($config, 'read'); if (isset($readConfig['host']) && is_array($readConfig['host'])) { $readConfig['host'] = count($readConfig['host']) > 1 ? $readConfig['host'][array_rand($readConfig['host'])] : $readConfig['host'][0]; } return $this->mergeReadWriteConfig($config, $readConfig); } ... / Get a read / write level configuration. @param array $config @param string $type @return array / protected function getReadWriteConfig(array $config, $type) { if (isset($config[$type][0])) { return $config[$type][array_rand($config[$type])]; } return $config[$type]; } ... / Merge a configuration for a read / write connection. @param array $config @param array $merge @return array / protected function mergeReadWriteConfig(array $config, array $merge) { return Arr::except(array_merge($config, $merge), ['read', 'write']); } ... } class Arr { ... / Get all of the given array except for a specified array of items. @param array $array @param array|string $keys @return array / public static function except($array, $keys) { static::fet($array, $keys); return $array; } ... / Remove one or many array items from a given array using "dot" notation. @param array $array @param array|string $keys @return void / public static function fet(&$array, $keys) { $original = &$array; $keys = (array) $keys; if (count($keys) === 0) { return; } foreach ($keys as $key) { $parts = explode('.', $key); while (count($parts) > 1) { $part = array_shift($parts); if (isset($array[$part]) && is_array($array[$part])) { $array = &$array[$part]; } else { $parts = []; } } unset($array[array_shift($parts)]); // clean up after each pass $array = &$original; } } ... }
以上这篇Laravel数据库读写分离配置的方法就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程