RPC全稱為Remote Procedure Call,翻譯過來為"遠(yuǎn)程過程調(diào)用"丁存。主要應(yīng)用于不同的系統(tǒng)之間的遠(yuǎn)程通信和相互調(diào)用肩杈。
比如有兩個(gè)系統(tǒng),一個(gè)是PHP寫的解寝,一個(gè)是JAVA寫的扩然,而PHP想要調(diào)用JAVA中的某個(gè)類的某個(gè)方法,這時(shí)候就需要用到RPC了编丘。
怎么調(diào)与学?直接調(diào)是不可能,只能是PHP通過某種自定義協(xié)議請求JAVA的服務(wù)嘉抓,JAVA解析該協(xié)議索守,在本地實(shí)例化類并調(diào)用方法,然后把結(jié)果返回給PHP抑片。
這里我們用PHP的socket擴(kuò)展來創(chuàng)建一個(gè)服務(wù)端和客戶端卵佛,演示調(diào)用過程。
目錄結(jié)構(gòu)
RpcServer.php代碼如下:
<?php
class RpcServer {
protected $serv = null;
public function __construct($host, $port, $path) {
//創(chuàng)建一個(gè)tcp socket服務(wù)
$errstr = '---';
$this->serv = stream_socket_server("tcp://{$host}:{$port}", $errno, $errstr);
if (!$this->serv) {
exit("{$errno} : {$errstr} \n");
}
//判斷我們的RPC服務(wù)目錄是否存在
$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";
//解析客戶端發(fā)送過來的協(xié)議
$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;
//實(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)建一個(gè)客戶端
$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()." \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);
}
}
然后分別運(yùn)行兩個(gè)腳本(注意,php要添加環(huán)境變量)
php RpcServer.php
php RpcClient.php
結(jié)果如下