尊龙凯时人生就是博

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
sitemap¡¢ÍøÕ¾µØͼ