本篇文章给大家带来的内容是关于laravel学习:主从读写分离配置的实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
在DB的连接工厂中找到以下代码
.../vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php
/**
* Get the read configuration for a read / write connection.
*
* @param array $config
* @return array
*/
protected function getReadConfig(array $config)
{
$readConfig = $this->getReadWriteConfig($config, 'read');
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 array_except(array_merge($config, $merge), ['read', 'write']);
}
工厂类通过随机获取读DB配置来进行读取操作,由此可推出DB的配置应该如下
'mysql' => [
'write' => [
'host' => '192.168.1.180',
],
'read' => [
['host' => '192.168.1.182'],
['host' => '192.168.1.179'],
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]
加强版,支持多主多从,支持独立用户名和密码,配置如下
'mysql' => [
'write' => [
[
'host' => '192.168.1.180',
'username' => '',
'password' => '',
],
],
'read' => [
[
'host' => '192.168.1.182',
'username' => '',
'password' => '',
],
[
'host' => '192.168.1.179',
'username' => '',
'password' => '',
],
],
'driver' => 'mysql',
'database' => 'database',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]
以上就是Swoole实现Websocket每秒钟主动推送的详细内容,觉得对你有帮助记得点个关注!!
小编结合以上知识体系整理了一套BAT面试真题+架构技术资料,退可提升软硬实力,进可收获高薪好offer。资料内容涵盖BAT架构技术与面试真题,减少你到处搜索资料的时间,成体系的实战技术知识,让你面试、进阶都更加容易。
领取方式:对面试文档总结感兴趣的帮可以点下面链接进入石墨文档了解大型互联网企业面试答案获取详情!
PHP进阶学习笔记、面试;文档、视频资源免费获取shimo.im