server.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. use Hyperf\Server\Event;
  12. use Hyperf\Server\Server;
  13. use Swoole\Constant;
  14. return [
  15. 'mode' => SWOOLE_PROCESS,
  16. 'servers' => [
  17. [
  18. 'name' => 'http',
  19. 'type' => Server::SERVER_HTTP,
  20. 'host' => '0.0.0.0',
  21. 'port' => 9501,
  22. 'sock_type' => SWOOLE_SOCK_TCP,
  23. 'callbacks' => [
  24. Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
  25. Event::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
  26. Event::ON_FINISH => [\Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'], // 确保有这一行配置
  27. ],
  28. 'options' => [
  29. // Whether to enable request lifecycle event
  30. 'enable_request_lifecycle' => false,
  31. ],
  32. ],
  33. ],
  34. 'settings' => [
  35. Constant::OPTION_ENABLE_COROUTINE => true,
  36. Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
  37. Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
  38. Constant::OPTION_OPEN_TCP_NODELAY => true,
  39. Constant::OPTION_MAX_COROUTINE => 100000,
  40. Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
  41. Constant::OPTION_MAX_REQUEST => 100000,
  42. Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
  43. Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
  44. 'task_worker_num' => 8, // 适当增加任务工作进程数量,可根据实际情况调整
  45. 'document_root' => BASE_PATH . '/public',
  46. 'enable_static_handler' => true,
  47. // 'task_max_request_time' => 120, // 延长任务超时时间,可根据实际情况调整
  48. ],
  49. 'callbacks' => [
  50. Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
  51. Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
  52. Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
  53. ],
  54. ];