start.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. $serviceConfigPath = __DIR__ . DIRECTORY_SEPARATOR . $serviceName . DIRECTORY_SEPARATOR . 'Config'.DIRECTORY_SEPARATOR.'Config.php';
  23. if (!file_exists($serviceConfigPath)) {
  24. exit($serviceConfigPath . 'config file is not found');
  25. }
  26. Config::load($serviceConfigPath);
  27. $service = Config::getInstance()->get('service');
  28. $buildSwooleConfig = [
  29. 'worker_num' => 2, // 建议开启核数的1-4倍
  30. 'daemonize' => $daemonize,
  31. 'open_length_check' => true, // 开启协议解析
  32. 'package_length_type' => 'N', // 长度字段的类型
  33. 'package_length_offset' => 0, //第几个字节是包长度的值
  34. 'package_body_offset' => PACKAGE_BODY_OFFSET, //第几个字节开始计算包内容
  35. 'package_max_length' => PACKAGE_MAXLENG, //协议最大长度
  36. ];
  37. if($service['ssl'] == true){
  38. $ssl = Config::getInstance()->get('ssl');
  39. $buildSwooleConfig = array_merge($buildSwooleConfig, $ssl);
  40. }
  41. if ($daemonize) {
  42. $dir = sys_get_temp_dir();
  43. $buildSwooleConfig['log_file'] = $dir . DIRECTORY_SEPARATOR . 'swoole_' . $serviceName . '.log';
  44. echo '控制台信息输出请查看:' . $buildSwooleConfig['log_file'] . "\n";
  45. }
  46. Factory::swoole($service, $service['type'])->setConfig($buildSwooleConfig)->begin();