皇上,还记得我吗?我就是1999年那个Linux伊甸园啊-----24小时滚动更新开源资讯,全年无休!

Swoft 2.0.4 发布,进程、TCP 组件

什么是 Swoft ?

Swoft 是一款基于 Swoole 扩展实现的 PHP 微服务协程框架。Swoft 能像 Go 一样,内置协程网络服务器及常用的协程客户端且常驻内存,不依赖传统的 PHP-FPM。有类似 Go 语言的协程操作方式,有类似 Spring Cloud 框架灵活的注解、强大的全局依赖注入容器、完善的服务治理、灵活强大的 AOP、标准的 PSR 规范实现等等。

Swoft 通过长达三年的积累和方向的探索,把 Swoft 打造成 PHP 界的 Spring Cloud, 它是 PHP 高性能框架和微服务治理的最佳选择。

进程

进程组件包括如下三部分功能:

  • 进程操作
  • 用户进程
  • 进程池

用户进程

Http/RPC/Websocket/TCP 等服务有些业务场景,需要一个后台运行进程去监控、上报或者其它特殊操作,此时可以在相应服务启动的时候,添加一个用户自定义工作进程,来实现。 自定义用户进程与服务一起启动,服务关闭一起退出,如果自定义用户进程被意外关闭,服务会重新启动一个新的自定义用户进程,保证自定义用户进程一直存在。

/**
 * Class MonitorProcess
 *
 * @since 2.0
 *
 * @Bean()
 */
class MonitorProcess extends UserProcess
{
    /**
     * @param Process $process
     *
     * @throws DbException
     */
    public function run(Process $process): void
    {
        $process->name('swoft-monitor');

        while (true) {
            $connections = context()->getServer()->getSwooleServer()->connections;
            CLog::info('monitor = ' . json_encode($connections));

            // Database
            $user = User::find(1)->toArray();
            CLog::info('user='.json_encode($user));

            // Redis
            Redis::set('test', 'ok');
            CLog::info('test='.Redis::get('test'));

            Coroutine::sleep(3);
        }
    }
}

进程池

进程池一般用于需要程序一直运行的场景,比如队列消费,数据计算。Swoft 框架中,基于 Swoole 进程池模型再次封装,便于开发者快速简单的使用进程池。

配置

return [
    'processPool' => [
        'class' => ProcessPool::class,
        'workerNum' => 3
    ]
];

进程定义

/**
 * Class Worker1Process
 *
 * @since 2.0
 *
 * @Process()
 */
class Worker1Process implements ProcessInterface
{
    /**
     * @param Pool $pool
     * @param int  $workerId
     */
    public function run(Pool $pool, int $workerId): void
    {
        while (true) {
            CLog::info('worker-' . $workerId);

            Coroutine::sleep(3);
        }
    }
}

命令

$ php bin/swoft process

Group: process
Usage:
  bin/swoft process:COMMAND [--opt ...] [arg ...]

Global Options:
      --debug      Setting the application runtime debug level(0 - 4)
      --no-color   Disable color/ANSI for message output
  -h, --help       Display this help message
  -V, --version    Show application version information

Commands:
  reload        No description message
  restart       No description message
  start         No description message
  stop          No description message

Example:
 bin/swoft process:start     Start the process pool
 bin/swoft process:stop      Stop the process pool

View the specified command, please use: bin/swoft process:COMMAND -h

TCP

TCP 组件是在原有 swoole server的基础上,封装并细化功能使用

配置

 'tcpServer'   => [
    'class'   => TcpServer::class,
    'port' => 18309,
    'debug' => env('SWOFT_DEBUG', 0),
    /* @see TcpServer::$setting */
    'setting' => [
        'log_file' => alias('@runtime/swoole.log'),
    ],
],
/** @see \Swoft\Tcp\Protocol */
'tcpServerProtocol' => [
    'type'            => \Swoft\Tcp\Packer\SimpleTokenPacker::TYPE,
    // 'openEofCheck'    => true, // Defalut use EOF check
    // 'openLengthCheck' => true,
],

控制器

/**
 * Class DemoController
 *
 * @TcpController()
 */
class DemoController
{
    /**
     * @TcpMapping("list", root=true)
     * @param Response $response
     */
    public function list(Response $response): void
    {
        $response->setData('[list]allow command: list, echo, demo.echo');
    }
}

命令

$ php bin/swoft tcp
Description:
  There some commands for manage the tcp server

Usage:
  tcp:{command} [arguments] [options]

Commands:
  start    Start the tcp server
  stop     Stop the running server
  restart  Restart the running server

Options:
  -h, --help  Show help of the command group or specified command action

更新内容

增强(Enhancement):

  • Swoft\Http\Message\Request 新增 getHeaderLines() (74a2a91)
  • Aop 新增 getArgsMap() 和 getClassName() 方法 (c47e785)
  • 新增 srun() 函数,用于协程调度 (3c4a6a4)
  • 优化 server 事件(onStart / onWorkStart / onWorkStop / onShutdown),事件自带支持协程 (a8d5a8d)
  • 新增投递同步阻塞任务(ec938e5)
  • 新增 Redis call 方法, 用于使用同一连接操作(92456987)
  • 兼容 Swoole 4.4.x

修复(Fixed):

  • 修复 迁移类名太长导致记录类名不全(58314b8)
  • 修复 实体查询之后使用Setter更新字段值之后update更新无效(caadf0e)
  • 修复 stop 后删除pid文件的结果返回错误,导致restart失败 (2be450bf11)
  • 修复 i18n 设置默认语言不生效的问题 (b401a504e)
  • 修复 ws server在有多个worker时,无法主动关闭其他worker的连接(271b6398)
  • 修复 http server接收xml请求时,content type 不能正确匹配(2ff9a4e61)
  • 修复 使用 Database, json 操作无效(92456987)
  • 修复 limiter 限速器 Redis 加前缀无法使用问题(7b54d4c)

更新(Update):

  • 更新 ws server 可以通过配置 disabledModules 来禁用 ws 模块(fa31111d)

扩展(Extra):

  • 在官网增加案例展示,欢迎大家提交案例到官方案例仓库 swoft-cloud/swoft-case
  • 在GitHub上对文档的修改,将会自动更新到官网文档,不再需要手动刷新

新增(New)

升级注意:

  1. 请去掉 bin/swoft 里的 Runtime::enanbleCoroutine() 设置
  2. 请确保 swoole 的 swoole.use_shortname 的值为 On

资源

转自 https://www.oschina.net/news/108521/swoft-2-0-4-released