×ðÁú¿­Ê±

thinkphpÔõÑùÉèÖÃÊý¾Ý¿âÅþÁ¬³Ø

Ëæ×Å»¥ÁªÍøÓ¦ÓÃÔ½À´Ô½ÖØ´ó £¬¹ØÓÚÊý¾Ý¿âµÄÐèÇóÒ²Ô½À´Ô½¸ß¡£Ôڸ߲¢·¢µÄÇéÐÎÏ £¬¹Å°åµÄÊý¾Ý¿âÅþÁ¬·½·¨ÍùÍùÎÞ·¨Öª×ãÐèÇó £¬Õâʱ¼äÊý¾Ý¿âÅþÁ¬³Ø¾ÍÏÔµÃÓÈΪÖ÷Òª¡£ÔÚʹÓÃ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-&gt;query('SELECT * FROM `user`');
        
        //ËÍ»¹ÅþÁ¬¸øÅþÁ¬³Ø
        $pool-&gt;putConnection($connection);
        
        //·µ»ØЧ¹û
        return json($result);
    }
}

µÇ¼ºó¸´ÖÆ

ËÄ¡¢×ܽá

ʹÓÃÊý¾Ý¿âÅþÁ¬³Ø¿ÉÒÔÏÔÖøÌá¸ßÊý¾Ý¿âµÄÐÔÄÜ £¬²¢ÇÒïÔÌ­Á˹ŰåÅþÁ¬µÄÆÌÕÅ×ÊÔ´¡£Í¨¹ýÉÏÊö°ì·¨ £¬ÎÒÃÇ¿ÉÒÔÔÚthinkphp¿ò¼ÜÖÐÉèÖúÍʹÓÃÊý¾Ý¿âÅþÁ¬³Ø¡£ËäÈ» £¬Ê¹ÓÃÅþÁ¬³ØʱÐèÒª°ü¹ÜÅþÁ¬µÄºÏÀíÊÍ·Å £¬ÒÔÃâϵͳ×ÊÔ´ÆÌÕźÍÐÔÄÜϽµ¡£

ÒÔÉϾÍÊÇthinkphpÔõÑùÉèÖÃÊý¾Ý¿âÅþÁ¬³ØµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

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

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

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