企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 服务器 官方开发的 HTTP 服务器,用于执行 "HTTP 服务",基于 Swoole 扩展的 `Coroutine\Http\Server` 封装,拥有比 Apache/PHP-FPM 更高的性能。 ## 启动 启动服务器: - web ~~~ php bin/mix.php web:start ~~~ - api ~~~ php bin/mix.php api:start ~~~ 以上命令的各部分拆解如下: - `bin/mix.php` 为入口文件 - `web:start` 为命令 查看帮助: ~~~ php bin/mix.php web:start --help ~~~ `web:start` 包含以下可选参数: - `-d, --daemon` 程序在后台执行 - `-h, --host` 指定服务器 IP - `-p, --port` 指定服务器端口 - `-r, --reuse-port ` 端口复用,用于多开利用多核 ## 入口代码 从 `manifest.php` 的 `commands` 或者 `commandPath` 配置我们能看到 `web:start` 命令启动的是: [>> \App\Web\Commands\StartCommand::class <<](https://github.com/mix-php/mix-skeleton/tree/v2.1/app/Web/Commands/StartCommand.php) >[success] 服务器代码都写在 StartCommand::class 中,用户能最大粒度的修改每一处服务器的执行细节 - `$server->start($this->route);` 传入了路由对象,使用 [Router.php#L210](https://github.com/mix-php/route/blob/master/src/Router.php#L207) 处理 http 请求, - 通过 `match` 方法匹配路由组件定义的控制器与中间件。 - 通过 MiddlewareDispatcher::class 执行匹配的中间件。 - 最后执行控制器 callback,并使用 `$response->end()` 发送至客户端。 ## 命令管理 与 mix 之前版本不同的是,新版只提供了 start 命令,这是因为新版 mix 是单进程协程框架,因为是单进程所以可以直接通过 kill 停止执行,停止是平滑安全的这一点能在 StartCommand::class 的源码中看到实现逻辑: ``` // 查找名为 mix 的进程 $> ps -ef | grep mix // kill指定pid $> kill [PID] ``` ## 热更新 (仅限开发阶段使用) 为了提升开发效率,我们提供了 [https://github.com/mix-php/swoolefor](https://github.com/mix-php/swoolefor) 工具,能监控文件系统变化,通过设置的命令自动重启服务器,可用于修改代码后自动重启各种 Swoole 常驻服务器 (仅限开发阶段使用)