This article does not involve technical sharing. Those who want to read technical blog posts can turn it off in the upper right corner.

1. Nagging

Since I went to the information center for work-study in my freshman year, I got in touch with PHP by accident. It has been 10 now. The development of the Internet, from the earliest stand-alone application, to Nginx load balancing, and now k8s + micro-services are gradually popularized , the best language will inevitably go downhill, after all, it is still a scripting language. In the face of the microservice architecture, PHP like a teenager who has lost his spiritual roots in the Internet, and is forced to cultivate himself, alas

Record a super rough PHP remote call, goodbye, and embrace DevOps + continuous delivery + + containers in the future.

2.RpcServer.php

<?php
class RpcServer{

    private $port = 0;
    private $host = '';

    public function __construct($host, $port){
        $this->host = $host;
        $this->port = $port;
    }

    public function run(){
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if (!$socket) {
            echo "socket_create falied \n";
            return;
        }
        //为套接字绑定ip和端口
        if( !socket_bind($socket, $this->host, $this->port) ) return;
        //监听socket
        if( !socket_listen($socket,4) ) return;

        while(true)
        {
            //接收客户端请求
            if(($msgsocket = socket_accept($socket)) !== false)
            {
                //读取请求内容
                $buf = socket_read($msgsocket, 1024);
                echo "Received msg: $buf \n";
                $obj = json_decode($buf, true);
                if (is_array($obj)
                    && isset($obj['Class'])
                    && isset($obj['Method'])
                ) {
                    $instance = (new $obj['Class']);
                    $method   = $obj['Method'];
                    $str = $instance->$method(...$obj['params']);
                }else{
                    $str = "RPC: Hello!";
                }                
                //向连接的客户端发送数据 
                socket_write($msgsocket, $str,strlen($str));
                //操作完之后需要关闭该连接否则 feof() 函数无法正确识别打开的句柄是否读取完成
                socket_close($msgsocket);
            }
        }
    }
}
//RPC测试类
class Test{
    public function add($a, $b){
        return $a + $b;
    }
}

( new RpcServer('192.168.27.128', 8888) )->run();

3.RpcClient.php

<?php
class RpcClient{
    private $className = '';

    private function __construct($className){
        $this->className = $className;
    }

    public static function getInstance($className){
        return new RpcClient($className);
    }

    public function __call($name, $arguments){
        var_dump($arguments);
        $st = json_encode([
            'Class' => $this->className,
            'Method' => $name,
            'params' => $arguments,
        ]);
        $length = strlen($st);
        //创建tcp套接字
        $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
        if (!$socket) {
            echo 'socket_create failed'.PHP_EOL;
            return;
        }
        //连接tcp
        if(!socket_connect($socket, '192.168.27.128',8888)){
            echo 'socket_connect failed'.PHP_EOL;
            return;
        }
        //向打开的套集字写入数据(发送数据)
        $s = socket_write($socket, $st, $length);
        //从套接字中获取服务器发送来的数据
        $msg = socket_read($socket,1024);

        echo 'Server :'.$msg.PHP_EOL;
        //关闭连接
        socket_close($socket);
    }
}

$test = RpcClient::getInstance('Test');
echo $test->add(4, 6);

4.Result

Another call in the LAN:

[root@localhost ~]# php RPCClient.php 
array(2) {
  [0]=>
  int(4)
  [1]=>
  int(6)
}
Server :10

tfzh
231 声望17 粉丝

code what u love & love what u code


引用和评论

0 条评论