尊龙凯时人生就是搏

SwooleÒì²½±à³Ìʵ¼ù£º´òÔì¸ßÐÔÄÜÅŶÓϵͳ

Ëæ×Å»¥ÁªÍøÓ¦ÓõĿìËÙÉú³¤£¬Ô½À´Ô½¶àµÄ¹«Ë¾×îÏÈÇãÏòÓÚʹÓÃÒì²½±à³ÌµÄ·½·¨À´Ìá¸ß´úÂëÐÔÄܺÍÓ¦ÓÃЧÂÊ¡£swooleÊÇphpµÄÒ»¸öÇ¿Ê¢µÄÒì²½±à³Ì¿ò¼Ü£¬ÓµÓиßÐÔÄÜ¡¢¸ß²¢·¢ÐÔºÍ׿ԽµÄ¿ÉÀ©Õ¹ÐÔ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃswooleÀ´¹¹½¨Ò»¸ö¸ßÐÔÄܵÄÅŶÓϵͳ¡£

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÏàʶʲôÊÇÅŶÓϵͳ¡£ÅŶÓϵͳÊÇÒ»ÖÖЧÀÍͳ³ïµ÷Àíϵͳ£¬Ëüͨ¹ý¶Ô¸÷ÏîЧÀ;ÙÐÐÅŶÓÖÎÀíºÍµ÷Àí£¬Ìá¸ßЧÀ͵ÄÏìÓ¦ËÙÂʺÍϵͳµÄ²¢·¢´¦ÀíÄÜÁ¦¡£ÔÚÏÖʵӦÓÃÖУ¬ÅŶÓϵͳͨ³£ÓÃÓÚʵÏָ߲¢·¢»á¼û¡¢Ò첽ʹÃüµ÷Àí¡¢¸ºÔØƽºâµÈ¹¦Ð§£¬Òò´Ë£¬Æä¸ßÐÔÄܺ͸߿ÉÓÃÐÔÊDZØÐèµÄ¡£

½ÓÏÂÀ´£¬ÎÒÃǽ«ÒÔÏÂÃæµÄÐèÇóΪÀýÀ´½â˵ÔõÑùʹÓÃSwoole¹¹½¨Ò»¸ö¸ßÐÔÄܵÄÅŶÓϵͳ£º

Ö§³Ö¶à¸öÐÐÁУ¬²¢ÄܶÔÐÐö¾ÙÐÐÖÎÀí£»

Ö§³ÖʹÃüµÄÌí¼ÓºÍÖ´ÐУ¬²¢ÄܶÔʹÃü¾ÙÐÐ״̬ÖÎÀí£»

Ö§³Ö¶à¸öÏûºÄÕ߶ÔʹÃü¾ÙÐд¦Àí£¬²¢ÄܶÔÏûºÄÕß¾ÙÐÐÖÎÀí£»

Ö§³ÖʹÃüµÄÖØÊԺͳ¬Ê±´¦Àí£»

Ö§³ÖʹÃüµÄÒì²½´¦ÀíºÍͬ²½´¦Àí¡£

ÏÖÔÚ£¬ÈÃÎÒÃDz½ÈëÕýÌ⣬×îÏÈʹÓÃSwooleÀ´¹¹½¨Õâ¸ö¸ßÐÔÄܵÄÅŶÓϵͳ¡£

Ò»¡¢ÒýÈëSwoole

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÏîÄ¿ÖÐÒýÈëSwoole¡£ÕâÀïÎÒÃÇ¿ÉÒÔͨ¹ýComposerÀ´Àû±ãµØÒýÈëSwooleÒÀÀµ¡£

composer require swoole/swoole

¶þ¡¢¹¹½¨ÐÐÁÐ

ÔÚÅŶÓϵͳÖУ¬ÐÐÁÐÊǴ洢ʹÃüµÄ½¹µã½á¹¹¡£ÎÒÃÇÐèÒª¹¹½¨Ò»¸öÐÐÁУ¬²¢ÔÚÐÐÁÐÖÐÌí¼ÓʹÃü¡£ÕâÀïÎÒÃÇʹÓÃRedis×÷ΪÐÐÁд洢·½·¨£¬²¢Ê¹ÓÃPHP RedisÀ©Õ¹À´¶ÔÐÐö¾ÙÐвÙ×÷¡£

½¨ÉèRedisÅþÁ¬

