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ЧÀÍÆ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