序言
本文只是結合GatewayWorker和Workerman的官方文檔和源碼,深入了解執行過程。以便更深入的了解并使用
GatewayWorker基于Workerman開發的一個項目框架。Register進程負責保存Gateway進程和BusinessWorker進程的地址,建立兩者的連接。Gateway進程負責維持客戶端連接,并轉發客戶端的數據給BusinessWorker進程處理,BusinessWorker進程負責處理實際的業務邏輯(默認調用Events.php處理業務),并將結果推送給對應的客戶端。Register、Gateway、BusinessWorker進程都是繼承Worker類實現各自的功能,所以了解GatewayWorker框架的內部執行過程,需要優先理解Worker的內容
GatewayWorker目錄結構
├── Applications // 這里是所有開發者應用項目
│ └── YourApp // 其中一個項目目錄,目錄名可以自定義
│ ├── Events.php // 開發者只需要關注這個文件
│ ├── start_gateway.php // gateway進程啟動腳本,包括端口 號等設置
│ ├── start_businessworker.php // businessWorker進程啟動 腳本
│ └── start_register.php // 注冊服務啟動腳本
│
├── start.php // 全局啟動腳本,此腳本會依次加載Applications/項目/start_*.php啟動腳本
│
└── vendor // GatewayWorker框架和Workerman框架源碼目 錄,此目錄開發者不用關心
start.php 為啟動腳本,在該腳本中,統一加載start_gateway.php start_businessworker.php start_register.php進程腳本,最后通過Worker::runAll();運行所有服務。
工作原理
1、Register、Gateway、BusinessWorker進程啟動
2、Gateway、BusinessWorker進程啟動后向Register服務進程發起長連接注冊自己
3、Register服務收到Gateway的注冊后,把所有Gateway的通訊地址保存在內存中
4、Register服務收到BusinessWorker的注冊后,把內存中所有的Gateway的通訊地址發給BusinessWorker
5、BusinessWorker進程得到所有的Gateway內部通訊地址后嘗試連接Gateway
6、如果運行過程中有新的Gateway服務注冊到Register(一般是分布式部署加機器),則將新的Gateway內部通訊地址列表將廣播給所有BusinessWorker,BusinessWorker收到后建立連接
7 、如果有Gateway下線,則Register服務會收到通知,會將對應的內部通訊地址刪除,然后廣播新的內部通訊地址列表給所有BusinessWorker,BusinessWorker不再連接下線的Gateway
8、至此Gateway與BusinessWorker通過Register已經建立起長連接
9、客戶端的事件及數據全部由Gateway轉發給BusinessWorker處理,BusinessWorker默認調用Events.php中的onConnect onMessage onClose處理業務邏輯。
10、BusinessWorker的業務邏輯入口全部在Events.php中,包括onWorkerStart進程啟動事件(進程事件)、onConnect連接事件(客戶端事件)、onMessage消息事件(客戶端事件)、onClose連接關閉事件(客戶端事件)、onWorkerStop進程退出事件(進程事件)
1 Register、Gateway、BusinessWorker進程啟動
項目根目錄下的start.php 為啟動腳本,在該腳本中,加載start_gateway.php start_businessworker.php start_register.php進程腳本,完成各個服務的Worker初始化:
// 加載所有Applications/*/start.php,以便啟動所有服務
foreach(glob(__DIR__.'/Applications/*/start*.php') as $start_file)
{
require_once $start_file;
}
最后通過Worker::runAll();運行所有服務。
// 運行所有服務
Worker::runAll();
運行所有服務,先看一遍runAll()方法的執行內容
public static function runAll()
{
// 檢查運行環境
self::checkSapiEnv();
//初始化環境變量
self::init();
// 解析命令
self::parseCommand();
// 嘗試以守護進程模式運行
self::daemonize();
// 初始化所有worker實例,主要是監聽端口
self::initWorkers();
// 初始化所有信號處理函數
self::installSignal();
// 保存主進程pid
self::saveMasterPid();
// 展示啟動界面
self::displayUI();
// 創建子進程(worker進程),然后給每個子進程綁定loop循環監聽事件tcp
self::forkWorkers();
// 嘗試重定向標準輸入輸出
self::resetStd();
// 監控所有子進程(worker進程)
self::monitorWorkers();
}
self::init()初始化環境變量中,有以下部分代碼,保存$_idMap從PID映射到工作進程ID
// Init data for worker id. self::initId(); protected static function initId() { foreach (self::$_workers as $worker_id => $worker) { $new_id_map = array(); for($key = 0; $key < $worker->count; $key++) { $new_id_map[$key] = isset(self::$_idMap[$worker_id] [$key]) ? self::$_idMap[$worker_id][$key] : 0; } self::$_idMap[$worker_id] = $new_id_map; } }
self::forkWorkers()方法通過循環self::$_workers數組,fork各自worker的count數量的進程。然后通過調用
$worker->run();
運行當前worker實例,在run方法中通過
if (!self::$globalEvent) { $event_loop_class = self::getEventLoopName(); self::$globalEvent = new $event_loop_class; // Register a listener to be notified when server socket is ready to read. if ($this->_socketName) { if ($this->transport !== 'udp') { self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection')); } else { self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection')); } }
獲取一個當前可用的事件輪詢方式,然后根據當前的協議類型添加一個監聽到事件輪詢中
然后,嘗試出發當前進程模型的onWorkerStart回調,此回調會在Gateway類以及BusinessWorker類中都會定義,代碼if ($this->onWorkerStart) { try { call_user_func($this->onWorkerStart, $this); } catch (\Exception $e) { self::log($e); // Avoid rapid infinite loop exit. sleep(1); exit(250); } catch (\Error $e) { self::log($e); // Avoid rapid infinite loop exit. sleep(1); exit(250); } }
最后,執行事件的循環等待socket事件,處理讀寫等操作,代碼
// Main loop. self::$globalEvent->loop();
以上是runAll()方法的部分內容,會在了解GatewayWorker的工作原理的時候用到
2.1 Gateway進程向Register服務進程發起長連接注冊自己
初始化Gateway
$gateway = new Gateway("text://0.0.0.0:8383");
在Gateway類中重寫run方法,當調用runAll()方法啟動進程時,fork進程之后,運行worker實例的時候,會調用到此重寫的run方法
public function run()
{
// 保存用戶的回調,當對應的事件發生時觸發
$this->_onWorkerStart = $this->onWorkerStart;
$this->onWorkerStart = array($this, 'onWorkerStart');
// 保存用戶的回調,當對應的事件發生時觸發
$this->_onConnect = $this->onConnect;
$this->onConnect = array($this, 'onClientConnect');
// onMessage禁止用戶設置回調
$this->onMessage = array($this, 'onClientMessage');
// 保存用戶的回調,當對應的事件發生時觸發
$this->_onClose = $this->onClose;
$this->onClose = array($this, 'onClientClose');
// 保存用戶的回調,當對應的事件發生時觸發
$this->_onWorkerStop = $this->onWorkerStop;
$this->onWorkerStop = array($this, 'onWorkerStop');
$this->_startTime = time();
// 運行父方法
parent::run();
}
定義了$this->onWorkerStart回調,
$this->onWorkerStart = array($this, 'onWorkerStart');
執行到Worker類中的run()方法時,被觸發。即,上邊提到的調用Gateway類中的onWorkerStart方法,代碼
public function onWorkerStart()
{
$this->lanPort = $this->startPort + $this->id;
if ($this->pingInterval > 0) {
$timer_interval = $this->pingNotResponseLimit > 0 ? $this->pingInterval / 2 : $this->pingInterval;
Timer::add($timer_interval, array($this, 'ping'));
}
if ($this->lanIp !== '127.0.0.1') {
Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, array($this, 'pingBusinessWorker'));
}
if (strpos($this->registerAddress, '127.0.0.1') !== 0) {
Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, array($this, 'pingRegister'));
}
if (!class_exists('\Protocols\GatewayProtocol')) {
class_alias('GatewayWorker\Protocols\GatewayProtocol', 'Protocols\GatewayProtocol');
}
// 初始化 gateway 內部的監聽,用于監聽 worker 的連接已經連接上發來的數據
$this->_innerTcpWorker = new Worker("GatewayProtocol://{$this->lanIp}:{$this->lanPort}");
$this->_innerTcpWorker->listen();
// 重新設置自動加載根目錄
Autoloader::setRootPath($this->_autoloadRootPath);
// 設置內部監聽的相關回調
$this->_innerTcpWorker->onMessage = array($this, 'onWorkerMessage');
$this->_innerTcpWorker->onConnect = array($this, 'onWorkerConnect');
$this->_innerTcpWorker->onClose = array($this, 'onWorkerClose');
// 注冊 gateway 的內部通訊地址,worker 去連這個地址,以便 gateway 與 worker 之間建立起 TCP 長連接
$this->registerAddress();
if ($this->_onWorkerStart) {
call_user_func($this->_onWorkerStart, $this);
}
}
$this->startPort : 內部通訊起始端口,假如$gateway->count=4,起始端口為4000,可在gateway啟動腳本中自定義
$this->id : 基于worker實例分配的進程編號,當前從0開始,根據count自增。在fork進程的時候生成
$this->registerAddress(): 代碼中$this->registerAddress是在start_gateway.php初始化Gateway類之后定義的。該端口是Register進程所監聽。此處異步的向Register進程發送數據,存儲當前 Gateway 的內部通信地址
public function registerAddress()
{
$address = $this->lanIp . ':' . $this->lanPort;
$this->_registerConnection = new AsyncTcpConnection("text://{$this->registerAddress}");
$this->_registerConnection->send('{"event":"gateway_connect", "address":"' . $address . '", "secret_key":"' . $this->secretKey . '"}');
$this->_registerConnection->onClose = array($this, 'onRegisterConnectionClose');
$this->_registerConnection->connect();
}
$this->lanIp: Gateway所在服務器的內網IP
2.2 BusinessWorker進程向Register服務進程發起長連接注冊自己
BusinessWorker類中同樣重寫run方法,定義了$this->onWorkerStart
public function run()
{
$this->_onWorkerStart = $this->onWorkerStart;
$this->_onWorkerReload = $this->onWorkerReload;
$this->_onWorkerStop = $this->onWorkerStop;
$this->onWorkerStop = array($this, 'onWorkerStop');
$this->onWorkerStart = array($this, 'onWorkerStart');
$this->onWorkerReload = array($this, 'onWorkerReload');
parent::run();
}
執行Worker類中的run方法,觸發BusinessWorker中的onWorkerStart
protected function onWorkerStart()
{
if (!class_exists('\Protocols\GatewayProtocol')) {
class_alias('GatewayWorker\Protocols\GatewayProtocol', 'Protocols\GatewayProtocol');
}
$this->connectToRegister();
\GatewayWorker\Lib\Gateway::setBusinessWorker($this);
\GatewayWorker\Lib\Gateway::$secretKey = $this->secretKey;
if ($this->_onWorkerStart) {
call_user_func($this->_onWorkerStart, $this);
}
if (is_callable($this->eventHandler . '::onWorkerStart')) {
call_user_func($this->eventHandler . '::onWorkerStart', $this);
}
if (function_exists('pcntl_signal')) {
// 業務超時信號處理
pcntl_signal(SIGALRM, array($this, 'timeoutHandler'), false);
} else {
$this->processTimeout = 0;
}
// 設置回調
if (is_callable($this->eventHandler . '::onConnect')) {
$this->_eventOnConnect = $this->eventHandler . '::onConnect';
}
if (is_callable($this->eventHandler . '::onMessage')) {
$this->_eventOnMessage = $this->eventHandler . '::onMessage';
} else {
echo "Waring: {$this->eventHandler}::onMessage is not callable\n";
}
if (is_callable($this->eventHandler . '::onClose')) {
$this->_eventOnClose = $this->eventHandler . '::onClose';
}
// 如果Register服務器不在本地服務器,則需要保持心跳
if (strpos($this->registerAddress, '127.0.0.1') !== 0) {
Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, array($this, 'pingRegister'));
}
}
通過connectToRegister方法,發送數據到Register進程,連接服務注冊中心
public function connectToRegister()
{
$this->_registerConnection = new AsyncTcpConnection("text://{$this->registerAddress}");
$this->_registerConnection->send('{"event":"worker_connect","secret_key":"' . $this->secretKey . '"}');
$this->_registerConnection->onClose = array($this, 'onRegisterConnectionClose');
$this->_registerConnection->onMessage = array($this, 'onRegisterConnectionMessage');
$this->_registerConnection->connect();
}
3 Register服務收到Gateway的注冊后,把所有的Gateway的通訊地址保存在內存中
在Register類中,重寫了run方法,定義了當前的
$this->onConnect = array($this, 'onConnect');
// 設置 onMessage 回調
$this->onMessage = array($this, 'onMessage');
// 設置 onClose 回調
$this->onClose = array($this, 'onClose');
三個屬性,當Register啟動的進程收到消息時,會觸發onMessage方法
public function onMessage($connection, $buffer)
{
// 刪除定時器
Timer::del($connection->timeout_timerid);
$data = @json_decode($buffer, true);
if (empty($data['event'])) {
$error = "Bad request for Register service. Request info(IP:".$connection->getRemoteIp().", Request Buffer:$buffer). See http://wiki.workerman.net/Error4 for detail";
Worker::log($error);
return $connection->close($error);
}
$event = $data['event'];
$secret_key = isset($data['secret_key']) ? $data['secret_key'] : '';
// 開始驗證
switch ($event) {
// 是 gateway 連接
case 'gateway_connect':
if (empty($data['address'])) {
echo "address not found\n";
return $connection->close();
}
if ($secret_key !== $this->secretKey) {
Worker::log("Register: Key does not match ".var_export($secret_key, true)." !== ".var_export($this->secretKey, true));
return $connection->close();
}
$this->_gatewayConnections[$connection->id] = $data['address'];
$this->broadcastAddresses();
break;
// 是 worker 連接
case 'worker_connect':
if ($secret_key !== $this->secretKey) {
Worker::log("Register: Key does not match ".var_export($secret_key, true)." !== ".var_export($this->secretKey, true));
return $connection->close();
}
$this->_workerConnections[$connection->id] = $connection;
$this->broadcastAddresses($connection);
break;
case 'ping':
break;
default:
Worker::log("Register unknown event:$event IP: ".$connection->getRemoteIp()." Buffer:$buffer. See http://wiki.workerman.net/Error4 for detail");
$connection->close();
}
}
當$event = ‘gateway_connect’時,是Gateway發來的注冊消息,保存到$this->_gatewayConnections數組中,在通過broadcastAddresses方法將當前$this->_gatewayConnections中所有的Gatewat通訊地址轉發給所有BusinessWorker進程
4 Register服務收到BusinessWorker的注冊后,把內存中所有的Gateway的通訊地址發給BusinessWorker
同第3步中,Register類收到BusinessWorker的注冊時,會觸發onMessage方法中的worker_connect,case選項。同時,將當前worker連接加入到$_workerConnections數組中,在通過broadcastAddresses方法將當前$this->_gatewayConnections中所有的Gatewat通訊地址轉發給所有BusinessWorker進程。
5 BusinessWorker進程得到所有的Gateway內部通訊地址后嘗試連接Gateway
在BusinessWoker類的啟動中,通過重寫run方法,定義的啟動onWorkerStart方法中,通過connectToRegister方法注冊服務中心的同時,也定義了onMessage匿名函數,用于接收消息回調。
$this->_registerConnection->onMessage = array($this, 'onRegisterConnectionMessage');
即,當注冊中心發來消息時候,回調到此處
public function onRegisterConnectionMessage($register_connection, $data)
{
$data = json_decode($data, true);
if (!isset($data['event'])) {
echo "Received bad data from Register\n";
return;
}
$event = $data['event'];
switch ($event) {
case 'broadcast_addresses':
if (!is_array($data['addresses'])) {
echo "Received bad data from Register. Addresses empty\n";
return;
}
$addresses = $data['addresses'];
$this->_gatewayAddresses = array();
foreach ($addresses as $addr) {
$this->_gatewayAddresses[$addr] = $addr;
}
$this->checkGatewayConnections($addresses);
break;
default:
echo "Receive bad event:$event from Register.\n";
}
}
其中Register類發來的數據是
$data = array(
'event' => 'broadcast_addresses',
'addresses' => array_unique(array_values($this->_gatewayConnections)),
);
這個時候,就會通過checkGatewayConnections方法檢查gateway的這些通信端口是否都已經連接,在通過tryToConnectGateway方法嘗試連接gateway的這些內部通信地址
6 Gateway進程收到BusinessWorker進程的連接消息
同樣,在Gateway進程啟動的時候,觸發的onWorkerStart方法中,也定義了一個內部通訊的onWorkerMessage
$this->_innerTcpWorker->onMessage = array($this, 'onWorkerMessage');
由此來接收BusinessWorker進程發來的連接消息,部分代碼
public function onWorkerMessage($connection, $data)
{
$cmd = $data['cmd'];
if (empty($connection->authorized) && $cmd !== GatewayProtocol::CMD_WORKER_CONNECT && $cmd !== GatewayProtocol::CMD_GATEWAY_CLIENT_CONNECT) {
self::log("Unauthorized request from " . $connection->getRemoteIp() . ":" . $connection->getRemotePort());
return $connection->close();
}
switch ($cmd) {
// BusinessWorker連接Gateway
case GatewayProtocol::CMD_WORKER_CONNECT:
$worker_info = json_decode($data['body'], true);
if ($worker_info['secret_key'] !== $this->secretKey) {
self::log("Gateway: Worker key does not match ".var_export($this->secretKey, true)." !== ". var_export($this->secretKey));
return $connection->close();
}
$key = $connection->getRemoteIp() . ':' . $worker_info['worker_key'];
// 在一臺服務器上businessWorker->name不能相同
if (isset($this->_workerConnections[$key])) {
self::log("Gateway: Worker->name conflict. Key:{$key}");
$connection->close();
return;
}
$connection->key = $key;
$this->_workerConnections[$key] = $connection;
$connection->authorized = true;
return;
// GatewayClient連接Gateway
將worker的進程連接保存到$this->_workerConnections[$key] = $connection;
7 Gateway進程收到客戶端的連接,消息時,會通過Gateway轉發給worker處理
// Gateway類的run方法中定義此屬性
$this->onMessage = array($this, 'onClientMessage');
// 收到客戶端消息的時候出發此函數
public function onClientMessage($connection, $data)
{
$connection->pingNotResponseCount = -1;
$this->sendToWorker(GatewayProtocol::CMD_ON_MESSAGE, $connection, $data);
}
在sendToWorker方法中,將數據發給worker進程處理