美文网首页
swoole实现简易RPC

swoole实现简易RPC

作者: theache | 来源:发表于2020-04-26 03:46 被阅读0次

    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

    image

    相关文章

      网友评论

          本文标题:swoole实现简易RPC

          本文链接:https://www.haomeiwen.com/subject/seyrwhtx.html