一、初识RPC

RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。

二、工作原理

运行时,一次客户机对服务器的RPC调用,其内部操作大致有如下十步:

  • 1.调用客户端句柄;执行传送参数
  • 2.调用本地系统内核发送网络消息
  • 3.消息传送到远程主机
  • 4.服务器句柄得到消息并取得参数
  • 5.执行远程过程
  • 6.执行的过程将结果返回服务器句柄
  • 7.服务器句柄返回结果,调用远程系统内核
  • 8.消息传回本地主机
  • 9.客户句柄由内核接收消息
  • 10.客户接收句柄返回的数据 (以上信息来自百度百科)

三、在PHP中的实现

  • 文件结构
zrj@zrj:~/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?