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)
升级注意:
- 请去掉
bin/swoft
里的Runtime::enanbleCoroutine()
设置 - 请确保 swoole 的
swoole.use_shortname
的值为On
资源
- Gitee: https://gitee.com/swoft/swoft
- GitHub: https://github.com/swoft-cloud/swoft
- 官网:https://www.swoft.org
- 文档:https://www.swoft.org/docs
原文地址:https://segmentfault.com/a/1190000019867577
相关推荐
-
比较CGI,FastCGI,PHP-CGI与PHP-FPM的区别 php基础
2019-8-26
-
让子弹飞~利用OPcache扩展提升PHP7性能 | laravel篇 php基础
2019-9-5
-
Redis 高并发,给你分享前百度技术总监教了我些什么 php基础
2019-9-6
-
PHP标准库 (SPL) 笔记 php基础
2019-9-15
-
Mysql – JOIN详解 php基础
2019-9-7
-
用 PHP 花两小时自制脚本语言 php基础
2019-2-23
-
EventDispatcher 事件分发组件 php基础
2019-5-13
-
使用过Redis,我竟然还不知道Rdb php基础
2019-5-18
-
🚀 Hyperf v1.0 发布,全新企业级的 PHP 协程框架 php基础
2019-6-22
-
图解HTTP缓存 php基础
2019-9-9