RPC採用客戶機/服務器模式。 請求程序就是一個客戶機,而服務提供程序就是一個服務器。首先,客戶機調用進程發送一個有進程參數的調用信息到服務進程,而後等待應答信息。在服務器端, 進程保持睡眠狀態直到調用信息的到達爲止。當一個調用信息到達,服務器得到進程參數,計算結果,發送答覆信息,而後等待下一個調用信息,最後,客戶端調用進程接收答覆信息,得到進程結果,而後調用執行繼續進行。php
php中RPC簡單框架感受是YAR,嘗試本身寫一個案例,提早須要安裝yar框架,具體安裝方法上網搜搜:html
服務器端:地址:http://html.com/yar/Operator.php 代碼:服務器
class Operator { /** * Add two operands * @param interge * @return interge */ public function add($a, $b) { return $this->_add($a, $b); } /** * Sub */ public function sub($a, $b) { return $a - $b; } /** * Mul */ public function mul($a, $b) { return $a * $b; } /** * Protected methods will not be exposed * @param interge * @return interge */ protected function _add($a, $b) { return $a + $b; } } $server = new Yar_Server(new Operator()); $server->handle();
客戶的地址:http://html.com/yar/yar.php框架
代碼:this
$url="http://html.com/yar/Operator.php"; $client = new yar_client($url); var_dump($client->add(1, 2)); var_dump($client->call("add", array(3, 2)));
打印結果爲:url
int(3)
int(5)