RPC 組件配置及使用
配置
主配置
<?php
// 構(gòu)造方法內(nèi)用戶可傳入 節(jié)點(diǎn)管理器實(shí)現(xiàn)類(實(shí)現(xiàn) `NodeManagerInterface` 接口的類) 默認(rèn)為 `MemoryManager`
$config = new \EasySwoole\Rpc\Config();
// 設(shè)置服務(wù)名稱
$config->setServerName('User'); // 默認(rèn) EasySwoole
// 設(shè)置節(jié)點(diǎn)id
$config->setNodeId(\EasySwoole\Utility\Random::character(10)); // 可忽略 構(gòu)造函數(shù)已經(jīng)設(shè)置
// 【必須設(shè)置】設(shè)置異常處理器,對 Service-Worker 和 AssistWorker 的異常進(jìn)行處理,防止未捕獲導(dǎo)致進(jìn)程退出
$config->setOnException(function (\Throwable $throwable) {
});
服務(wù)端
<?php
/** @var \EasySwoole\Rpc\Config $config */
$serverConfig = $config->getServer();
// 【必須設(shè)置】設(shè)置本機(jī) ip 外網(wǎng) 或者 內(nèi)網(wǎng) ip 向其他服務(wù)端同步本機(jī)信息
$serverConfig->setServerIp('127.0.0.1');
// 設(shè)置工作進(jìn)程數(shù)量
$serverConfig->setWorkerNum(4);
// 設(shè)置監(jiān)聽地址及端口 端口可被復(fù)用
$serverConfig->setListenAddress('0.0.0.0');
$serverConfig->setListenPort('9600');
// 設(shè)置服務(wù)端最大接受包大小
$serverConfig->setMaxPackageSize(1024 * 1024 * 2);
// 設(shè)置接收客戶端數(shù)據(jù)時(shí)間
$serverConfig->setNetworkReadTimeout(3);
廣播配置
<?php
/** @var \EasySwoole\Rpc\Config $config */
$assistConfig = $config->getAssist();
// 服務(wù)定時(shí)自刷新到節(jié)點(diǎn)管理器
$assistConfig->setAliveInterval(5000);
// 廣播進(jìn)程設(shè)置
$serviceFinderConfig = $assistConfig->getUdpServiceFinder();
// 監(jiān)聽地址和端口
$serviceFinderConfig->setEnableListen(true);
$serviceFinderConfig->setListenAddress('0.0.0.0');
$serviceFinderConfig->setListenPort(9600);
// 設(shè)置廣播地址
$serviceFinderConfig->setEnableBroadcast(true);
// 255.255.255.255 udp 廣播地址
$serviceFinderConfig->setBroadcastAddress(['127.0.0.1:9600', '127.0.0.1:9601','255.255.255.255:9600']);
$serviceFinderConfig->setBroadcastInterval(5000); // 5s 廣播一次
// 設(shè)置廣播秘鑰
$serviceFinderConfig->setEncryptKey('EasySwoole');
客戶端配置
<?php
// 如果只是暴露 rpc 服務(wù) 不進(jìn)行調(diào)用別的rpc服務(wù) 可不用設(shè)置
/** @var \EasySwoole\Rpc\Config $config */
$clientConfig = $config->getClient();
// 傳輸最大數(shù)據(jù)包大小
$clientConfig->setMaxPackageSize(1024 * 1024 * 2);
// 設(shè)置全局回調(diào)函數(shù) 成功及失敗 $response->getStatus !== 0 全部為失敗
$clientConfig->setOnGlobalSuccess(function (\EasySwoole\Rpc\Protocol\Response $response){
});
$clientConfig->setOnGlobalFail(function (\EasySwoole\Rpc\Protocol\Response $response){
});
注冊服務(wù)
注冊 rpc 服務(wù)
EasySwooleEvent
事件 mainServerCreate
注冊
<?php
###### 配置服務(wù)端 ######
// 構(gòu)造方法內(nèi)用戶可傳入 節(jié)點(diǎn)管理器實(shí)現(xiàn)類(實(shí)現(xiàn) `NodeManagerInterface` 接口的類) 默認(rèn)為 `MemoryManager`
$config = new \EasySwoole\Rpc\Config();
// 設(shè)置服務(wù)名稱
$config->setServerName('User'); // 默認(rèn) EasySwoole
// 設(shè)置節(jié)點(diǎn)id,可忽略,構(gòu)造函數(shù)已經(jīng)設(shè)置
$config->setNodeId(\EasySwoole\Utility\Random::character(10)); //
// 【必須設(shè)置】設(shè)置異常處理器,對 Service-Worker 和 AssistWorker 的異常進(jìn)行處理,防止未捕獲導(dǎo)致進(jìn)程退出
$config->setOnException(function (\Throwable $throwable) {
});
$serverConfig = $config->getServer();
// 【必須設(shè)置】設(shè)置本機(jī)ip
$serverConfig->setServerIp('127.0.0.1');
/**
* 注冊服務(wù)
*/
$rpc = new \EasySwoole\Rpc\Rpc($config);
// 創(chuàng)建 ServiceOne 服務(wù)
$serviceOne = new \EasySwoole\Rpc\Tests\Service\ServiceOne();
// 在 ServiceOne 服務(wù)中添加 ModuleOne 模塊
$serviceOne->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleOne());
// 在 ServiceOne 服務(wù)中添加 ModuleTwo 模塊
$serviceOne->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleTwo());
// 創(chuàng)建 ServiceTwo 服務(wù)
$serviceTwo = new \EasySwoole\Rpc\Tests\Service\ServiceTwo();
// 在 ServiceTwo 服務(wù)中添加 ModuleOne 模塊
$serviceTwo->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleOne());
// 在 ServiceTwo 服務(wù)中添加 ModuleTwo 模塊
$serviceTwo->addModule(new \EasySwoole\Rpc\Tests\Service\ModuleTwo());
// 添加服務(wù)到服務(wù)管理器
$rpc->serviceManager()->addService($serviceOne);
$rpc->serviceManager()->addService($serviceTwo);
// 注冊服務(wù)
$http = \EasySwoole\EasySwoole\ServerManager::getInstance()->getSwooleServer();
$rpc->attachServer($http);
Redis
節(jié)點(diǎn)管理器實(shí)現(xiàn)類(實(shí)現(xiàn) NodeManagerInterface
接口即可),來完成 rpc
服務(wù)端的配置。下文將介紹使用默認(rèn)節(jié)點(diǎn)管理器(即 MemoryManager
) 完成 rpc
服務(wù)端的配置、rpc
服務(wù)的注冊及服務(wù)調(diào)用。
節(jié)點(diǎn)管理器
<?php
/** 節(jié)點(diǎn)管理器 */
// 用戶在調(diào)用rpc過程中 當(dāng)發(fā)現(xiàn)節(jié)點(diǎn)不可用 可自行調(diào)用下線
// 構(gòu)造方法內(nèi)用戶可傳入節(jié)點(diǎn)管理器實(shí)現(xiàn)`NodeManagerInterface` 默認(rèn)`MemoryManager`
$config = new \EasySwoole\Rpc\Config();
$rpc = new \EasySwoole\Rpc\Rpc($config);
$nodeManager = $rpc->getConfig()->getNodeManager();
// 獲取服務(wù)的所有節(jié)點(diǎn)
$nodeManager->getNodes('serviceOne', 1);
// 隨機(jī)獲取服務(wù)的一個(gè)節(jié)點(diǎn)
$nodeManager->getNode('serviceOne', 1);
// 下線一個(gè)服務(wù)節(jié)點(diǎn)
$nodeManager->offline(new \EasySwoole\Rpc\Server\ServiceNode());
// 刷新一個(gè)服務(wù)節(jié)點(diǎn)
$nodeManager->alive(new \EasySwoole\Rpc\Server\ServiceNode());
// 宕機(jī)一個(gè)服務(wù)節(jié)點(diǎn)
$nodeManager->failDown(new \EasySwoole\Rpc\Server\ServiceNode());