PHP code example of easyswoole / rpc

1. Go to this page and download the library: Download easyswoole/rpc 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/ */

    

easyswoole / rpc example snippets




// 构造方法内用户可传入节点管理器实现`NodeManagerInterface` 默认`MemoryManager`
$config = new \EasySwoole\Rpc\Config();

/** 服务端配置 */

// 设置服务名称
$config->setServerName('User'); // 默认 EasySwoole

// 设置节点id
$config->setNodeId(\EasySwoole\Utility\Random::character(10)); // 可忽略 构造函数已经设置

// 设置异常处理器 对Service-Worker 和 AssistWorker的异常进行处理 必须设置 防止未捕获导致进程退出
$config->setOnException(function (\Throwable $throwable) {

});

$serverConfig = $config->getServer();

// 设置本机ip 必须设置
$serverConfig->setServerIp('127.0.0.1');

// 设置工作进程数量
$serverConfig->setWorkerNum(4);

// 设置监听地址及端口
$serverConfig->setListenAddress('0.0.0.0');
$serverConfig->setListenPort('9600');

// 设置服务端最大接受包大小
$serverConfig->setMaxPackageSize(1024 * 1024 * 2);

// 设置接收客户端数据时间
$serverConfig->setNetworkReadTimeout(3);

/** 广播设置 */

$assistConfig = $config->getAssist();

// 服务定时自刷新到节点管理器
$assistConfig->setAliveInterval(5000);

// 广播进程设置
$serviceFinderConfig = $assistConfig->getUdpServiceFinder();

// 监听地址和端口
$serviceFinderConfig->setEnableListen(true);
$serviceFinderConfig->setListenAddress('0.0.0.0');
$serviceFinderConfig->setListenPort(9600);

// 设置广播地址
$serviceFinderConfig->setEnableBroadcast(true);
$serviceFinderConfig->setBroadcastAddress(['127.0.0.1:9600', '127.0.0.1:9601']);
$serviceFinderConfig->setBroadcastInterval(5000); // 5s 广播一次

// 设置广播秘钥
$serviceFinderConfig->setEncryptKey('EasySwoole');

/** 客户端设置 */

// 如果只是暴露rpc服务 不进行调用别的rpc服务 可不用设置

$clientConfig = $config->getClient();

// 传输最大数据包大小
$clientConfig->setMaxPackageSize(1024 * 1024 * 2);

// 设置全局回调函数  成功及失败 $response->getStatus !== 0 全部为失败
$clientConfig->setOnGlobalSuccess(function (\EasySwoole\Rpc\Protocol\Response $response){

});
$clientConfig->setOnGlobalFail(function (\EasySwoole\Rpc\Protocol\Response $response){

});

/** 注册服务 */

$rpc = new \EasySwoole\Rpc\Rpc($config);

$serviceOne = new \EasySwoole\Rpc\Tests\Service\ServiceOne();
$serviceOne->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleOne());
$serviceOne->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleTwo());

$serviceTwo = new \EasySwoole\Rpc\Tests\Service\ServiceTwo();
$serviceTwo->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleOne());
$serviceTwo->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleTwo());

$rpc->serviceManager()->addService($serviceOne);
$rpc->serviceManager()->addService($serviceTwo);

/** 客户端调用 */

$client = $rpc->client();
// 添加请求
$ctx1 = $client->addRequest('ServiceOne.ModuleOne.action');
// 设置参数
$ctx1->setArg(['a','b','c']);
$ctx1->setOnSuccess(function (\EasySwoole\Rpc\Protocol\Response $response){

});
$ctx1->setOnFail(function (\EasySwoole\Rpc\Protocol\Response $response){

});

$ctx2 = $client->addRequest('ServiceTwo.ModuleOne.action');
// 设置参数
$ctx2->setArg(['a','b','c']);
$ctx2->setOnSuccess(function (\EasySwoole\Rpc\Protocol\Response $response){

});
$ctx2->setOnFail(function (\EasySwoole\Rpc\Protocol\Response $response){

});

$client->exec(3);

/** 节点管理器 */

// 用户在调用rpc过程中 当发现节点不可用 可自行调用下线

$nodeManager = $rpc->getConfig()->getNodeManager();

// 获取服务的所有节点
$nodeManager->getNodes('serviceOne', 1);

// 随机获取服务的一个节点
$nodeManager->getNode('serviceOne', 1);

// 下线一个服务节点
$nodeManager->offline(new \EasySwoole\Rpc\Server\ServiceNode());

// 刷新一个服务节点
$nodeManager->alive(new \EasySwoole\Rpc\Server\ServiceNode());

// 宕机一个服务节点
$nodeManager->failDown(new \EasySwoole\Rpc\Server\ServiceNode());

use EasySwoole\Rpc\Config;
use EasySwoole\Rpc\Protocol\Response;
use EasySwoole\Rpc\Rpc;
use EasySwoole\Rpc\Tests\Service\ModuleOne;
use EasySwoole\Rpc\Tests\Service\ServiceOne;
use Swoole\Http\Server;
e($service);

$http = new Server('0.0.0.0', 9501);

$rpc->attachServer($http);

$http->on('request', function ($request, $response) use($rpc){
    $client = $rpc->client();
    $ctx1 = $client->addRequest('Service.Module');
    $ctx2 = $client->addRequest('Service.Module.action');
    $ctx2->setArg('xxx');
    $ctx2->setOnSuccess(function (Response $response){
        var_dump($response->getMsg());
    });
    $client->exec();
});

$http->start();

public static function mainServerCreate(EventRegister $register)
{
    $config = new \EasySwoole\Rpc\Config();
    $config->getServer()->setServerIp('127.0.0.1');
    
    $rpc = new \EasySwoole\Rpc\Rpc($config);
    
    $service = new \EasySwoole\Rpc\Tests\Service\ServiceOne();
    $service->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleOne());
    
    $rpc->serviceManager()->addService($service);
    
    
    $rpc->attachServer(ServerManager::getInstance()->getSwooleServer());
}