配置范例
'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'
],
],
...
]
擴(kuò)展配置范例
'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',
],
...
]
公司數(shù)據(jù)庫架構(gòu)為一主多從,從庫訪問地址為唯一地址,該處方便負(fù)載均衡及擴(kuò)展從庫。所以最終線上采用的配置
'mysql' => [
'driver' => 'mysql',
'write' => [
'host' => '192.168.1.180',
'username' => 'write',
'password' => 'write',
],
'read' => [
'host' => '192.168.1.179'
'username' => 'read',
'password' => 'read',
],
...
]
代碼分析
授人以魚不如授人以漁,之所以配置如此靈活的原因,以及如何查找到這些配置方式。主要通過查找代碼,分析代碼;相關(guān)代碼都在下面粘出,這里就不做解釋了,代碼能說明一切;
路徑: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::forget($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 forget($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數(shù)據(jù)庫讀寫分離配置的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Laravel如何友好的修改.env配置文件詳解
- Nginx中運(yùn)行PHP框架Laravel的配置文件分享
- Laravel框架環(huán)境與配置操作實例分析
- Laravel 前端資源配置教程
- laravel config文件配置全局變量的例子
- Laravel 數(shù)據(jù)庫加密及數(shù)據(jù)庫表前綴配置方法
- laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫示例
- laravel-admin自動生成模塊,及相關(guān)基礎(chǔ)配置方法
- laravel 配置路由 api和web定義的路由的區(qū)別詳解
- Laravel5.6框架使用CKEditor5相關(guān)配置詳解
- Laravel配置全局公共函數(shù)的方法步驟
- Laravel5框架自定義錯誤頁面配置操作示例
- laravel配置Redis多個庫的實現(xiàn)方法
- nginx實現(xiàn)一個域名配置多個laravel項目的方法示例
- laravel 框架配置404等異常頁面
- Laravel 5.5官方推薦的Nginx配置學(xué)習(xí)教程
- Laravel Memcached緩存驅(qū)動的配置與應(yīng)用方法分析
- Laravel 5+ .env環(huán)境配置文件詳解