PHP code example of simple-swoole / rpc-multiplex

1. Go to this page and download the library: Download simple-swoole/rpc-multiplex library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simple-swoole / rpc-multiplex example snippets


composer create-project simple-swoole/skeleton
cd skeleton
composer 

namespace App\Service;

class RpcService
{
    public function hello(string $name): string
    {
        return 'Hello ' . $name;
    }
}

return [
    ['GET', '/Health', function ($request, $response) {
        $response->end('Im ok');//定义健康监测接口给consul调用
    }],
];


return [
    'main' => [
        'class_name' => \Swoole\Http\Server::class,
        'ip' => '0.0.0.0',
        'port' => 9501,//主服务用来接收consul的心跳请求
        'sock_type' => SWOOLE_SOCK_TCP,
        'callbacks' => [
            'request' => [\App\Events\Http::class, 'onRequest'],
            'start' => [Simps\RpcMultiplex\TcpServer::class, 'onStart'],
            'workerStart' => [Simps\RpcMultiplex\TcpServer::class, 'onWorkerStart'],
        ],
        'settings' => [
            'worker_num' => swoole_cpu_num(),
        ],
        'sub' => [
             //定义rpc服务,注册服务到consul
                [
                'callbacks' => [
                    "receive" => [Simps\RpcMultiplex\TcpServer::class, 'onReceive'],
                ],
                'port' => 9503,//rpc服务监听9503端口
                'sock_type' => SWOOLE_SOCK_TCP,
                //注册到consul时候,'0.0.0.0'默认会拿第一块网卡的ip注册到consul
                'ip' => '0.0.0.0',
                //服务名称
                'service_name' => ['rpc1','rpc2'],
                //有`consul`配置的时候会自动注册到consul
                'consul' => [
                    'publish_to' => 'http://127.0.0.1:8500', //本机的consul client地址
                    "checks" => [
                            [
                            "name" => "rpc1",
                            "http" => "http://127.0.0.1:9501/Health", //consul请求这个地址 连通就证明服务可用,checks的更多配置请参考consul官方文档。
                            "interval" => "5s",
                        ],
                            [
                            "name" => "rpc2",
                            "http" => "http://127.0.0.1:9501/Health", //consul请求这个地址 连通就证明服务可用,checks的更多配置请参考consul官方文档。
                            "interval" => "5s",
                        ]
                    ]
                ],
                'settings' => [
                    // 以下参数不允许修改
                    'open_length_check' => true,
                    'package_length_type' => 'N',
                    'package_length_offset' => 0,
                    'package_body_offset' => 4,
                    'package_max_length' => 1024 * 1024 * 2,
                ],
            ],
        ]
    ]
];



class IndexController
{
    public function index($request, $response)
    {
        $serviceName = 'rpc1';
        $consulUri = 'http://127.0.0.1:8500';//本机的consul client地址,框架会通过这个consul client找到所有可用的机器,随机找到一台机器进行rpc调用,如果所有机器都挂了会抛异常
        $data = CallWithConsul::getInstance($serviceName, $consulUri)->call('App\Service\RpcService', 'hello', 'World'); //call(类名,方法,参数)

        $response->end(
                json_encode(
                        [
                            'method' => $request->server['request_method'],
                            'message' => 'Hello Simps. ' . $data->getResult(),
                        ]
                )
        );
    }

}