ÔÚʹÓÃRedis֮ǰ£¬ÎÒÃÇÐèÒªÏȽ¨ÉèÓëRedisµÄÅþÁ¬¡£ÕâÀïÎÒÃǽ¨ÉèÒ»¸öRedisÅþÁ¬³ØÀ´ÖÎÀíRedisÅþÁ¬¡£

use SwooleCoroutineChannel;

class RedisPool

{

private $max;
private $pool;

public function __construct($max = 100)
{
    $this->max = $max;
    $this->pool = new Channel($max);
}

public function get($config)
{
    if (!$this->pool->isEmpty()) {
        return $this->pool->pop();
    }

    $redis = new Redis();
    $redis->connect($config['host'], $config['port']);
    $redis->select($config['db']);
    
    return $redis;
}

public function put($redis)
{
    if ($this->pool->length() < $this->max) {
        $this->pool->push($redis);
    } else {
        $redis->close();
    }
}

µÇ¼ºó¸´ÖÆ

}

½¨ÉèÐÐÁÐ

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÐÐÁÐÀàÀ´ÖÎÀíÐÐÁеIJÙ×÷£¬°üÀ¨Ê¹ÃüÌí¼Ó¡¢Ê¹Ãü»ñÈ¡ºÍʹÃüɾ³ýµÈ²Ù×÷¡£

class Queue

{

private $redis;

public function __construct($config)
{
    $this->redis = (new RedisPool())->get($config);
}

public function push($queueName, $data)
{
    $this->redis->lpush($queueName, $data);
}

public function pop($queueName)
{
    return $this->redis->rpop($queueName);
}

public function del($queueName, $data)
{
    $this->redis->lrem($queueName, -1, $data);
}

µÇ¼ºó¸´ÖÆ

}

Èý¡¢ÊµÏÖʹÃüÖ´ÐÐ

ÔÚÐÐÁÐÖÐÌí¼ÓʹÃüÖ®ºó£¬ÎÒÃÇÐèÒªÒ»¸öʹÃüÖ´ÐÐÕßÀ´Ö´ÐÐʹÃü¡£ÕâÀïÎÒÃÇʹÓÃЭ³ÌÀ´ÊµÏÖʹÃüµÄÒì²½Ö´ÐУ¬Í¬Ê±Ê¹ÓÃWorkerÀú³ÌÀ´Ìá¸ßʹÃüÖ´ÐÐЧÂÊ¡£

½¨ÉèWorkerÀú³Ì

ÔÚSwooleÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃWorkerÀú³ÌÀ´ÊµÏÖ¶àÀú³Ì´¦ÀíʹÃü¡£ÕâÀïÎÒÃǽ¨ÉèÒ»¸öWorkerÀú³ÌÀ´´¦ÀíʹÃü¡£

$worker = new SwooleProcessWorker();

½¨ÉèЭ³ÌÖ´ÐÐÕß

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öЭ³ÌÖ´ÐÐÕßÀ´´¦ÀíʹÃü¡£ÕâÀïÎÒÃÇʹÓÃЭ³ÌÀ´ÊµÏÖÒ첽ʹÃüÖ´ÐУ¬²¢Ê¹ÓÃGolangÆøÑæÆøÑæµÄЭ³Ì³ØÀ´Ìá¸ß²¢·¢´¦ÀíµÄЧÂÊ¡£

class CoroutineExecutor

{

private $pool;
private $redisConfig;

public function __construct($maxCoroutineNum, $redisConfig)
{
    $this->pool = new SwooleCoroutineChannel($maxCoroutineNum);
    $this->redisConfig = $redisConfig;

    for ($i = 0; $i < $maxCoroutineNum; $i++) {
        $this->pool->push(new Coroutine());
    }
}

public function execute($callback, $data)
{
    $coroutine = $this->pool->pop();
    $coroutine->execute($callback, $data, $this->redisConfig);
    $this->pool->push($coroutine);
}

µÇ¼ºó¸´ÖÆ

}

½¨ÉèЭ³Ì

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öЭ³ÌÀ´Ö´ÐÐʹÃü¡£

class Coroutine

{

private $redis;

public function __construct()
{
    $this->redis = null;
}

public function execute($callback, $data, $config)
{
    if (!$this->redis) {
        $this->redis = (new RedisPool())->get($config);
    }
    
    Coroutine::create(function () use ($callback, $data) {
        call_user_func($callback, $this->redis, $data);
    });
}

µÇ¼ºó¸´ÖÆ

}

ËÄ¡¢½¨ÉèЧÀÍ

×îºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃSwoole½¨ÉèÒ»¸öЧÀÍÀ´ÌṩÐÐÁÐÅÌÎʺÍʹÃüÌí¼ÓµÄ¹¦Ð§¡£

ʵÏÖÐÐÁÐÖÎÀí

ÎÒÃÇ¿ÉÒÔʹÓÃSwooleµÄHTTP ServerÀ´ÊµÏÖЧÀͶ˿ڼàÌý£¬Í¨¹ýHTTPÇëÇó·½·¨À´¾ÙÐÐÐÐÁÐÖÎÀí¡£ÕâÀïÎÒÃÇÌṩÁбí»ñÈ¡¡¢Ê¹Ãüɾ³ýºÍʹÃüÌí¼Ó½Ó¿Ú¡£

ʵÏÖʹÃüÖ´ÐÐ

ÎÒÃÇ¿ÉÒÔʹÓÃSwooleµÄTaskWorkerÀú³ÌÀ´ÊµÏÖʹÃüÖ´ÐС£Í¨¹ý½«Ê¹ÃüÅÉ·¢µ½TaskWorkerÀú³ÌÖУ¬ÔÙÓÉTaskWorkerÀú³ÌÒì²½Ö´ÐÐʹÃü¡£

class Task

{

public function execute($worker, $workerId, $taskId, $taskData)
{
    $executor = new CoroutineExecutor(64, [
        'host' => '127.0.0.1',
        'port' => 6379,
        'db' => 0
    ]);
    $executor->execute($taskData['callback'], $taskData['data']);

    return true;
}

µÇ¼ºó¸´ÖÆ

}

ʵÏÖЧÀÍÆô¶¯

×îºó£¬ÎÒÃÇ¿ÉÒÔʵÏÖЧÀÍÆô¶¯£¬¼àÌý¶Ë¿Ú£¬²¢Æô¶¯TaskWorkerÀú³ÌÀ´Ö´ÐÐʹÃü¡£

$http = new SwooleHttpServer(“127.0.0.1”, 9501);

$http->on(‘start’, function () {

echo "Server started

µÇ¼ºó¸´ÖÆ

“;

});

$http->on(‘request’, function ($request, $response) {

$queue = new Queue([
    'host' => '127.0.0.1',
    'port' => 6379,
    'db' => 0
]);

switch ($request->server['request_uri']) {
    case '/queue/list':
        // »ñÈ¡ÐÐÁÐÁбí
        break;
    case '/queue/delete':
        // ɾ³ýʹÃü
        break;
    case '/queue/add':
        $data = json_decode($request->rawContent(), true);
        $queue->push($data['queue'], $data['data']);
        $http->task([
            'callback' => function ($redis, $data) {
                // ʹÃüÖ´ÐÐÂß¼­
            },
            'data' => $data
        ]);
        break;
    default:
        $response->status(404);
        $response->end();
        break;
}

µÇ¼ºó¸´ÖÆ

});

$http->on(‘task’, function ($http, $taskId, $workerId, $data) {

$task = new Task();
$result = $task->execute($http, $workerId, $taskId, $data);

return $result;

µÇ¼ºó¸´ÖÆ

});

$http->on(‘finish’, function ($http, $taskId, $data) {

// ʹÃüÖ´ÐÐÍê³ÉÂß¼­

µÇ¼ºó¸´ÖÆ

});

$http->start();

Îå¡¢×ܽá

±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃSwooleÀ´ÊµÏÖÒ»¸ö¸ßÐÔÄܵÄÅŶÓϵͳ¡£Í¨¹ýSwooleµÄЭ³ÌºÍWorkerÀú³Ì£¬ÎÒÃÇ¿ÉÒÔʵÏÖÒ첽ʹÃüµÄ¸ßÐÔÄÜ´¦Àí£¬²¢Í¨¹ýRedis´æ´¢½á¹¹£¬ÊµÏÖ¸ßЧÂʵÄʹÃüÖÎÀíºÍµ÷Àí¡£ÕâÑùµÄÅŶÓϵͳ¿ÉÒÔÆÕ±éÓ¦ÓÃÓÚÒ첽ʹÃüµ÷Àí¡¢¸ß²¢·¢»á¼û¡¢¸ºÔØƽºâµÈ¹¦Ð§³¡¾°£¬ÊÇÒ»¸öÖµµÃÍƹãºÍʹÓõļƻ®¡£

ÒÔÉϾÍÊÇSwooleÒì²½±à³Ìʵ¼ù£º´òÔì¸ßÐÔÄÜÅŶÓϵͳµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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