天天看点

1hyperf生命周期

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里面实现了的,也就是上文讲到的重点