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 組件是在原有 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)getArgsMap()
和 getClassName()
方法 (c47e785)srun()
函數,用於協程調度 (3c4a6a4)onStart
/ onWorkStart
/ onWorkStop
/ onShutdown
),事件自帶支持協程 (a8d5a8d)call
方法, 用於使用同一連接操作(92456987)修復(Fixed):
Setter
更新欄位值之後update
更新無效(caadf0e)json
操作無效(92456987)更新(Update):
disabledModules
來禁用 ws 模塊(fa31111d)擴展(Extra):
新增(New)
升級注意:
- 請去掉
bin/swoft
里的Runtime::enanbleCoroutine()
設置- 請確保 swoole 的
swoole.use_shortname
的值為On
[admin
]