php调用rpc,PHP实现远程过程调用RPC

本文详细介绍了RPC(远程过程调用)的概念及其工作原理,并通过一个具体的PHP实现案例展示了如何在客户端和服务端进行交互。客户端通过魔术方法__call发送HTTP请求到服务提供者,服务提供者接收到请求后执行对应服务方法并返回结果。示例中,用户服务类UserService实现了获取用户信息的方法,客户端通过RPC调用此方法获取随机用户信息。
摘要由CSDN通过智能技术生成

一、初识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,

];

}

}

#这里我们利用PHP自带的cli模式开启服务

php -S 127.0.0.1:8080 -t /www/zhangrenjie_test/test/rpc/Provider

至此,大功告成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值