×ðÁú¿­Ê±¹ÙÍøµÇ¼

SwooleʵÏÖ¸ßÐÔÄܵÄRPCЧÀÍÆ÷

½üÄêÀ´£¬Ëæ×ÅÍøÂçÓ¦ÓõÄÒ»Ö±Éú³¤£¬Ô½À´Ô½¶àµÄÓ¦ÓóÌÐòÐèҪʵÏÖÔ¶³ÌÀú³ÌŲÓã¨remote procedure call£¬¼ò³Ærpc£©µÄ¹¦Ð§¡£¹Å°åµÄrpc¿ò¼ÜÈçdubbo¡¢thrift¡¢grpcµÈ¶¼Äܹ»Öª×ãÕâ·½ÃæµÄÐèÇ󣬿ÉÊÇËæ×ÅÓ¦ÓóÌÐòºÍÓªÒµµÄÔöÌí£¬ÐÔÄÜ·½ÃæµÄÎÊÌâÒ²Óú·¢Ã÷ÏÔ¡£ÎªÏàʶ¾öÕâЩÎÊÌ⣬¿ªÔ´ÉçÇøÍƳöÁËÒ»¸ö»ùÓÚphpÓïÑԵĸßÐÔÄܵÄrpcЧÀÍÆ÷¡ª¡ªswoole¡£

SwooleÊÇÒ»¸ö»ùÓÚPHPÓïÑÔ¿ª·¢µÄÒì²½¡¢²¢ÐС¢¸ßÐÔÄܵÄÍøÂçͨѶ¿ò¼Ü£¬Ê¹µÃPHP³ÌÐò¿ÉÒÔÔ½·¢¸ßЧµØ´¦ÀíÍøÂçÇëÇó¡£RPCЧÀÍÆ÷ÊÇSwooleµÄÒ»¸ö×é¼þ£¬ËüÌṩÁËÒ»ÖÖ»ùÓÚTCPЭÒéµÄÔ¶³ÌÀú³ÌŲÓÃÒªÁ죬֧³ÖÒì²½I/O¡¢Ð­³Ì¡¢Àú³ÌÖÎÀíµÈ¶àÖÖÌØÕ÷£¬¿ÉÒÔÇáËÉʵÏÖ¸ßÐÔÄÜ¡¢¸ß²¢·¢µÄRPCЧÀÍ¡£

½ÓÏÂÀ´£¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃSwooleʵÏÖ¸ßÐÔÄܵÄRPCЧÀÍÆ÷¡£

×°ÖÃSwooleÀ©Õ¹

ÔÚ×îÏÈ֮ǰ£¬ÎÒÃÇÐèÒªÊ×ÏÈ×°ÖÃSwooleÀ©Õ¹¡£ÓÉÓÚSwooleÒÀÀµÓÚPHPµÄµ×²ãCÀ©Õ¹£¬Òò´ËÐèÒªÏÈ×°ÖÃC±àÒëÆ÷£¬ÒÔ¼°SwooleµÄÒÀÀµ¿â¡£

yum install -y gcc 
    automake 
    autoconf 
    libtool 
    make 
    php-devel 
    php-pear 
    pcre-devel 
    openssl-devel

µÇ¼ºó¸´ÖÆ

×°ÖÃÍêÒÀÀµ¿âºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃpeclÏÂÁîÀ´×°ÖÃSwooleÀ©Õ¹£º

pecl install swoole

µÇ¼ºó¸´ÖÆ

×°ÖÃÍê³Éºó£¬ÎÒÃÇÐèÒªÔÚphp.iniÎļþÖÐÌí¼ÓÒÔÏÂÐÐÒÔ¿ªÆôSwooleÀ©Õ¹£º

extension=swoole.so

µÇ¼ºó¸´ÖÆ

ʵÏÖRPCЧÀÍÆ÷

ÔÚ×°ÖÃÍêSwooleÀ©Õ¹ºó£¬ÎÒÃÇ¿ÉÒÔ×îÏÈʵÏÖRPCЧÀÍÆ÷¡£ÕâÀïÎÒÃÇ»áʹÓÃPHPµÄ·´Éä»úÖÆÀ´ÊµÏÖ×Ô¶¯»¯µÄЧÀÍ×¢²á£¬ÒÔ¼°SwooleµÄЭ³ÌÀ´´¦ÀíÒì²½I/O¡£

½¨ÉèЧÀÍÀà

Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öЧÀÍÀ࣬ÓÃÓÚ̻¶¹©Ô¶³ÌŲÓõÄÒªÁì¡£ÔÚÕâ¸öÀàÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵¶à¸öÒªÁ죬²¢Ê¹ÓÃPHPµÄDocBlockÀ´±ê×¢ÒªÁìµÄ²ÎÊýºÍ·µ»ØÖµÀàÐÍ£¬ÒÔ±ãÓÚ×Ô¶¯ÌìÉúÎĵµºÍ´úÂëÌáÐÑ¡£

/**
 * @method string hello(string $name)
 */
