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