尊龙凯时人生就是博

ÔõÑùÔÚThinkPHP6ÖоÙÐÐÊý¾Ý¿âË®Öзֿâ²Ù×÷£¿

Ëæ×ÅÓªÒµ¹æÄ£µÄÀ©´ó£¬Êý¾Ý¿âËùÐè´¦ÀíµÄÊý¾ÝÁ¿Ò²Ò»Ö±ÔöÌí£¬µ¼Ö¼òµ¥Êý¾Ý¿âÃæÁÙ×ÅѹÁ¦¡£Õâʱ¼äÎÒÃǾÍÐèÒª¾ÙÐÐÊý¾Ý¿âË®Öзֿâ²Ù×÷£¬½«Êý¾ÝÊèÉ¢µ½²î±ðµÄÊý¾Ý¿âÖУ¬´Ó¶øÌá¸ßϵͳµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ¡£±¾ÎĽ«ÏÈÈÝÔÚthinkphp6ÖÐÔõÑù¾ÙÐÐÊý¾Ý¿âË®Öзֿâ²Ù×÷¡£

Ò»¡¢Ê²Ã´ÊÇÊý¾Ý¿âË®Öзֿ⣿

Êý¾Ý¿âË®ÖзֿâÊǽ«Ò»¸öÊý¾Ý¿âÖеÄÊý¾ÝÊèÉ¢µ½¶à¸öÊý¾Ý¿âÖеÄÀú³Ì¡£ÎÒÃÇ¿ÉÒÔ½«Êý¾Ýƾ֤ijÖÖ¹æÔò£¨ºÃ±Èƾ֤Óû§ID»òʱ¼ä¶Î£©»®·Öµ½²î±ðµÄÊý¾Ý¿âÖУ¬´Ó¶ø½µµÍ¼òµ¥Êý¾Ý¿âµÄ¸ºÔØѹÁ¦¡£Í¬Ê±£¬ÔÚÊý¾ÝÁ¿´óµÄÇéÐÎÏ£¬Ë®Öзֿ⻹ÄÜÌá¸ßÅÌÎÊЧÂÊ£¬ÔöÇ¿Êý¾ÝÇå¾²ÐÔ¡£

¶þ¡¢ThinkPHP6ÖÐË®ÖзֿâµÄʵÏÖ

ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÃÊý¾Ý¿âÖÐÐļþµÄ·½·¨À´ÊµÏÖË®Öзֿâ¡£ ½«Êý¾Ý¿âÖÐÐļþ·ÅÔÚThinkPHP6µÄMySQLÅþÁ¬ÖУ¬ÓÃÓÚ¿ØÖÆ·Ö¿â¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

×°ÖÃThinkswoole

ÔÚThinkPHP6ÖУ¬½ÓÄÉThinkswoole×÷ΪÊý¾Ý¿âÖÐÐļþ¡£ÎÒÃÇÐèÒªÔÚÏîÄ¿ÖÐ×°ÖÃThinkswoole¡£

ÔÚcomposer.jsonÎļþÖмÓÈëThinkSwooleµÄ°æ±¾ÐÅÏ¢£¬È»ºóʹÓÃcomposer¾ÙÐÐ×°Öá£

ÐÞ¸ÄÊý¾Ý¿âÉèÖÃ

Ê×ÏÈÕÒµ½config/database.phpÎļþ£¬½«MySQLÅþÁ¬Ìæ»»³ÉSwooleÅþÁ¬¡£×¢Ê͵ôÔ­À´µÄMySQLÅþÁ¬ÐÅÏ¢£º

// 'mysql' => [
    //     // ĬÈÏÊý¾ÝÅþÁ¬±êʶ
    //     'default' => env('database.driver', 'mysql'),
    //     // Êý¾Ý¿âÅþÁ¬ÐÅÏ¢
    //     'connections' => [
    //         'mysql' => [
    //             // Êý¾Ý¿âÀàÐÍ
    //             'type' => 'mysql',
    //             // Ö÷»úµØµã
    //             'host' => env('database.hostname', '127.0.0.1'),
    //             // Êý¾Ý¿âÃû
    //             'database' => env('database.database', ''),
    //             // ̞
    //             'username' => env('database.username', 'root'),
    //             // ÃÜÂë
    //             'password' => env('database.password', ''),
    //             // ¶Ë¿Ú
    //             'hostport' => env('database.hostport', '3306'),
    //             // Êý¾Ý¿âÅþÁ¬²ÎÊý
    //             'params' => [],
    //             // Êý¾Ý¿â±àÂëĬÈϽÓÄÉutf8
    //             'charset' => 'utf8',
    //             // Êý¾Ý¿â±íǰ׺
    //             'prefix' => env('database.prefix', ''),
    //             // Êý¾Ý¿âµ÷ÊÔģʽ
    //             'debug' => env('database.debug', true),
    //             // Êý¾Ý¿â°²ÅÅ·½·¨:0 ¼¯ÖÐʽ(¼òµ¥Ð§ÀÍÆ÷),1 ÂþÑÜʽ(Ö÷´ÓЧÀÍÆ÷)
    //             'deploy' => 0,
    //             // Êý¾Ý¿â¶ÁдÊÇ·ñÊèÉ¢ Ö÷´ÓʽÓÐÓÃ
    //             'rw_separate' => false,
    //             // ¶ÁдÊèÉ¢ºó Ö÷ЧÀÍÆ÷ÊýÄ¿
    //             'master_num' => 1,
    //             // Ö¸¶¨´ÓЧÀÍÆ÷ÐòºÅ
    //             'slave_no' => '',
    //             // ÊÇ·ñÑÏ¿á¼ì²é×Ö¶ÎÊÇ·ñ±£´æ
    //             'fields_strict' => true,
    //             // Êý¾Ý¼¯·µ»ØÀàÐÍ
    //             'resultset_type' => 'array',
    //             // ×Ô¶¯Ð´Èëʱ¼ä´Á×Ö¶Î
    //             'auto_timestamp' => false,
    //             // ʱ¼ä×Ö¶ÎÈ¡³öºóµÄĬÈÏʱ¼äÃûÌÃ
    //             'datetime_format' => false,
    //             // BuilderÀà
    //             'builder' => '',
    //             // QueryÀà
    //             'query' => '\think\db\Query',
    //             // ÊÇ·ñÐèÒª¾ÙÐÐSQLÐÔÄÜÆÊÎö
    //             'sql_explain' => false,
    //         ],
    //     ],
    // ],

