swoole achieve a simple rpc call

Introduction

rpc translates remote procedure call, which implements the form restful apiin the form of calls and tcpform of calls. Of course, terms of performance, is certainly tcphigher performance in the form of calls.

Here with a tcpbriefly about the rpcimplementation process.

Directory Structure

│  Client.php 		// 客户端
│  composer.json	// 主要为了自动加载
│  Server.php		// 服务端
│
├─Service			// 存放各种服务的文件夹
│      CartService.php // 具体的服务类
│
└─vendor
    │  autoload.php

Fundamental

rpcUse tcpbasic principle is to call the service you will need to call the class, class and method to be called in, in the tcppass over the parameters, and then parse the parameters on the server side method calls the specified class.

Show

Client.php

<?php


namespace rpc;


class Client
{
    protected $service;

    public function __call($name, $arguments)
    {
        if ($name == 'service') {
            $this->service = $arguments[0];
            return $this;
        }
        $json_data = json_encode([
            'service' => $this->service,
            'action' => $name,
            'param' => $arguments[0],
        ]);
        $client = new \Swoole\Client(SWOOLE_SOCK_TCP);
        if (!$client->connect('127.0.0.1', 9501, -1)) {
            exit("connect failed. Error: {$client->errCode}\n");
        }
        $client->send($json_data);
        $result = $client->recv();
        var_dump($result);
        $client->close();
    }
}

$client = new Client();
$client->service('CartService')->getList(1);

Server.php

<?php
require_once 'vendor/autoload.php';

class Server
{
    protected $server;

    public function __construct()
    {
        $this->server = new \Swoole\Server("127.0.0.1", 9501);
        $this->onConnect();
        $this->onReceive();
        $this->onClose();
        $this->start();;
    }

    public function onConnect()
    {
        $this->server->on('Connect', function ($serv, $fd) {
            echo "Client: Connect.\n";
        });
    }

    public function onReceive()
    {
        $this->server->on('Receive', function ($serv, $fd, $from_id, $data) {
            $data = json_decode($data, true);
            $service = $data['service'];
            $action = $data['action'];
            $page = $data['param'];
            $class = "rpc\\Service\\" . $service;
            $instance = new $class;
            $result = json_encode($instance->$action($page));
            $serv->send($fd, $result);
        });
    }

    public function onClose()
    {
        $this->server->on('Close', function ($serv, $fd) {
            echo "Client: Close.\n";
        });
    }

    public function start()
    {
        $this->server->start();
    }
}

$server = new Server();

CartService.php

<?php


namespace rpc\Service;


class CartService
{
    public function getList($page)
    {
        return [
            [
                'name' => 'zhangsan',
                'sex' => $page,
            ],
            [
                'name' => 'lisi',
                'sex' => 0,
            ]
        ];
    }
}

composer.json (main statement about load automatically rule)

{
    "name": "gift/rpc",
    "authors": [
        {
            "name": "gift",
            "email": "[email protected]"
        }
    ],
    "require": {},
    "autoload": {
        "psr-4": {
            "rpc\\": ""
        }
    }
}

Results of the

After starting the server, we then execute client, you will receive the following returns the contents:

string(53) "[{"name":"zhangsan","sex":1},{"name":"lisi","sex":0}]"

demo download link: https: //github.com/zhang-jianqiang/rpcdemo

Published 48 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_37825371/article/details/104909723