手冊中寫刀,阿里云等這些,通過彈性公網(wǎng)IP 轉發(fā)到ECS上的云服務器,是沒有辦法進行 公網(wǎng)分布式部署的,因為
當gateway啟動時候,會執(zhí)行
// 注冊 gateway 的內(nèi)部通訊地址,worker 去連這個地址,以便 gateway 與 worker 之間建立起 TCP 長連接
public function onWorkerstart(){
//每個進程執(zhí)行一次
$this->registerAddress();
}
public function registerAddress()
{
$address = $this->lanIp . ':' . $this->lanPort;
foreach ($this->registerAddress as $register_address) {
$register_connection = new AsyncTcpConnection("text://{$register_address}");
$secret_key = $this->secretKey;
$register_connection->onConnect = function($register_connection) use ($address, $secret_key, $register_address){
$register_connection->send('{"event":"gateway_connect", "address":"' . $address . '", "secret_key":"' . $secret_key . '"}');
// 如果Register服務器不在本地服務器,則需要保持心跳
if (strpos($register_address, '127.0.0.1') !== 0) {
$register_connection->ping_timer = Timer::add(self::PERSISTENCE_CONNECTION_PING_INTERVAL, function () use ($register_connection) {
$register_connection->send('{"event":"ping"}');
});
}
};
$register_connection->onClose = function ($register_connection) {
if(!empty($register_connection->ping_timer)) {
Timer::del($register_connection->ping_timer);
}
$register_connection->reconnect(1);
};
$register_connection->connect();
}
// 初始化 gateway 內(nèi)部的監(jiān)聽,用于監(jiān)聽 worker 的連接已經(jīng)連接上發(fā)來的數(shù)據(jù)
$this->_innerTcpWorker = new Worker("GatewayProtocol://{$this->lanIp}:{$this->lanPort}");
$this->_innerTcpWorker->reusePort = false;
$this->_innerTcpWorker->listen();
$this->_innerTcpWorker->name = 'GatewayInnerWorker';