thinkphp版本:5.1
tp5.1運行命令行php think worker:gateway
出現GatewayWorker Not Support On Windows.
是因為在tp5.1的命令行中做了判定,不支持windows環境下運行。
這里不支持windows環境并不是說gateway worker不支持windows,而是tp5.1的命令行運行方式不支持windows,原因是(在官方文檔中有說明):
windows操作系統下無法在一個php文件里初始化多個Worker
tp5.1的命令php think worker:gateway
就是在同一個php文件里啟動了多個worker,因為不能成功運行所以tp5.1的命令行提前做了判定,顯示不支持在windows下運行。
按照官方文檔的說明,只要分開啟動就好了。
解決方案:
最簡單一點的:按照官方文檔,建立多個文件分開運行worker。
高大上一點的(瞎折騰):自己寫tp的命令行,實質還是將多個worker放在不同的文件中運行。
附:自定義TP命令行
命令行文件application\common\command\Workerman.php
<?phpnamespace app\common\command;use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;
use Workerman\Worker;/*** Worker 命令行*/
class Workerman extends Command
{protected function configure(){$this->setName('workerman')->addArgument('service', Argument::OPTIONAL, 'workerman service: gateway|register|businessworker', null)->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server', null)->addOption('port', 'P', Option::VALUE_OPTIONAL, 'the port of workerman server', null)->addOption('daemon', 'd', Option::VALUE_OPTIONAL, 'Run the workerman server in daemon mode.')->setDescription('workerman Server for ThinkPHP');}public function execute(Input $input, Output $output){$service = $input->getArgument('service');$option = Config::pull('gateway_worker');if ($input->hasOption('host')) {$host = $input->getOption('host');} else {$host = !empty($option['host']) ? $option['host'] : '0.0.0.0';}if ($input->hasOption('port')) {$port = $input->getOption('port');} else {$port = !empty($option['port']) ? $option['port'] : '2347';}$registerAddress = !empty($option['registerAddress']) ? $option['registerAddress'] : '127.0.0.1:1236';switch ($service) {case 'register':$this->register($registerAddress);break;case 'businessworker':$this->businessWorker($registerAddress, isset($option['businessWorker']) ? $option['businessWorker'] : []);break;case 'gateway':$this->gateway($registerAddress, $host, $port, $option);break;default:$output->writeln("<error>Invalid argument action:{$service}, Expected gateway|register|businessworker .</error>");exit(1);break;}Worker::runAll();}/*** 啟動register* @access public* @param string $registerAddress* @return void*/public function register($registerAddress){// 初始化registernew Register('text://' . $registerAddress);}/*** 啟動businessWorker* @access public* @param string $registerAddress registerAddress* @param array $option 參數* @return void*/public function businessWorker($registerAddress, $option = []){// 初始化 bussinessWorker 進程$worker = new BusinessWorker();$this->option($worker, $option);$worker->registerAddress = $registerAddress;}/*** 啟動gateway* @access public* @param string $registerAddress registerAddress* @param string $host 服務地址* @param integer $port 監聽端口* @param array $option 參數* @return void*/public function gateway($registerAddress, $host, $port, $option = []){// 初始化 gateway 進程if (!empty($option['socket'])) {$socket = $option['socket'];unset($option['socket']);} else {$protocol = !empty($option['protocol']) ? $option['protocol'] : 'websocket';$socket = $protocol . '://' . $host . ':' . $port;unset($option['host'], $option['port'], $option['protocol']);}$gateway = new Gateway($socket, isset($option['context']) ? $option['context'] : []);// 以下設置參數都可以在配置文件中重新定義覆蓋$gateway->name = 'Gateway';$gateway->count = 4;$gateway->lanIp = '127.0.0.1';$gateway->startPort = 2000;$gateway->pingInterval = 30;$gateway->pingNotResponseLimit = 0;$gateway->pingData = '{"type":"ping"}';$gateway->registerAddress = $registerAddress;// 全局靜態屬性設置foreach ($option as $name => $val) {if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) {Worker::${$name} = $val;unset($option[$name]);}}$this->option($gateway, $option);}/*** 設置參數* @access protected* @param Worker $worker Worker對象* @param array $option 參數* @return void*/protected function option($worker, array $option = []){// 設置參數if (!empty($option)) {foreach ($option as $key => $val) {$worker->$key = $val;}}}
}
這個文件放哪里都無所謂,只要和對應的command參數配置對應就好了
在application\command.php
命令行參數配置文件中添加
return ['workerman' => '\\app\\common\\command\\Workerman',
];
運行方式: php think workerman gateway|register|businessworker
這里是是直接修改了官方的php think worker:gateway
命令,因為windows不支持reload等啟動方式,所以取消了action
的啟動參數,改為了服務名稱service
,例如啟動 worker register服務php think workerman register
,所以在這里得開3個命令窗口分別運行 register、businessworker、gateway