本文實(shí)例講述了PHP實(shí)現(xiàn)創(chuàng)建一個RPC服務(wù)操作。分享給大家供大家參考,具體如下:
RPC全稱為Remote Procedure Call,翻譯過來為"遠(yuǎn)程過程調(diào)用"。主要應(yīng)用于不同的系統(tǒng)之間的遠(yuǎn)程通信和相互調(diào)用。
比如有兩個系統(tǒng),一個是PHP寫的,一個是JAVA寫的,而PHP想要調(diào)用JAVA中的某個類的某個方法,這時候就需要用到RPC了。
怎么調(diào)?直接調(diào)是不可能,只能是PHP通過某種自定義協(xié)議請求JAVA的服務(wù),JAVA解析該協(xié)議,在本地實(shí)例化類并調(diào)用方法,然后把結(jié)果返回給PHP。
這里我們用PHP的socket擴(kuò)展來創(chuàng)建一個服務(wù)端和客戶端,演示調(diào)用過程。
RpcServer.php代碼如下:
?php class RpcServer { protected $serv = null; public function __construct($host, $port, $path) { //創(chuàng)建一個tcp socket服務(wù) $this->serv = stream_socket_server("tcp://{$host}:{$port}", $errno, $errstr); if (!$this->serv) { exit("{$errno} : {$errstr} \n"); } //判斷我們的RPC服務(wù)目錄是否存在 $realPath = realpath(__DIR__ . $path); if ($realPath === false || !file_exists($realPath)) { exit("{$path} error \n"); } while (true) { $client = stream_socket_accept($this->serv); if ($client) { //這里為了簡單,我們一次性讀取 $buf = fread($client, 2048); //解析客戶端發(fā)送過來的協(xié)議 $classRet = preg_match('/Rpc-Class:\s(.*);\r\n/i', $buf, $class); $methodRet = preg_match('/Rpc-Method:\s(.*);\r\n/i', $buf, $method); $paramsRet = preg_match('/Rpc-Params:\s(.*);\r\n/i', $buf, $params); if($classRet $methodRet) { $class = ucfirst($class[1]); $file = $realPath . '/' . $class . '.php'; //判斷文件是否存在,如果有,則引入文件 if(file_exists($file)) { require_once $file; //實(shí)例化類,并調(diào)用客戶端指定的方法 $obj = new $class(); //如果有參數(shù),則傳入指定參數(shù) if(!$paramsRet) { $data = $obj->$method[1](); } else { $data = $obj->$method[1](json_decode($params[1], true)); } //把運(yùn)行后的結(jié)果返回給客戶端 fwrite($client, $data); } } else { fwrite($client, 'class or method error'); } //關(guān)閉客戶端 fclose($client); } } } public function __destruct() { fclose($this->serv); } } new RpcServer('127.0.0.1', 8888, './service');
RpcClient.php代碼如下:
?php class RpcClient { protected $urlInfo = array(); public function __construct($url) { //解析URL $this->urlInfo = parse_url($url); if(!$this->urlInfo) { exit("{$url} error \n"); } } public function __call($method, $params) { //創(chuàng)建一個客戶端 $client = stream_socket_client("tcp://{$this->urlInfo['host']}:{$this->urlInfo['port']}", $errno, $errstr); if (!$client) { exit("{$errno} : {$errstr} \n"); } //傳遞調(diào)用的類名 $class = basename($this->urlInfo['path']); $proto = "Rpc-Class: {$class};" . PHP_EOL; //傳遞調(diào)用的方法名 $proto .= "Rpc-Method: {$method};" . PHP_EOL; //傳遞方法的參數(shù) $params = json_encode($params); $proto .= "Rpc-Params: {$params};" . PHP_EOL; //向服務(wù)端發(fā)送我們自定義的協(xié)議數(shù)據(jù) fwrite($client, $proto); //讀取服務(wù)端傳來的數(shù)據(jù) $data = fread($client, 2048); //關(guān)閉客戶端 fclose($client); return $data; } } $cli = new RpcClient('http://127.0.0.1:8888/test'); echo $cli->hehe(); echo $cli->hehe2(array('name' => 'test', 'age' => 27));
然后分別運(yùn)行上面兩個腳本(注意,php要添加環(huán)境變量)
> php RpcServer.php > php RpcClient.php
結(jié)果如下:
Test.php代碼如下:
?php class Test { public function hehe() { return 'hehe'; } public function hehe2($params) { return json_encode($params); } }
目錄結(jié)構(gòu)如下:
上面我們自定義的協(xié)議,可以隨意修改,只要是客戶端和服務(wù)端兩邊能夠統(tǒng)一并能解析。
客戶端通過請求服務(wù)端,把要調(diào)用的類,方法和參數(shù)傳遞給服務(wù)端,服務(wù)端去通過實(shí)例化調(diào)用方法返回結(jié)果。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php socket用法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》及《PHP網(wǎng)絡(luò)編程技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
標(biāo)簽:婁底 鎮(zhèn)江 寶雞 宜賓 湛江 銅川 黃南 南陽
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)現(xiàn)創(chuàng)建一個RPC服務(wù)操作示例》,本文關(guān)鍵詞 PHP,實(shí)現(xiàn),創(chuàng)建,一個,RPC,服務(wù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。