Example of combining Swoole's RPC service with ThinkPHP framework

Description

All three files are in the same directory

Config.php

<?php
$config = [];
$config['User']['worker'] = '8888';
$config['User']['ip'] = '127.0.0.1';

$config['Order']['worker'] = '8887';
$config['Order']['ip'] = '127.0.0.1';

return $config;

Order service swoole_server_order.php

<?php

class server{
    
    
    public function get_data($_arr){
    
    
        $action = $_arr['action'];
        $site = $_arr['site'];
        $param = isset($_arr['param']) ? $_arr['param'] : [];
        static $import = true;
        if ($import == true) {
    
    
            define('APP_PATH', "/usr/local/nginx/html/{
      
      $site}/application"); //框架目录
            define('RPC_RUN', true);
        }
        $import = false;
        $_REQUEST['argv_rpc'] = $action; //
        $path = "/usr/local/nginx/html/{
      
      $site}/thinkphp/base.php"; //base文件所在路径
        require_once $path;
        $_GET = $_POST = $param;
        $app = new \think\App();
        $data = $app->run();
        return $data;
    }
}
$serv=new swoole_server('127.0.0.1',8887);//创建服务
$serv->set(array('worker_num'=>2));
$serv->on("receive",function($serv,$fd,$from_id,$data){
    
    
    //$data 接受客户端发送的数据
    $data = json_decode($data,true);
    $server = new server();
    $result = $server->get_data($data);
    $serv->send($fd,json_encode(['data'=>$result]));//给客户端发送数据
});
$serv->start();


User service swoole_server_order.php

<?php

class server{
    
    
    public function get_data($_arr){
    
    
        $action = $_arr['action'];
        $site = $_arr['site'];
        $param = isset($_arr['param']) ? $_arr['param'] : [];
        static $import = true;
        if ($import == true) {
    
    
            define('APP_PATH', "/usr/local/nginx/html/{
      
      $site}/application"); //框架目录
            define('RPC_RUN', true);
        }
        $import = false;
        $_REQUEST['argv_rpc'] = $action; //
        $path = "/usr/local/nginx/html/{
      
      $site}/thinkphp/base.php"; //base文件所在路径
        require_once $path;
        $_GET = $_POST = $param;
        $app = new \think\App();
        $data = $app->run();
        return $data;
    }
}
$serv=new swoole_server('127.0.0.1',8888);//创建服务
$serv->set(array('worker_num'=>2));
$serv->on("receive",function($serv,$fd,$from_id,$data){
    
    
    //$data 接受客户端发送的数据
    $data = json_decode($data,true);
    $server = new server();
    $result = $server->get_data($data);
    $serv->send($fd,json_encode(['data'=>$result]));//给客户端发送数据
});
$serv->start();


swoole_client.php

<?php
$config = include_once './Config.php';
//客户端:
$site = $_GET['site'];
//$_GET['action'] = index/index/user
$action = $_GET['action'];
$client_info = $config[$site];

$cli = new swoole_client(SWOOLE_SOCK_TCP);
$cli->connect($client_info['ip'], $client_info['worker']);

$data = json_encode(['site' => $site, 'action' => $action]);
$cli->send($data);
$result = $cli->recv();//接收消息
$cli->close();
var_dump($result);

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_39218464/article/details/113787129