hyperf| hyperf 源码解读 2: start

简介: 上篇我们跟着 `php bin/hyperf.php` 命令, 看到了框架的核心 `container`, 这篇我们跟着 `php bin/hyperf.php start`, 来会一会强大到爆炸的 `swoole`

上篇我们跟着 php bin/hyperf.php 命令, 看到了框架的核心 container, 这篇我们跟着 php bin/hyperf.php start, 来会一会强大到爆炸的 swoole

开始之前, 请确保自己具备一定的 swoole 基础知识, 这篇适合你:

千万不要小看了 基础知识, 与其一直卡壳浪费时间, 不如沉下心来好好读一下 swoole wiki, 看似 艰辛, 绝对比没有这些基础知识导致的时间浪费要划算得多得多得多 !

Command 基础

从上一篇的 blog 可知, hyperf 源码解读 1: 启动, php bin/hyperf.php 执行的命令, 是基于 Symfony\Component\Console\Application 提供的命令行应用提供的功能, 初始内置的 command, 是有 ConfigProvider 配置项中的 command 字段设置的. 那么, 我们将要执行的 start 命令, 是由哪个 command 提供的呢?

可以根据 gen:command 命令生成的 demo 代码, 了解到:

// \App\Command\TestCommand
public function __construct(ContainerInterface $container)
{
    $this->container = $container;

    // 命令的名字通常在这里配置
    parent::__construct('t');
}

对应的 start 命令, 就可以通过全局搜索 parent::__construct('start') 查询到

搜索是很重要的能力, 甚至可以上升到 搜商. 你并不需要特别熟悉一个事物, 但是你依旧有 能力 进行处理, 搜商就是这样的一个基础能力. 阅读源码也是提高搜商的一个有力方法.

OK, 就定位到了我们 start 命令对应的代码: \Hyperf\Server\Command\StartServer

StartServer 一览

/**
 * @Command
 */
class StartServer extends SymfonyCommand
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        parent::__construct('start');
        $this->container = $container;

        $this->setDescription('Start swoole server.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        \Swoole\Runtime::enableCoroutine(true);

        $this->checkEnvironment($output);

        $serverFactory = $this->container->get(ServerFactory::class)
            ->setEventDispatcher($this->container->get(EventDispatcherInterface::class))
            ->setLogger($this->container->get(StdoutLoggerInterface::class));

        $serverConfig = $this->container->get(ConfigInterface::class)->get('server', []);
        if (! $serverConfig) {
            throw new \InvalidArgumentException('At least one server should be defined.');
        }

        $serverFactory->configure($serverConfig);
        $serverFactory->start();
    }

    private function checkEnvironment(OutputInterface $output)
    {
        if (ini_get_all('swoole')['swoole.use_shortname']['local_value'] !== 'Off') {
            $output->writeln('<error>ERROR</error> Swoole short name have to disable before start server, please set swoole.use_shortname = \'Off\' into your php.ini.');
            exit(0);
        }
    }
}
  • @Command 注解, container 初始化的时候会有 scan 注解, 就能把这个类解析为 command 来使用
  • execute() 是 command 实际执行的方法, \Symfony\Component\Console\Command\Command 作为基类采用这个方法, 而 \Hyperf\Command\Command 作为基类, 则是使用 handle() 方法
  • $serverFactory = $this->container->get(ServerFactory::class): container 的经典使用场之一, 由 container 来解决类的初始化, 使用放直接使用即可

ServerFactory 细节

还是上面的代码:

$serverFactory = $this->container->get(ServerFactory::class)
  ->setEventDispatcher($this->container->get(EventDispatcherInterface::class))
  ->setLogger($this->container->get(StdoutLoggerInterface::class));

$serverConfig = $this->container->get(ConfigInterface::class)->get('server', []);
if (! $serverConfig) {
    throw new \InvalidArgumentException('At least one server should be defined.');
}

$serverFactory->configure($serverConfig);

$serverFactory->start();
  • 实例化 ServerFactory 时, 配置好框架的基础组件 Event(事件模块) / Logger(日志模块)
  • 从框架通的 Config(配置) 组件获取 server 配置, 即 config/autoload/server.php 文件的配置内容
  • server start, 配置好了 swoole server 后, 使用 Swoole\Server->start() 启动服务即可

server config 的实现细节

server config 的实现细节, 直接跟读代码定位到:

