RPC

RPC(Remote Procedure Call)远程过程调用,简单的理解是一个节点请求另一个节点提供的服务

代码实现

<?php
/**
* Short description for client.php
*
* @package client
* @author ache <1751987128@qq.com>
* @version 0.1
* @copyright (C) 2020 ache <1751987128@qq.com>
* @license MIT
*/


// 客户端类
class Client
{
protected $serviceName;

public function __call($name, $arguments)
{
if ($name == 'service') {
$this->serviceName = $arguments[0];
return $this;
}

// 加密数据
$msg = json_encode([
'service' => $this->serviceName,
'action' => $name,
'params' => $arguments,
]);

$client = new swoole_client(SWOOLE_SOCK_TCP);

// 连接
$client->connect('127.0.0.1', 9502);

// 发送消息
$client->send($msg);

// 接收消息
echo $client->recv();

// 关闭连接
$client->close();
}
}

$client = new Client();
// $list = $client->service('User')->index(2);
$list = $client->service('User')->list();

更多文章请关注《万象专栏》