1hyperf生命周期

1 篇文章 0 订阅
1 篇文章 0 订阅

Hyperf 的命令管理默认由

symfony/console 

提供支持 (如果您希望更换该组件您也可以通过改变 skeleton 的入口文件更换成您希望使用的组件), 在执行

php bin/hyperf.php start 

后,将由

Hyperf\Server\Command\StartServer 

命令类接管,并根据配置文件

config/autoload/server.php

内定义的 Server 逐个启动

其实最开始的文档已经写清楚,只是太着急没时间看,到了后来才发现 众里寻他千百度,蓦然回首,那人却在灯火阑珊处.

Hyperf\Server\Command\StartServer.php
public function __construct(ContainerInterface $container)
{
    $this->container = $container;
    parent::__construct('start');
    $this->setDescription('Start hyperf servers.');
}

这里是定义start的地方

protected function execute(InputInterface $input, OutputInterface $output)
{
    $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);

    Runtime::enableCoroutine(true, swoole_hook_flags());

    $serverFactory->start();

    return 0;
}

这里运行的时候可以看到 $output的内容

object(Symfony\Component\Console\Output\ConsoleOutput)#77168 (5) {
  ["stderr":"Symfony\Component\Console\Output\ConsoleOutput":private]=>
  object(Symfony\Component\Console\Output\StreamOutput)#76982 (3) {
    ["stream":"Symfony\Component\Console\Output\StreamOutput":private]=>
    resource(3636) of type (stream)
    ["verbosity":"Symfony\Component\Console\Output\Output":private]=>
    int(32)
    ["formatter":"Symfony\Component\Console\Output\Output":private]=>
    object(Symfony\Component\Console\Formatter\OutputFormatter)#77677 (3) {
      ["decorated":"Symfony\Component\Console\Formatter\OutputFormatter":private]=>
      bool(true)
      ["styles":"Symfony\Component\Console\Formatter\OutputFormatter":private]=>
      array(4) {
        ["error"]=>
        object(Symfony\Component\Console\Formatter\OutputFormatterStyle)#77063 (5) {
          ["foreground":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          array(2) {
            ["set"]=>
            int(37)
            ["unset"]=>
            int(39)
          }
          ["background":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          array(2) {
            ["set"]=>
            int(41)
            ["unset"]=>
            int(49)
          }
          ["href":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          NULL
          ["options":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          array(0) {
          }
          ["handlesHrefGracefully":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          NULL
        }
        ["info"]=>
        object(Symfony\Component\Console\Formatter\OutputFormatterStyle)#77102 (5) {
          ["foreground":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          array(2) {
            ["set"]=>
            int(32)
            ["unset"]=>
            int(39)
          }
          ["background":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          NULL
          ["href":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          NULL
          ["options":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          array(0) {
          }
          ["handlesHrefGracefully":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          NULL
        }
        ["comment"]=>
        object(Symfony\Component\Console\Formatter\OutputFormatterStyle)#77462 (5) {
          ["foreground":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          array(2) {
            ["set"]=>
            int(33)
            ["unset"]=>
            int(39)
          }
          ["background":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          NULL
          ["href":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          NULL
          ["options":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          array(0) {
          }
          ["handlesHrefGracefully":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
          NULL
        }

...

并且要求至少启动一个server,由配置文件 config/autoload/server.php 定义的server逐个启动,例如主程序启动使用9501,websocket启动使用9502,Grpc启动使用9503...这样服务依次启动,互不影响,方便使用

而且这个操作默认是在入口文件config/container.php里面实现了的,也就是上文讲到的重点

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值