start.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/11/5
  6. * Time: 11:38 AM
  7. *
  8. * Service 启动脚本
  9. * 启动命令 php start.php --service=xxx & (XXX 表示对应的service目录名称) --daemonize=0(是否开启后台运行)
  10. * @Demo php start.php --service=Article --daemonize=0
  11. */
  12. use Mall\Framework\Core\Config;
  13. use Mall\Framework\Factory;
  14. // swoole 服务项目目录名称
  15. define('SWOOLESERVER_NAME', 'swoole_server');
  16. // 项目根目录
  17. define('PROJECT_PATH', __DIR__);
  18. require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Mall' . DIRECTORY_SEPARATOR . 'Bootstrap.php';
  19. $serviceName = Factory::request()->param('service');
  20. $serviceName = ucfirst($serviceName);
  21. $daemonize = intval(Factory::request()->param('daemonize'));
  22. var_dump($daemonize);
  23. $serviceConfigPath = __DIR__ . DIRECTORY_SEPARATOR . $serviceName . DIRECTORY_SEPARATOR . 'Config'.DIRECTORY_SEPARATOR.'Config.php';
  24. var_dump($serviceConfigPath);
  25. if (!file_exists($serviceConfigPath)) {
  26. exit($serviceConfigPath . 'config file is not found');
  27. }
  28. Config::load($serviceConfigPath);
  29. $service = Config::getInstance()->get('service');
  30. $buildSwooleConfig = [
  31. 'worker_num' => 2, // 建议开启核数的1-4倍
  32. 'daemonize' => $daemonize,
  33. 'open_length_check' => true, // 开启协议解析
  34. 'package_length_type' => 'N', // 长度字段的类型
  35. 'package_length_offset' => 0, //第几个字节是包长度的值
  36. 'package_body_offset' => PACKAGE_BODY_OFFSET, //第几个字节开始计算包内容
  37. 'package_max_length' => PACKAGE_MAXLENG, //协议最大长度
  38. ];
  39. if($service['ssl'] == true){
  40. $ssl = Config::getInstance()->get('ssl');
  41. $buildSwooleConfig = array_merge($buildSwooleConfig, $ssl);
  42. }
  43. if ($daemonize) {
  44. $dir = sys_get_temp_dir();
  45. $buildSwooleConfig['log_file'] = $dir . DIRECTORY_SEPARATOR . 'swoole_' . $serviceName . '.log';
  46. echo '控制台信息输出请查看:' . $buildSwooleConfig['log_file'] . "\n";
  47. }
  48. Factory::swoole($service, $service['type'])->setConfig($buildSwooleConfig)->begin();