Swoft 2.0.4 发布,进程、TCP 组件

什么是 Swoft ?

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

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

进程

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

  • 进程操做
  • 用户进程
  • 进程池

用户进程

Http/RPC/Websocket/TCP 等服务有些业务场景,须要一个后台运行进程去监控、上报或者其它特殊操做,此时能够在相应服务启动的时候,添加一个用户自定义工做进程,来实现。 自定义用户进程与服务一块儿启动,服务关闭一块儿退出,若是自定义用户进程被意外关闭,服务会从新启动一个新的自定义用户进程,保证自定义用户进程一直存在。json

/** * 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 进程池模型再次封装,便于开发者快速简单的使用进程池。bash

配置

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):swoole

  • 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):app

  • 更新 ws server 能够经过配置 disabledModules 来禁用 ws 模块(fa31111d)

扩展(Extra):框架

  • 在官网增长案例展现,欢迎你们提交案例到官方案例仓库 swoft-cloud/swoft-case
  • 在GitHub上对文档的修改,将会自动更新到官网文档,再也不须要手动刷新

新增(New)

升级注意:

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

资源

相关文章
相关标签/搜索