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 组件是在原有 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)getArgsMap()
和 getClassName()
方法 (c47e785)srun()
函数,用于协程调度 (3c4a6a4)onStart
/ onWorkStart
/ onWorkStop
/ onShutdown
),事件自带支持协程 (a8d5a8d)call
方法, 用于使用同一链接操做(92456987)修复(Fixed):网络
Setter
更新字段值以后update
更新无效(caadf0e)json
操做无效(92456987)更新(Update):app
disabledModules
来禁用 ws 模块(fa31111d)扩展(Extra):框架
新增(New)
升级注意:
bin/swoft
里的 Runtime::enanbleCoroutine()
设置swoole.use_shortname
的值为 On