1. 程式人生 > >PHP實現遠端過程呼叫RPC

PHP實現遠端過程呼叫RPC

一、初識RPC

RPC(Remote Procedure Call)—遠端過程呼叫,它是一種通過網路從遠端計算機程式上請求服務,而不需要了解底層網路技術的協議。

二、工作原理

執行時,一次客戶機對伺服器的RPC呼叫,其內部操作大致有如下十步:

  • 1.呼叫客戶端控制代碼;執行傳送引數
  • 2.呼叫本地系統核心傳送網路訊息
  • 3.訊息傳送到遠端主機
  • 4.伺服器控制代碼得到訊息並取得引數
  • 5.執行遠端過程
  • 6.執行的過程將結果返回伺服器控制代碼
  • 7.伺服器控制代碼返回結果,呼叫遠端系統核心
  • 8.訊息傳回本地主機
  • 9.客戶控制代碼由核心接收訊息
  • 10.客戶接收控制代碼返回的資料
    (以上資訊來自百度百科)

三、在PHP中的實現

  • 檔案結構
[email protected]:~/www/zhangrenjie_test/test/rpc$ tree
├── Client.php
└── Provider
    ├── index.php
    └── Services
        └── UserService.php
  • 客戶端Client.php
class Client
{
    private $serviceUrl;
    private $serviceName;
    private $rpcConfig = [
        'UserService' => 'http://127.0.0.1:8081',
    ];

    public function __construct($serviceName)
    {
        if (array_key_exists($serviceName, $this->rpcConfig)) {
            $this->serviceUrl = $this->rpcConfig[$serviceName];
            $this->serviceName = $serviceName;
        }
    }

    // __call() is triggered when invoking inaccessible methods in an object context.
    //呼叫不可訪問的方法
    public function __call($actionName, $arguments)
    {
        $content = json_encode($arguments);
        $options['http'] = [
            'timeout' => 5,
            'method' => 'POST',
            'header' => 'Content-type:applicaion/x-www-form-urlencoode',
            'content' => $content,
        ];
        //建立資源流上下文
        $context = stream_context_create($options);

        $get = [
            'service_name' => $this->serviceName,
            'action_name' => $actionName
        ];

        $serviceUrl = $this->serviceUrl . '?' . http_build_query($get);
        $result = file_get_contents($serviceUrl, false, $context);
        return json_decode($result, true);
    }
}

$userService = new Client('UserService');
$result=$userService->getUserInfo(random_int(1,10));
var_dump($result);

步驟:

  • 1.註冊服務$rpcConfig。
  • 2.例項化客戶端時,指定服務名稱。
  • 3.訪問物件不可訪問的方法getUserInfo,自動呼叫__call魔術方法。
  • 4.在__call魔術方法中想指定的遠端服務傳送請求(http://127.0.0.1:8081
  • 服務提供者(Service Provider)的實現

Provider目錄為演示的服務提供者的總目錄,index.php為管理排程服務的入口。
Provider\Services目錄為所有服務類目錄。

服務排程入口:index.php

namespace rpc\Provider;

require_once './Services/UserService.php';

use rpc\Provider\Services\{
    UserService
};

$serviceName = trim($_GET['service_name']);
$serviceAction = trim($_GET['action_name']);
$argv = file_get_contents("php://input");
if (empty($serviceName) || empty($serviceAction)) die('paramas is missing');
if (!empty($argv)) {
    $argv = json_decode($argv, true);
}

$result = call_user_func_array([$serviceName, $serviceAction], $argv);
echo  json_encode($result);

User服務:UserService.php


namespace rpc\Provider\Services;

class UserService
{
    public static function getUserInfo(int $uid): array
    {
        return [
            'id' => $uid,
            'user_name' => 'jack_' . $uid,
        ];
    }
}

開啟服務:對外提供服務遠端呼叫地址(http://127.0.0.1:8081)。

#這裡我們利用PHP自帶的cli模式開啟服務
php -S 127.0.0.1:8080 -t /www/zhangrenjie_test/test/rpc/Provider

至此,大功告成。

瞭解php開啟服務,請參照沒有第三方web服務,怎麼執行php?