RPC全稱爲Remote Procedure Call,翻譯過來爲「遠程過程調用」。目前,主流的平臺中都支持各類遠程調用技術,以知足分佈式系統架構中不一樣的系統之間的遠程通訊和相互調用。遠程調用的應用場景極其普遍,實現的方式也各式各樣。php
基於HTTP協議的(例如基於文本的SOAP(XML)、Rest(JSON),基於二進制Hessian(Binary))
基於TCP協議的(一般會藉助Mina、Netty等高性能網絡框架)數據庫
單種語言或平臺特定支持的通訊技術(例如Java平臺的RMI、.NET平臺Remoting)
支持跨平臺通訊的技術(例如HTTP Rest、Thrift等)編程
同步通訊調用(同步RPC)
異步通訊調用(MQ、異步RPC)json
遠程數據共享(例如:共享遠程文件,共享數據庫等實現不一樣系統通訊)
消息隊列
RPC(遠程過程調用)api
目錄結構 安全
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; } }
效果
性能:影響RPC性能的主要在幾個方面:
1.序列化/反序列化的框架
2.網絡協議,網絡模型,線程模型等
安全
RPC安全的主要在於服務接口的鑑權和訪問控制支持。
跨平臺
跨不一樣的操做系統,不一樣的編程語言和平臺。