• laravel学习:主从读写分离配置的实现


    本篇文章给大家带来的内容是关于laravel学习:主从读写分离配置的实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    在DB的连接工厂中找到以下代码
    .../vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    /**

     * 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']);

    }

    链接:https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g 提取码:x2p5

    免费分享,但是X度限制严重,如若链接失效点击链接或搜索加群 群号518475424

    工厂类通过随机获取读DB配置来进行读取操作,由此可推出DB的配置应该如下

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    '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'    => '',

    ]

    加强版,支持多主多从,支持独立用户名和密码,配置如下

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    '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'    => '',

    ]

    验证
    开启MySQL的general-log,通过tail -f的方式监控log变化来确定配置是否生效

  • 相关阅读:
    通过system调用Am命令执行动作
    windows中如何在命令行启动启动程序
    UICC 实现框架和数据读写
    软件设计方法(转载)
    好诗欣赏——邀请 The Invitation
    leaflet使用turfjs插件,基于格点数据绘制等值线效果
    深信服防火墙做端口映射
    关于本博客的一些声明
    sqlserver – SQL Server – 包含多个字段的IN子句
    JavaScript Array join() 方法
  • 原文地址:https://www.cnblogs.com/it-3327/p/11799611.html
Copyright © 2020-2023  润新知