php rpcÔõôʵÏÖ£¿
1.ʲôÊÇrpc
RPCÈ«³ÆΪRemote Procedure Call£¬·Òë¹ýÀ´Îª¡°³¤Í¾Àú³Ìµ÷È¡¡±¡£µ±Ç°£¬Ö÷Á÷µÄƽ̨Öж¼Ö§³Å¸÷ÖÖ³¤Í¾µ÷È¡¼¼Êõ£¬ÒÔÖª×ã·Ö²¼Ê½ÏµÍ³¼Ü¹¹Öв»Ò»ÑùµÄϵͳ֮¼äµÄ³¤Í¾Í¨Ñ¶ºÍ±Ë´Ëµ÷È¡¡£³¤Í¾µ÷È¡µÄÀûÓó¡¾°¼«Æä¹ã·º£¬ÊµÏֵķ½Ê½Ò²ÁÖÁÖ×Ü×Ü¡£
2.´ÓͨѶºÍ̸µÄ²ãÃæ
»ùÓÚHTTPºÍ̸µÄ£¨ÀýÈç»ùÓÚÎı¾µÄSOAP£¨XML£©¡¢Rest£¨JSON£©£¬»ùÓÚ¶þ½øÖÆHessian£¨Binary£©£©
»ùÓÚTCPºÍ̸µÄ£¨Æ½³£»á½èÖúMina¡¢NettyµÈ¸ß»úÄÜÍøÂç¿ò¼Ü£©
3.´Ó²»Ò»ÑùµÄ¿ª·¢Ëµ»°ºÍƽ̨²ãÃæ
µ¥ÖÖ˵»°»òƽ̨Ìض¨Ö§³ÅµÄͨѶ¼¼Êõ(ÀýÈçJavaƽ̨µÄRMI¡¢.NETƽ̨Remoting)
Ö§³Å¿çƽ̨ͨѶµÄ¼¼Êõ£¨ÀýÈçHTTP Rest¡¢ThriftµÈ£©
4.´Óµ÷È¡Àú³ÌÀ´¿´
ͬ²½Í¨Ñ¶µ÷È¡£¨Í¬²½RPC£©
Ò첽ͨѶµ÷È¡£¨MQ¡¢Òì²½RPC£©
5.³£¼ûµÄ¼¸ÖÖͨѶ·½Ê½
³¤Í¾Êý¾ÝͬÏí£¨ÀýÈ磺ͬÏí³¤Í¾Îļþ£¬Í¬ÏíÊý¾Ý¿âµÈʵÏÖ²»Ò»ÑùϵͳͨѶ£©
ÐÂÎŶÓÁÐ
RPC£¨³¤Í¾Àú³Ìµ÷È¡£©
6.phpʵÏÖ¼ò±ãµÄrpc
ÃûÄ¿¹¹Ôì
rpcЧÀͶË
<?php/** * User: yuzhao * CreateTime: 2018/11/15 ÏÂÎç11:46 * Description: RpcЧÀÍ¶Ë */class RpcServer { /** * User: yuzhao * CreateTime: 2018/11/15 ÏÂÎç11:51 * @var array * Description: ´ËÀàµÄ¸ù±¾ÅäÖà */ private $params = [ 'host' => '', // ipµØÖ·£¬ÁгöÀ´µÄÄ¿µÄÊÇΪÁËÓѺÿ´³öÀ´´Ë±äÁ¿Öд¢±¸µÄÐÅÏ¢ 'port' => '', // ¶Ë¿Ú 'path' => '' // ЧÀÍÃûÄ¿ ]; /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:14 * @var array * Description: ±¾Àà³£ÓÃÅäÖà */ private $config = [ 'real_path' => '', 'max_size' => 2048 // ×î´ó½ÓÊÕÊý¾Ý´óС ]; /** * User: yuzhao * CreateTime: 2018/11/15 ÏÂÎç11:50 * @var nul * Description: */ private $server = null; /** * Rpc constructor. */ public function __construct($params) { $this->check(); $this->init($params); } /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:0 * Description: ±ØÒªÑéÖ¤ */ private function check() { $this->serverPath(); } /** * User: yuzhao * CreateTime: 2018/11/15 ÏÂÎç11:48 * Description: ³õʼ»¯±ØÒª²ÎÊý */ private function init($params) { // ½«´«µÝ¹ýÀ´µÄ²ÎÊý³õʼ»¯ $this->params = $params; // ´´Á¢tcpsocketЧÀÍ $this->createServer(); } /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:0 * Description: ´´Á¢tcpsocketЧÀÍ */ private function createServer() { $this->server = stream_socket_server("tcp://{$this->params['host']}:{$this->params['port']}", $errno,$errstr); if (!$this->server) exit([ $errno,$errstr ]); } /** * User: yuzhao * CreateTime: 2018/11/15 ÏÂÎç11:57 * Description: rpcЧÀÍÃûÄ¿ */ public function serverPath() { $path = $this->params['path']; $realPath = realpath(__DIR__ . $path); if ($realPath === false ||!file_exists($realPath)) { exit("{$path} error!"); } $this->config['real_path'] = $realPath; } /** * User: yuzhao * CreateTime: 2018/11/15 ÏÂÎç11:51 * Description: ·µ»Øµ±Ç°¶ÔÏó */ public static function instance($params) { return new RpcServer($params); } /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:06 * Description: ÔËת */ public function run() { while (true) { $client = stream_socket_accept($this->server); if ($client) { echo "ÓÐÐÂÁ¬½Ó\n"; $buf = fread($client, $this->config['max_size']); print_r('½ÓÊÕµ½µÄÔʼÊý¾Ý:'.$buf."\n"); // ×Ô¶¨ÒåºÍ̸ĿµÄÊÇÄõ½Àà°ì·¨ºÍ²ÎÊý(¿É¸Ä³É±¾È˶¨ÒåµÄ) $this->parseProtocol($buf,$class, $method,$params); // Ê©Ðа취 $this->execMethod($client, $class, $method, $params); //¹Ø±Õ¿Í»§¶Ë fclose($client); echo "¹Ø±ÕÁËÁ¬½Ó\n"; } } } /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:19 * @param $class * @param $method * @param $params * Description: Ê©Ðа취 */ private function execMethod($client, $class, $method, $params) { if($class && $method) { // Ê××ÖĸתΪ´óд $class = ucfirst($class); $file = $this->params['path'] . '/' . $class . '.php'; //ÍƶÏÎļþ¿É·ñ´æÔÚ£¬¼ÙÈçÓУ¬ÔòÒýÈëÎļþ if(file_exists($file)) { require_once $file; //ʵÀý»¯À࣬²¢µ÷È¡¿Í»§¶ËÖ¸¶¨µÄ°ì·¨ $obj = new $class(); //¼ÙÈçÓвÎÊý£¬Ôò´«ÈëÖ¸¶¨²ÎÊý if(!$params) { $data = $obj->$method(); } else { $data = $obj->$method($params); } // ´ò°üÊý¾Ý $this->packProtocol($data); //°ÑÔËתºóµÄ½á¹û·µ»Ø¸ø¿Í»§¶Ë fwrite($client, $data); } } else { fwrite($client, 'class or method error'); } } /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:10 * Description: ½âÎöºÍ̸ */ private function parseProtocol($buf, &$class, &$method, &$params) { $buf = json_decode($buf, true); $class = $buf['class']; $method = $buf['method']; $params = $buf['params']; } /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:30 * @param $data * Description: ´ò°üºÍ̸ */ private function packProtocol(&$data) { $data = json_encode($data, JSON_UNESCAPED_UNICODE); } } RpcServer::instance([ 'host' => '127.0.0.1', 'port' => 8888, 'path' => './api'])->run();
rpc ¿Í»§¶Ë
<?php/** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:2 * Description: Rpc¿Í»§¶Ë */class RpcClient { /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:21 * @var array * Description: µ÷È¡µÄµØÖ· */ private $urlInfo = array(); /** * RpcClient constructor. */ public function __construct($url) { $this->urlInfo = parse_url($url); } /** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:2 * Description: ·µ»Øµ±Ç°¶ÔÏó */ public static function instance($url) { return new RpcClient($url); } public function __call($name, $arguments) { // TODO: Implement __call() method. //´´Á¢Ò»¸ö¿Í»§¶Ë $client = stream_socket_client("tcp://{$this->urlInfo['host']}:{$this->urlInfo['port']}", $errno, $errstr); if (!$client) { exit("{$errno} : {$errstr} \n"); } $data = [ 'class' => basename($this->urlInfo['path']), 'method' => $name, 'params' => $arguments ]; //ÏòЧÀͶ˷¢ËÍÎÒÃÇ×Ô¶¨ÒåµÄºÍ̸Êý¾Ý fwrite($client, json_encode($data)); //¶ÁȡЧÀͶ˴«À´µÄÊý¾Ý $data = fread($client, 2048); //¹Ø±Õ¿Í»§¶Ë fclose($client); return $data; } } $cli = new RpcClient('http://127.0.0.1:8888/test');echo $cli->tuzisir1()."\n";echo $cli->tuzisir2(array('name' => 'tuzisir', 'age' => 23));
¹©¸øЧÀ͵ÄÎļþ
<?php/** * User: yuzhao * CreateTime: 2018/11/16 ÉÏÎç12:28 * Description: */class Test { public function tuzisir1() { return 'ÎÒÊÇÎ޲ΰ취'; } public function tuzisir2($params) { return $params; } }
½á¹û
7.RPCµÄÁôÒâÊÂÏî
»úÄÜ:Ó°ÏìRPC»úÄܵÄÖ÷ÒªÔÚ¼¸¸ö·½Ã棺
1.ÐòÁл¯/·´ÐòÁл¯µÄ¿ò¼Ü
2.ÍøÂçºÍ̸£¬ÍøÂçÄ£ÐÍ£¬Ïß³ÌÄ£Ð͵È
ƽ°²
RPCƽ°²µÄÖ÷ÒªÔÚÓÚЧÀͽӿڵļøȨºÍ°Ý·Ã²Ù×ÝÖ§³Å¡£
¿çƽ̨
¿ç²»Ò»ÑùµÄ²Ù×Ýϵͳ£¬²»Ò»ÑùµÄ±à³Ì˵»°ºÍƽ̨¡£
ÒÔÉϾÍÊÇphp rpcÈçºÎʵÏÖ£¿µÄ¾ßÌåÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢°Ù·Ö°ÙÔ´ÂëÍøÆäËüÏà¹ØÎÄÕ£¡
¸ÐлÄúµÄÖ§³Ö£¬ÎÒ»á¼ÌÐøŬÁ¦µÄ!
´ò¿ªÖ§¸¶±¦É¨Ò»É¨£¬¼´¿É½øÐÐɨÂë´òÉÍŶ
°Ù·Ö°ÙÔ´ÂëÍø ½¨Òé´òÉÍ1¡«10Ôª£¬ÍÁºÀËæÒ⣬¸ÐлÄúµÄÔĶÁ£¡