swoole实现简易RPC

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();
<?php
/**
 * Short description for server.php
 *
 * @package server
 * @author ache <1751987128@qq.com>
 * @version 0.1
 * @copyright (C) 2020 ache <1751987128@qq.com>
 * @license MIT
 */

// 引入用户类
include_once 'User.php';

// 服务端类
class Server
{
    protected $server;

    public function __construct()
    {
        $this->server = new swoole_server('0.0.0.0', 9502);
        $this->onReceive();
        $this->start();
    }
    /**
     * 接受事件
     *
     * @return void
     */
    public function onReceive()
    {
        $this->server->on('receive', function ($ser, $fd, $reactor_id, $data) {
            $data = json_decode($data, true);
            $service = $data['service'];
            $action = $data['action'];
            $params = $data['params'];

            $instance = new $service;
            $res = $instance->$action(...$params);
            $ser->send($fd, $res);
        });
    }


    /**
     * 开启事件
     *
     * @return void
     */
    public function start()
    {
        $this->server->start();
    }
}

$server = new Server();
<?php
/**
 * Short description for Test.php
 *
 * @package Test
 * @author ache <1751987128@qq.com>
 * @version 0.1
 * @copyright (C) 2020 ache <1751987128@qq.com>
 * @license MIT
 */

class User
{
    /**
     * 用户详情
     *
     * @return void
     */
    public function index($id)
    {
        if ($id == 1) {
            return json_encode(['id'=>1, 'name'=>'张三']);
        } elseif ($id == 2) {
            return json_encode(['id'=>2, 'name'=>'李四']);
        } else {
            return json_encode([]);
        }
    }

    /**
     * 用户列表
     *
     * @return void
     */
    public function list()
    {
        return json_encode([
            ['id'=>1, 'name'=>'张三'],
            ['id'=>2, 'name'=>'李四'],
        ]);
    }
}

运行

php server.php

浏览器访问http://localhost/client.php

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

the ache

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值