什么是 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
TCPTCP 组件是在原有 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):
修复(Fixed):
更新(Update):
扩展(Extra):
新增(New)
资源(责任编辑:IT) |
