国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

php rpc怎么實現?

2020-03-22 18:29:15
字體:
來源:轉載
供稿:網友

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

目錄結構

40d784037eb297e737a45555afce217.png

rpc服務端

<?php/** * User: yuzhao * CreateTime: 2018/11/15 下午11:46 * Description: Rpc服務端 */html' target='_blank'>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;    }}

效果

8758965292a1186e13410a9add29bd1.png

7.RPC的注意事項

性能:影響RPC性能的主要在幾個方面:
1.序列化/反序列化的框架
2.網絡協議,網絡模型,線程模型等

安全
RPC安全的主要在于服務接口的鑒權和訪問控制支持。

跨平臺
跨不同的操作系統,不同的編程語言和平臺。

以上就是php rpc怎么實現?的詳細內容,更多請關注 其它相關文章!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福建省| 建昌县| 双柏县| 封开县| 高州市| 泰和县| 大邑县| 穆棱市| 泽库县| 容城县| 溧阳市| 金湖县| 林甸县| 广安市| 凤冈县| 莱西市| 泗水县| 枣庄市| 深水埗区| 行唐县| 疏附县| 福贡县| 池州市| 安岳县| 河东区| 商河县| 三台县| 黔东| 湘潭市| 宁河县| 黄大仙区| 老河口市| 高清| 民丰县| 沁水县| 西畴县| 剑阁县| 德令哈市| 永安市| 新邵县| 宜兰市|