// \Hyperf\Server\Server::initServers
    protected function initServers(ServerConfig $config)
    {
        $servers = $this->sortServers($config->getServers());

        foreach ($servers as $server) {
            $name = $server->getName();
            $type = $server->getType();
            $host = $server->getHost();
            $port = $server->getPort();
            $sockType = $server->getSockType();
            $callbacks = $server->getCallbacks();
            ...
  • 先根据 swoole 的限制, 如果配置了多个 server, 需要先启动 http/ws server, 再通过 addPort() 的方式添加其余的 server, 这一步 hyperf 框架已经处理掉了
  • 绑定 swoole server 需要 callback(回调函数)
  • 触发 BeforeMainServerStart / BeforeServerStart 等事件

深入 server start

等等, server start 这么简单 ?! 再来回顾一下 php bin/hyperf.php start:

root@820d21e61cd8 /d/hyperf-demo# php bin/hyperf.php start
Scanning ...
Scan completed.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Di\Listener\BootApplicationListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\RpcClient\Listener\AddConsumerDefinitionListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\JsonRpc\Listener\RegisterProtocolListener listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Amqp\Listener\BeforeMainServerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#1 started.
[INFO] TaskWorker#2 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Process\Event\BeforeProcessHandle handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Process\Event\BeforeProcessHandle handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\MainWorkerStart handled by Hyperf\Amqp\Listener\MainWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[INFO] Process[queue.default.0] start.
[DEBUG] Event Hyperf\Process\Event\BeforeProcessHandle handled by Hyperf\Process\Listener\LogBeforeProcessStartListener listener.
[INFO] Worker#0 started.
[INFO] Process[TestConsumer-hyperf.1] start.
[DEBUG] Event Hyperf\Process\Event\BeforeProcessHandle handled by Hyperf\Process\Listener\LogBeforeProcessStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] HTTP Server listening at 0.0.0.0:9501
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[INFO] TaskWorker#3 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Process\Event\BeforeProcessHandle handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Process[TestConsumer-hyperf.0] start.
[DEBUG] Event Hyperf\Process\Event\BeforeProcessHandle handled by Hyperf\Process\Listener\LogBeforeProcessStartListener listener.

是的, 使用了 Event 机制带来的灵活性, swoole Process 就是这个过程中启动的, 而 hyperf 中很多组件底层都是使用的 swoole Process 实现的, 这里只给出调用链路, 感兴趣的小伙伴自己去瞧哦:

// 触发 BeforeMainServerStart 事件
// vendor/hyperf/server/src/Server.php:110
$this->eventDispatcher->dispatch(new BeforeMainServerStart($this->server, $config->toArray()));

// 事件监听器处理
\Hyperf\Process\Listener\BootProcessListener::process

// 启动 swoole process
\Hyperf\Process\AbstractProcess::bind

// 对应的 swoole 方法
\Swoole\Server::addProcess

写在最后

start 命令对源码阅读确实是一个相当有挑战的部分:

  • 对 swoole 方法的封装, 要发挥出 swoole 超强能力, 所以说 swoole 的基础知识很重要
  • hyperf 多个基础组件的联动, 包括 container / event / log / config

希望看到这里, 你可以感受到 hyperf 和 swoole 的强大之处, 头脑中大概能对 hyperf 运行的 生命周期 有个大致的了解

下篇我们开始协程的话题, 以及转到协程下编程必备的组件, 协程编程须知等内容.

目录
相关文章
|
前端开发
Threejs - 加载视频纹理渲染 实现一个3D视频播放器
Threejs - 加载视频纹理渲染 实现一个3D视频播放器
2982 0
Threejs - 加载视频纹理渲染 实现一个3D视频播放器
|
前端开发
websocket的心跳机制
websocket的心跳机制
2080 3
|
存储 人工智能 安全
函数计算进化之路:AI Sandbox 新基座
AI Agent Sandbox 是应对 AI 代理自主性风险的关键技术,提供安全隔离环境以执行代码、交互应用和处理敏感数据。它解决了三大挑战:隔离与安全、状态管理与成本、可扩展性与运维。阿里云函数计算凭借物理隔离架构、Serverless 弹性与成本优势,结合会话亲和、隔离及存储安全等创新能力,成为 AI Agent Sandbox 的理想运行时平台,助力 AI 技术安全落地与商业化发展。
|
移动开发 Rust JavaScript
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1510 4
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
12月前
|
存储 编解码 人工智能
阿里云视频点播VOD收费标准:存储费用、流量价格、转码费用及视频加速价格整理
阿里云视频点播VOD按存储、转码、流量及加速等计费,支持资源包与按量付费。含新客9.9元套餐,涵盖流量、转码、存储等多种优惠,详情见官方页面。
|
机器学习/深度学习 人工智能 缓存
这门技术太炸了!精通Coze工作流,我成了公司里的“稀缺人才”
在AI时代,掌握Coze工作流是职场跃迁的关键。本文详解如何通过可视化编排,将AI能力融入业务,实现从执行者到架构师的转变,成为企业不可或缺的“稀缺人才”。
|
12月前
|
Java 数据库 微服务
微服务服务注册与发现
本文介绍了微服务架构的演进与实践。针对单体架构在团队协作、发布效率、扩展性等方面的局限,微服务通过将系统拆分为多个独立部署、单一职责的小型服务,实现高内聚、低耦合,提升系统的可维护性与伸缩能力。结合Spring Cloud与Spring Cloud Alibaba技术栈,文章以黑马商城项目为例,演示了如何创建微服务工程、进行服务拆分,并使用RestTemplate实现服务间远程调用,帮助开发者掌握微服务核心开发技能。
微服务服务注册与发现
|
11月前
|
人工智能 应用服务中间件 API
🦞 Clawdbot(Moltbot)云服务AI助手,阿里云一键部署配置步骤流程
Moltbot(原Clawdbot)是一款开源的本地优先AI代理与自动化平台,能够将多渠道通信能力与大语言模型深度集成,帮助用户创建具备持久记忆和主动执行能力的定制化AI助理。为简化部署流程,阿里云推出了Moltbot全套云服务,支持一键快速部署,让AI助手实现24小时不间断运行,适用于个人开发者和企业团队构建智能化应用。本文将详细拆解部署全流程,确保不改变原意且无营销表述,帮助用户高效完成配置。
1586 3
|
PHP 数据安全/隐私保护
PHP基于 OpenSSL 实现国密 SM4 加解密
PHP基于 OpenSSL 实现国密 SM4 加解密
3232 0