创建服务器

由于 fastd 天生支持 swoole,因此在结合上还是相对比较完善的。

fastd 内置 WebSocket、HTTP、TCP、UDP、Task 服务器,能够快速让开发者体验极速的性能。仅需一步:

配置文件(config/server.php):

  1. <?php
  2. return [
  3. 'host' => get_local_ip().':9999',
  4. 'class' => \Server\TaskServer::class,
  5. 'options' => [
  6. 'pid_file' => __DIR__ . '/../runtime/pid/' . app()->getName() . '.pid',
  7. 'log_file' => __DIR__ . '/../runtime/logs/' . app()->getName() . '.pid',
  8. 'log_level' => 5,
  9. 'worker_num' => 10,
  10. 'task_worker_num' => 20,
  11. ],
  12. 'processes' => [
  13. ],
  14. 'listeners' => [
  15. [
  16. 'host' => '0.0.0.0:9528',
  17. 'class' => \FastD\Servitization\Server\TCPServer::class
  18. ]
  19. ],
  20. ];

主要配置需要调整 class 选项。内置服务列表:

  • FastD\Servitization\Server\HTTPServer
  • FastD\Servitization\Server\TCPServer
  • FastD\Servitization\Server\UDPServer
  • FastD\Servitization\Server\WebSocketServer
  1. $ php bin/server start
  1. Server: dobee
  2. App version: 2.1.0
  3. Swoole version: 1.9.23
  4. Listen: http://0.0.0.0:9527
  5. > Listen: tcp://0.0.0.0:9528
  6. PID file: fastd-demo/config/../runtime/pid/dobee.pid, PID: 22074

启动后,对应访问: http://127.0.0.1:9527/ 即可得到对应结果。

结合进程

在我们开发完的进程,其实也可以无痕整合到 Server 中,仅需添加到配置文件中。

  1. <?php
  2. return [
  3. 'host' => '0.0.0.0:9527',
  4. 'class' => \Server\TaskServer::class,
  5. 'options' => [
  6. 'pid_file' => __DIR__ . '/../runtime/pid/' . app()->getName() . '.pid',
  7. 'log_file' => __DIR__ . '/../runtime/logs/' . app()->getName() . '.pid',
  8. 'log_level' => 5,
  9. 'worker_num' => 10,
  10. 'task_worker_num' => 20,
  11. ],
  12. 'processes' => [
  13. \Process\HelloProcess::class
  14. ],
  15. 'listeners' => [
  16. [
  17. 'host' => '0.0.0.0:9528',
  18. 'class' => \FastD\Servitization\Server\TCPServer::class
  19. ]
  20. ],
  21. ];
  1. $ php bin/server start
  1. Server: dobee
  2. App version: 2.1.0
  3. Swoole version: 1.9.23
  4. Listen: http://0.0.0.0:9527
  5. > Listen: tcp://0.0.0.0:9528
  6. PID file: /Users/janhuang/Documents/htdocs/me/fastd/fastd-demo/config/../runtime/pid/dobee.pid, PID: 22074
  7. 1
  8. 2
  9. 3

如果你需要将服务器守护进程化,需要添加 -d 选项。

  1. $ php bin/server start -d