µÇ¼ºó¸´ÖÆ

Ìí¼ÓSwooleÅþÁ¬ÐÅÏ¢£º

 // swoole
    'swoole' => [
        // ĬÈÏÊý¾ÝÅþÁ¬±êʶ
        'default' => 'swoole',
        // Êý¾Ý¿âÅþÁ¬ÐÅÏ¢
        'connections' => [
            'swoole' => [
                // Êý¾Ý¿âÀàÐÍ
                'type' => 'mysql',
                // ЧÀÍÆ÷µØµã
                'hostname' => [
                    '127.0.0.1:3305',
                    '127.0.0.1:3306',
                ],
                // Êý¾Ý¿âÃû
                'database' => 'test',
                // ̞
                'username' => 'root',
                // ÃÜÂë
                'password' => '',
                // ¶Ë¿Ú
                'hostport' => '',
                // Êý¾Ý¿âÅþÁ¬²ÎÊý
                'params' => [],
                // Êý¾Ý¿â±àÂëĬÈϽÓÄÉutf8mb4
                'charset' => 'utf8mb4',
                // Êý¾Ý¿â±íǰ׺
                'prefix' => '',
                // Êý¾Ý¿âµ÷ÊÔģʽ
                'debug' => true,
                // Êý¾Ý¿â°²ÅÅ·½·¨:0 ¼¯ÖÐʽ(¼òµ¥Ð§ÀÍÆ÷),1 ÂþÑÜʽ(Ö÷´ÓЧÀÍÆ÷)
                'deploy' => 0,
                // Êý¾Ý¿â¶ÁдÊÇ·ñÊèÉ¢ Ö÷´ÓʽÓÐÓÃ
                'rw_separate' => false,
                // ¶ÁдÊèÉ¢ºó Ö÷ЧÀÍÆ÷ÊýÄ¿
                'master_num' => 1,
                // Ö¸¶¨´ÓЧÀÍÆ÷ÐòºÅ
                'slave_no' => '',
                // ×Ô¶¯Ð´Èëʱ¼ä´Á×Ö¶Î
                'auto_timestamp' => false,
                // ʱ¼ä×Ö¶ÎÈ¡³öºóµÄĬÈÏʱ¼äÃûÌÃ
                'datetime_format' => 'Y-m-d H:i:s',
                // BuilderÀà
                'builder' => '',
                // QueryÀà
                'query' => '\think\db\Query',
                // ÊÇ·ñÐèÒª¾ÙÐÐSQLÐÔÄÜÆÊÎö
                'sql_explain' => false,
            ],
        ],
    ],

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÁ½¸öЧÀÍÆ÷µØµã£¨127.0.0.1:3305ºÍ127.0.0.1:3306£©£¬ÕâÊÇΪÁËʵÏִ󶼾ݽڵãµÄ·Ö¿â¡£Êý¾Ý¿âÃû¡¢Óû§Ãû¡¢ÃÜÂëµÈÐÅÏ¢ÎȹÌ¡£

½¨ÉèÊý¾Ý¿âÖÐÐļþ

ÔÚapp/middlewareĿ¼Ï½¨ÉèDb.phpµÄÊý¾Ý¿âÖÐÐļþ£¬Ìí¼ÓÒÔÏ´úÂ룺

<?php
namespace appmiddleware;

use thinkRequest;
use thinkContainer;

class Db
{
    public function handle(Request $request, Closure $next)
    {
        $serverIds = $this->getServerIds($request);
        //½ç˵һ¸öÅþÁ¬³Ø
        $conns = [];
        foreach($serverIds as $sid) {
            $sid = $request->$sid;
            if(empty($conns[$sid])) {
                $conns[$sid] = Container::getInstance()
                                         ->make('db')->connect($sid);
            }
        }
        Container::getInstance()->bind('db', function() use ($conns) {
            return $conns;
        });
        return $next($request);
    }

