使用PHP來簡單的建立一個RPC服務
阿新 • • 發佈:2018-12-22
RPC全稱為Remote Procedure Call,翻譯過來為"遠端過程呼叫"。主要應用於不同的系統之間的遠端通訊和相互呼叫。
比如有兩個系統,一個是PHP寫的,一個是JAVA寫的,而PHP想要呼叫JAVA中的某個類的某個方法,這時候就需要用到RPC了。
怎麼調?直接調是不可能,只能是PHP通過某種自定義協議請求JAVA的服務,JAVA解析該協議,在本地例項化類並呼叫方法,然後把結果返回給PHP。
這裡我們用PHP的socket擴充套件來建立一個服務端和客戶端,演示呼叫過程。
目錄結構
RpcServer.php程式碼如下:
<?php class RpcServer { protected $serv = null; public function __construct($host, $port, $path) { //建立一個tcp socket服務 $errstr = '---'; $this->serv = stream_socket_server("tcp://{$host}:{$port}", $errno, $errstr); if (!$this->serv) { exit("{$errno} : {$errstr} \n"); } //判斷我們的RPC服務目錄是否存在 $realPath = realpath($path); echo $realPath." \n"; if ($realPath === false || !file_exists($realPath)) { exit("{$path} error \n"); } while (true) { $client = stream_socket_accept($this->serv); echo $client." \n"; if ($client) { //這裡為了簡單,我們一次性讀取 $buf = fread($client, 2048); echo $buf." \n"; //解析客戶端傳送過來的協議 $classRet = preg_match('/Rpc-Class:\s(.*);/i', $buf, $class); $methodRet = preg_match('/Rpc-Method:\s(.*);/i', $buf, $method); $paramsRet = preg_match('/Rpc-Params:\s(.*);/i', $buf, $params); if($classRet && $methodRet) { $class = ucfirst($class[1]); $file = $realPath . '/' . $class . '.php'; //判斷檔案是否存在,如果有,則引入檔案 if(file_exists($file)) { require_once $file; //例項化類,並呼叫客戶端指定的方法 $obj = new $class(); //如果有引數,則傳入指定引數 if(!$paramsRet) { $data = $obj->$method[1](); } else { $data = $obj->$method[1](json_decode($params[1], true)); } //把執行後的結果返回給客戶端 fwrite($client, $data); } } else { fwrite($client, "class or method error"); } //關閉客戶端 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) { //建立一個客戶端 $client = stream_socket_client("tcp://{$this->urlInfo['host']}:{$this->urlInfo['port']}", $errno, $errstr); if (!$client) { exit("{$errno} : {$errstr} \n"); } //傳遞呼叫的類名 $class = basename($this->urlInfo['path']); $proto = "Rpc-Class: {$class};" . PHP_EOL; //傳遞呼叫的方法名 $proto .= "Rpc-Method: {$method};" . PHP_EOL; //傳遞方法的引數 $params = json_encode($params); $proto .= "Rpc-Params: {$params};" . PHP_EOL; //向服務端傳送我們自定義的協議資料 fwrite($client, $proto); //讀取服務端傳來的資料 $data = fread($client, 2048); //關閉客戶端 fclose($client); return $data; } } $cli = new RpcClient('http://127.0.0.1:8888/Test'); echo $cli->hehe()." \n"; echo $cli->hehe2(array('name' => 'Test', 'age' => 27))." \n";
Test.php程式碼如下:
<?php
class Test {
public function hehe() {
return 'hehe';
}
public function hehe2($params) {
return json_encode($params);
}
}
然後分別執行兩個指令碼(注意,php要新增環境變數)
php RpcServer.php
php RpcClient.php
結果如下