thinkphpÔõôÉèÖÃÊý¾Ý¿âÅþÁ¬³Ø
Ò»¡¢Ê²Ã´ÊÇÊý¾Ý¿âÅþÁ¬³Ø
¹Å°åÊý¾Ý¿âÅþÁ¬ÊÇÒ»ÖÖ¶ÀÍÌ×ÊÔ´µÄ·½·¨£¬Ã¿¸öÅþÁ¬ÐèÒªÏûºÄϵͳ×ÊÔ´£¬ÈôÊDz¢·¢Óû§½Ï¶à£¬ÄÇô¾Í»áµ¼ÖÂϵͳ×ÊÔ´µÄÆÌÕźÍÏìÓ¦ÑÓ³ÙµÈÎÊÌâ¡£¶øÊý¾Ý¿âÅþÁ¬³ØÊÇÒ»ÖÖÅþÁ¬¹²ÏíµÄ·½·¨£¬½«ÅþÁ¬»º´æµ½ÅþÁ¬³ØÖУ¬¶à¸öÏ߳̿ÉÒÔ¹²Ïíͳһ¸öÅþÁ¬³ØÖеÄÅþÁ¬£¬´Ó¶øïÔÌϵͳ×ÊÔ´µÄÏûºÄ¡£
¶þ¡¢thinkphpÔõÑùÉèÖÃÊý¾Ý¿âÅþÁ¬³Ø
1.ÔÚÓ¦ÓÃÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÄÚÈÝ
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
return [ //Êý¾Ý¿âÉèÖÃÐÅÏ¢ 'database' => [ // Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', // ЧÀÍÆ÷µØµã 'hostname' => '127.0.0.1', // Êý¾Ý¿âÃû 'database' => 'test', // Óû§Ãû 'username' => 'root', // ÃÜÂë 'password' => '', // ¶Ë¿Ú 'hostport' => '', // Êý¾Ý¿âÅþÁ¬²ÎÊý 'params' => [ // Êý¾Ý¿âÅþÁ¬³ØÉèÖà \think\helper\Arr::except(\Swoole\Coroutine::getContext(),'__timer'), ], // Êý¾Ý¿â±àÂëĬÈϽÓÄÉutf8 'charset' => 'utf8', // Êý¾Ý¿â±íǰ׺ 'prefix' => 'think_', ], ];
µÇ¼ºó¸´ÖÆ
2.ÔÚÈë¿ÚÎļþindex.phpÖмÓÈëÒÔÏÂÄÚÈÝ
use think\App; use think\facade\Config; use think\facade\Db; use think\swoole\Server; use think\swoole\websocket\socketio\Handler; use think\swoole\websocket\Websocket; use think\swoole\websocket\socketio\Packet; use think\swoole\coroutine\Context; use Swoole\Database\PDOPool; use Swoole\Coroutine\Scheduler; //½ç˵ӦÓÃĿ¼ define('APP_PATH', __DIR__ . '/app/'); // ¼ÓÔØ¿ò¼ÜÖ¸µ¼Îļþ require __DIR__ . '/thinkphp/vendor/autoload.php'; require __DIR__ . '/thinkphp/bootstrap.php'; // À©Õ¹Loader×¢²áµ½×Ô¶¯¼ÓÔØ \think\Loader::addNamespace('swoole', __DIR__ . '/thinkphp/library/swoole/'); // ³õʼ»¯Ó¦Óà App::getInstance()->initialize(); //»ñÈ¡Êý¾Ý¿âÉèÖÃÐÅÏ¢ $dbConfig = Config::get('database'); //½¨ÉèÊý¾Ý¿âÅþÁ¬³Ø $pool = new PDOPool($dbConfig['type'], $dbConfig); //ÉèÖÃÅþÁ¬³ØµÄ²ÎÊý $options = [ 'min' => 5, 'max' => 100, ]; $pool->setOptions($options); //ÅþÁ¬³Øµ¥Àýģʽ Context::set('pool', $pool); //Æô¶¯Swoole server $http = (new Server())->http('0.0.0.0', 9501)->set([ 'enable_static_handler' => true, 'document_root' => '/data/wwwroot/default/public/static', 'worker_num' => 2, 'task_worker_num' => 2, 'daemonize' => false, 'pid_file' => __DIR__.'/swoole.pid' ]); $http->on('WorkerStart', function (swoole_server $server, int $worker_id) { //¹¦Ð§ÊµÏÖ }); $http->start();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëµÄ¹¦Ð§Êǹ¹½¨Ò»¸öPDOPoolÅþÁ¬³Ø£¬²¢½«Æä×îµÍÅþÁ¬ÊýÉèΪ5£¬×î¸ßÅþÁ¬ÊýÉèΪ100¡£Ê¹ÓÃContext½«ÅþÁ¬³Ø´æ´¢ÔÚÄÚ´æÖУ¬ÒÔ¹©À©Õ¹µÄthinkphpÓ¦ÓóÌÐòʹÓá£
Èý¡¢ÅþÁ¬³ØµÄʹÓÃÒªÁì
ÔÚʹÓÃÅþÁ¬³ØµÄÀú³ÌÖУ¬ÐèҪעÖØÒÔϼ¸µã£º
ÅþÁ¬³ØµÄµ¥Àýģʽ£¬²î±ðµÄº¯ÊýʹÓÃͳһ¸öÅþÁ¬³Ø¹¤¾ß£¬°ü¹ÜÅþÁ¬³Ø²ÎÊýµÄÒ»ÖÂÐÔ¡£
²»ÒªÔÚÖ´ÐÐÍêÊý¾Ý¿â²Ù×÷ºóÂíÉ϶ÔMySQL¾ÙÐйرգ¬¶øÓ¦µ±Ö±½Ó½«ÆäËÍ»¹¸øÅþÁ¬³Ø¡£ÓÉÓÚÏÖʵÉÏÊǽ«ÅþÁ¬·Å»ØÅþÁ¬³ØÖУ¬¶ø²»ÊǹرÕÅþÁ¬¡£
²»Òª½«ÅþÁ¬³ØÊÓΪһ¸ö¡°²»ËÀÖ®Éí¡±£¬ËüÒ²ÐèÒªÊÍ·Å£¬ÊÍ·ÅÅþÁ¬³ØµÄÒªÁìΪ£º$pool->close()¡£
ÏÂÃæÊÇÒ»¸öʹÓÃÅþÁ¬³ØµÄʾÀý£º
<?php namespace app\index\controller; use think\Controller; use Swoole\Database\PDOPool; class Index extends Controller { public function index() { //»ñÈ¡ÅþÁ¬³Ø $pool = \Swoole\Coroutine::getContext('pool'); //´ÓÅþÁ¬³ØÖÐÈ¡³öÒ»¸öÅþÁ¬ $connection = $pool->getConnection(); //Ö´ÐвÙ×÷ $result = $connection->query('SELECT * FROM `user`'); //ËÍ»¹ÅþÁ¬¸øÅþÁ¬³Ø $pool->putConnection($connection); //·µ»ØЧ¹û return json($result); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇthinkphpÔõôÉèÖÃÊý¾Ý¿âÅþÁ¬³ØµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