    protected function getServerIds(Request $request)
    {
        return ['uid'];
    }
}

µÇ¼ºó¸´ÖÆ

ÕâÀィÉèÁËÒ»¸öÃûΪDbµÄÖÐÐļþ¡£ÔÚhandleÒªÁìÖУ¬Ê×ÏÈ»ñÈ¡Ä¿½ñÇëÇóµÄЧÀÍÆ÷IDÊý×顣ȻºóÒÀ´Î½«ÕâЩЧÀÍÆ÷µØµãÓëÅþÁ¬³Ø$consÖÐÒÑÓеĵصã½ÏÁ¿£¬ÈôÊDz»±£´æ¾Í¼ÓÈëÅþÁ¬³ØÖС£×îºó½«ÅþÁ¬³Ø$conns°ó¶¨µ½ÈÝÆ÷ʵÀýÖС£ÔÚgetServerIdsÒªÁìÖУ¬ÎÒÃÇ¿ÉÒÔÉèÖÃЧÀÍÆ÷IDµÄÃû³Æ£¬ÕâÀïĬÒÔΪuid¡£

×¢²áÖÐÐļþ

ÔÚconfig/middleware.phpÖмÓÈëÒÔÏ´úÂ룺

return [
    ...
    appmiddlewareDb::class,
];

µÇ¼ºó¸´ÖÆ

Õâ¶Î´úÂëÊÇÓÃÀ´×¢²áÖÐÐļþµÄ£¬ÔÚÖÐÐļþÖ´ÐÐÔ˶¯ÁбíÖÐÌí¼ÓÁËÎÒÃǵÄDbÖÐÐļþ¡£

ʵÏÖ·Ö¿â²Ù×÷

½ÓÏÂÀ´£¬ÎÒÃǽ«ÊµÏÖÔÚÄ£×ÓÖÐË®Öзֿâ²Ù×÷¡£ÕâÀïÒÔÓû§±íΪÀý£¬½«Óû§IDÒÔ10ÍòΪһ¸ö¿âµÄ½çÏß¾ÙÐзÖƬ²Ù×÷£¬ÌåÏÖÓû§IDÔÚ0-10ÍòÖ®¼äµÄÊý¾Ý´æ´¢ÔÚÒ»¸öÊý¾Ý¿âÀÒÔ´ËÀàÍÆ£¬Ö±µ½½«Óû§IDÔÚ90Íò-100ÍòÖ®¼äµÄÊý¾Ý´æ´¢ÔÚµÚ10¸öÊý¾Ý¿âÀï¡£

<?php
namespace appmodel;

use thinkModel;

class User extends Model
{
    protected $connection = [
        1 => 'user_1',
        2 => 'user_2',
        3 => 'user_3',
        4 => 'user_4',
        5 => 'user_5',
        6 => 'user_6',
        7 => 'user_7',
        8 => 'user_8',
        9 => 'user_9',
        10 => 'user_10',
    ];

    protected $pk = 'uid';

    public function getTableName(): string
    {
        $id = ceil($this->id / 100000);
        return $this->connection[$id] . '.' . $this->table;
    }
}

µÇ¼ºó¸´ÖÆ

ÕâÀïÎÒÃǽç˵ÁË10¸öÊý¾Ý¿âÅþÁ¬£¬Ã¿¸öÅþÁ¬ÌåÏÖÒ»¸öÊý¾Ý¿â·ÖƬ£¬ÊµÏÖÁËË®ÖзֿâµÄÄ¿µÄ¡£½Ó×ÅÎÒÃǽç˵getTableNameÒªÁ죬ÓÃÓÚ»ñÈ¡Ä¿½ñÄ£×Ó¶ÔÓ¦µÄÊý¾Ý±íÃû¡£Æ¾Ö¤Ä£×ÓÖеÄÖ÷¼üIDÖµÅÌËã³öÐèÒª»á¼ûµÄÊý¾Ý¿âÅþÁ¬£¬·µ»ØÊý¾Ý¿âÅþÁ¬ºÍÊý¾Ý±íÃû³ÆµÄ×éºÏ¡£

×ܽ᣺

±¾ÎÄÏÈÈÝÁËÔÚThinkPHP6ÖеÄË®Öзֿâ²Ù×÷¡£Ëæ×ÅÓªÒµµÄÒ»Ö±À©Õ¹ºÍÊý¾Ý¹æÄ£µÄÔöÌí£¬Ë®Öзֿâ¿ÉÒÔÌá¸ßϵͳµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ£¬ÒÔ¼°ÔöÇ¿Êý¾ÝÇå¾²ÐÔ¡£ÔÚThinkPHP6ÖпÉÒÔʹÓÃThinkswooleÖÐÐļþµÈÒªÁìʵÏÖË®Öзֿâ²Ù×÷¡£

ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖоÙÐÐÊý¾Ý¿âË®Öзֿâ²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