歡迎您光臨本站 註冊首頁

Swoft 2.0.4 發布,進程、TCP 組件

←手機掃碼閱讀     admin @ 2019-07-25 , reply:0

什麼是 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)

升級注意:

  1. 請去掉 bin/swoft 里的 Runtime::enanbleCoroutine() 設置
  2. 請確保 swoole 的 swoole.use_shortname 的值為 On

資源


[admin ]

來源:OsChina
連結:https://www.oschina.net/news/108521/swoft-2-0-4-released
Swoft 2.0.4 發布,進程、TCP 組件已經有295次圍觀

http://coctec.com/news/all/show-post-210772.html