class MyService
{
    public function hello(string $name): string
    {
        return "Hello, $name!";
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öMyServiceÀ࣬ÆäÖаüÀ¨Ò»¸öÃûΪhelloµÄÒªÁ죬ËüÎüÊÕÒ»¸ö×Ö·û´®ÀàÐ͵IJÎÊý$name£¬·µ»ØÒ»¸ö×Ö·û´®ÀàÐ͵ÄÊý¾Ý¡£

½¨ÉèRPCЧÀÍÆ÷

½ÓÏÂÀ´£¬ÎÒÃÇÐèҪʵÏÖRPCЧÀÍÆ÷À´ÎüÊÕ¿Í»§¶ËµÄÇëÇ󣬲¢Å²ÓÃЧÀÍÀàÖжÔÓ¦µÄÒªÁìÀ´´¦ÀíÇëÇó¡£

$server = new SwooleServer('127.0.0.1', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

/**
 * ×¢²áЧÀÍ
 */
$server->set([
    'worker_num' => 1,
    'dispatch_mode' => 1,
]);
$myService = new MyService();
$methods = get_class_methods($myService);
$availableMethods = [];
foreach ($methods as $method) {
    // ºöÂÔ __* ÀàÐ͵ÄÒªÁ죬˽ÓÐÒªÁìºÍ½á¹¹ÒªÁì
    if (!preg_match('/^__|^get[A-Z]/i', $method) && is_callable([$myService, $method])) {
        $availableMethods[] = $method;
    }
}
$server->on('WorkerStart', function () use ($availableMethods, $myService) {
    // ·­¿ªÐ­³ÌÖ§³Ö
    SwooleRuntime::enableCoroutine();
    $service = new HproseSwooleSocketService();
    foreach ($availableMethods as $method) {
        $service->addFunction([$myService, $method], $method);
    }
    $server = new HproseSwooleSocketServer('tcp://0.0.0.0:9501');

    //¼àÌý RPC ÇëÇó
    $coroutine = new SwooleCoroutineHttpClient();
    $coroutine->setHeaders([
        'Content-Type' => 'text/plain',
    ]);

    while (true) {
        $socket = $server->accept();
        if ($socket !== false) {
            $socket->setOption(['open_length_check' => 1]);
            $socket->setOption(['package_length_type' => 'N']);
            $socket->setOption(['package_length_offset' => 0]);
            $socket->setOption(['package_body_offset' => 4]);
            $socket->start();
            $client = new SwooleCoroutineClient(SWOOLE_SOCK_TCP);
            $client->connect('127.0.0.1', 9502);
            $client->send($socket->recv());
            $out = $client->recv();
            $socket->send($out);
            $socket->close();
        }
    }
});
$server->start();

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸ö$server¹¤¾ß£¬Ëü¼àÌý127.0.0.1:9501µØµãºÍ¶Ë¿Ú£¬Ê¹ÓÃSWOOLE_PROCESSÀú³ÌģʽºÍSWOOLE_SOCK_TCPЭÒé¡£

ÔÚЧÀÍÆ÷Æô¶¯ºó£¬ÎÒÃÇʹÓÃPHPµÄ·´Éä»úÖÆÀ´»ñȡЧÀÍÀàÖÐËùÓпɹ©Å²ÓõÄÒªÁ졣Ȼºó£¬ÎÒÃÇʹÓÃSwooleµÄЭ³ÌÀ´¼àÌýRPCÇëÇ󣬲¢Í¨¹ýŲÓÃЧÀÍÀàµÄÒªÁìÀ´´¦ÀíÇëÇó¡£ÔÚʵÏÖÀú³ÌÖУ¬ÎÒÃÇʹÓÃÁ˵ÚÈý·½¿âHprose£¬ËüÌṩÁËÒ»ÖÖ¾«Á·Ã÷ÎúµÄRPCЧÀÍʵÏÖ·½·¨£¬Ê¹ÓÃÆðÀ´ºÜÊÇÀû±ã¡£

½¨Éè¿Í»§¶Ë

×îºó£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö¿Í»§¶ËÀ´ÇëÇóRPCЧÀÍ¡£ÔÚ±¾ÀýÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃHprose×Ô´øµÄClientÀàÀ´ÊµÏÖÕâÒ»µã¡£

$client = new HproseHttpClient('http://127.0.0.1:9501/', false);
echo $client->hello('Swoole');

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öHproseµÄHTTP¿Í»§¶Ë¹¤¾ß£¬²¢Å²ÓÃЧÀÍÀàÖеÄhelloÒªÁìÀ´ÏòRPCЧÀÍÆ÷ÌᳫÇëÇó¡£

×ܽá

SwooleÊÇÒ»¸öÇ¿Ê¢µÄÍøÂçͨѶ¿ò¼Ü£¬ÌṩÁËÐí¶àÒì²½¡¢²¢ÐС¢¸ßÐÔÄܵÄÌØÕ÷£¬¿ÉÒÔ´ó´óÌá¸ßPHP³ÌÐòµÄ´¦ÀíÄÜÁ¦¡£Í¨¹ýѧϰ±¾ÎÄÖеÄÄÚÈÝ£¬ÎÒÃÇ¿ÉÒÔʵÏÖÒ»¸ö¸ßÐÔÄÜ¡¢¸ß²¢·¢µÄRPCЧÀÍÆ÷£¬ÌáÉýPHP³ÌÐòµÄ´¦ÀíºÍÔËÐÐЧÂÊ¡£

ÒÔÉϾÍÊÇSwooleʵÏÖ¸ßÐÔÄܵÄRPCЧÀÍÆ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