Swoole、Php
git clone https://github.com/Watish/WatishWEBcomposer create-project watish/watishweb:dev-masterプロジェクトのエントリファイルはProject /bin/coserver.phpです
swoole-cli ./bin/CoServer.php php ./bin/CoServer.php
SRC/Controllerディレクトリで新しいクラスを作成します。ここでは、 HelloControllerとして定義します
<?php
namespace Watish WatishWEB Controller ;
use Watish Components Attribute Path ;
use Watish Components Struct Request ;
class HelloController
{
#[Path( ' / ' )]
public function index ( Request $ request ) : array
{
return [
" msg " => " hello world "
];
}
}保存したら、プロジェクトを開始し、http://127.0.0.1:9502/にアクセスして
{ "msg" : " hello world " }とても簡単ではありませんか?
従来のPHP-FPMフォームとは異なり、複数のプロセス間にメモリの分離があります。つまり、プロセスAで設定された可変プロセスBは取得できません。さらに、リクエストは分離されていません。つまり、同じプロセスに基づく2つの要求がありますが、異なるコルーチンの処理ロジックが処理されますが、両方のグローバル変数Aが変更された場合、グローバル変数は2回変更されます。
詳細については、Swooleドキュメント#criticalエラーのプログラミング手順を参照してください
上記の問題を効果的に回避するために、 watish components include contextを使用します
Contextは、単純なGetおよびSet Methodを提供するだけでなく、GlobalSet、Globalet、およびプロセス通信を通じてマルチワーカープロセスグローバル変数のその他の方法を提供する静的クラスです。
注:マルチワーカープロセスグローバル変数設定(コンテキスト:: Global_setなど)は、UnixSocketに基づいて非同期に実装されます。特定の瞬間にデータの一貫性を保証することはできません。ここでは、 Swoole Tableのカプセル化されたKVメモリテーブルであるWatish Components Utils Tableを使用できます。これは、各列のリソースを最大限に活用し、閉鎖のシリアル化をサポートできます。
ブラウザがサーバーにリクエストを送信すると、サーバーはハンドルメソッドを呼び出し、リクエストされたルートがルーティングスケジューラを介して存在するかどうかを判断します。ルーティングパラメーターが存在し、それらをwatish components struct requestにカプセル化し、グローバルミドルウェア - >ローカルミドルウェア - >コントローラーに渡す
ルーティングを登録する2つの方法
注:** register_route_auto **を**/config/server.php **で変更する必要があります
. . .
" register_route_auto " => true
...プレフィックスは、このクラスの下にあるルートのプレフィックスを定義するクラスの注釈です。
#[Prefix(string $ prefix )]パスは、ルーティングパスを定義するメソッドアノテーションです
#[Path(string $ path ,array $ methods )]栗を取る:
<?php
namespace Watish WatishWEB Controller ;
use Watish Components Attribute Middleware ;
use Watish Components Attribute Path ;
use Watish Components Attribute Prefix ;
use Watish Components Struct Request ;
use Watish WatishWEB Middleware TestMiddleware ;
#[Prefix( ' /hello ' )]
class HelloController
{
#[Path( ' /index ' )]
public function index ( Request $ request ) : array
{
return [
" msg " => " hello world "
];
}
#[Path( ' /user/{name} ' ,[ ' GET ' , ' POST ' ])]
#[Middleware([TestMiddleware::class])]
public function msg ( Request $ request ) : array
{
return [
" msg " => " hello " . $ request -> route ( ' name ' )
];
}
}上記のコードは次のようにルーティングされます
| パス | コントローラ | 方法 | ミドルウェア |
|---|---|---|---|
| /hello/index | hellocontroller@index | どれでも | なし |
| /hello/user/{name} | hellocontroller@msg | 入手、投稿してください | TestMiddleware |
ルーティング構成ファイルへのパスは次のとおりです。プロジェクト/config/route.php
上記の栗を再利用すると、上記のルーティング構成は次のようになります
<?php
use Watish Components Includes Route ;
use Watish WatishWEB Controller HelloController ;
function do_register_global_middleware ( Route $ route ): void
{
/**
$route->register_global_middleware(CorsMiddleware::class);
*/
}
function do_register_routes ( Route $ route ): void
{
$ route -> register ( ' /hello/index ' ,[HelloController::class, ' index ' ],[],[]);
$ route -> register ( ' /hello/user/{name} ' ,[HelloController::class, ' msg ' ],[TestMiddleware: class ],[ ' GET ' , ' POST ' ]);
}次のように、メソッド転送パラメーターを登録します
Watish Components Includes Route-> register (string $ path , array $ callback , array $ before_middlewares , array $ methods )注:ミドルウェアはミドルウェアインターフェイスを実装する必要があります
注釈で登録します
GlobalMiddlewareクラスの注釈を使用して、グローバルミドルウェア登録を実装できます
例えば:
<?php
namespace Watish WatishWEB Middleware ;
use Watish Components Attribute GlobalMiddleware ;
use Watish Components Struct Request ;
use Watish Components Struct Response ;
#[GlobalMiddleware]
class CorsMiddleware implements MiddlewareInterface
{
public function handle ( Request $ request , Response $ response ): void
{
$ response -> header ( " Access-Control-Allow-Origin " , " * " );
$ response -> header ( " Access-Control-Allow-Credentials " , true );
}
}ルート経由で登録します
構成ファイルパスは次のとおりです。プロジェクト/config/route.php
<?php
use Watish Components Includes Route ;
use Watish WatishWEB Controller HelloController ;
use Watish WatishWEB Middleware CorsMiddleware ;
function do_register_global_middleware ( Route $ route ): void
{
$ route -> register_global_middleware (CorsMiddleware::class);
}
function do_register_routes ( Route $ route ): void
{
$ route -> register ( ' /hello/index ' ,[HelloController::class, ' index ' ],[],[]);
$ route -> register ( ' /hello/user/{name} ' ,[HelloController::class, ' msg ' ],[],[ ' GET ' , ' POST ' ]);
}注釈で登録します
ミドルウェアを使用して、コントローラーまたはメソッドに注釈を付けることができます
#[Middleware(array $ middlewares )]最初にtestmiddlewareを作成します
<?php
namespace Watish WatishWEB Middleware ;
use Watish Components Struct Request ;
use Watish Components Struct Response ;
class TestMiddleware implements MiddlewareInterface
{
public function handle ( Request $ request , Response $ response )
{
$ response -> header ( " test " , " test " );
}
}次に、 HelloControllerを変更します
<?php
namespace Watish WatishWEB Controller ;
use Watish Components Attribute Middleware ;
use Watish Components Attribute Path ;
use Watish Components Attribute Prefix ;
use Watish Components Struct Request ;
use Watish WatishWEB Middleware TestMiddleware ;
#[Prefix( ' /hello ' )]
class HelloController
{
#[Path( ' /index ' )]
#[Middleware([TestMiddleware::class])]
public function index ( Request $ request ) : array
{
return [
" msg " => " hello world "
];
}
#[Path( ' /user/{name} ' ,[ ' GET ' , ' POST ' ])]
#[Middleware([TestMiddleware::class])]
public function msg ( Request $ request ) : array
{
return [
" msg " => " hello " . $ request -> route ( ' name ' )
];
}
}上記のように、インデックスメソッドとMSGメソッドにはローカルミドルウェアTestMiddlewareがあります
もちろん、上記のコードはこのように書くことができ、ミドルウェアアノテーションをHelloControllerに直接追加できます
<?php
namespace Watish WatishWEB Controller ;
use Watish Components Attribute Middleware ;
use Watish Components Attribute Path ;
use Watish Components Attribute Prefix ;
use Watish Components Struct Request ;
use Watish WatishWEB Middleware TestMiddleware ;
#[Prefix( ' /hello ' )]
#[Middleware([TestMiddleware::class])]
class HelloController
{
#[Path( ' /index ' )]
public function index ( Request $ request ) : array
{
return [
" msg " => " hello world "
];
}
#[Path( ' /user/{name} ' ,[ ' GET ' , ' POST ' ])]
public function msg ( Request $ request ) : array
{
return [
" msg " => " hello " . $ request -> route ( ' name ' )
];
}
}構成ファイルを介して登録します
ルーティングセクションレジスタパラメーターの構成ファイルルーティング登録方法を参照してください。詳細はこちらではありません
コントローラーはビジネスプロジェクト全体の中核であり、リクエストの処理、サービスの呼び出し、データの返信を担当します
シンプル、説明なし
依存関係の注入と一緒に、栗を教えてください:
<?php
namespace Watish WatishWEB Controller ;
use Watish Components Attribute Inject ;
use Watish Components Attribute Middleware ;
use Watish Components Attribute Path ;
use Watish Components Attribute Prefix ;
use Watish Components Struct Request ;
use Watish WatishWEB Middleware TestMiddleware ;
use Watish WatishWEB Service BaseService ;
#[Prefix( ' /hello ' )]
#[Middleware([TestMiddleware::class])]
class HelloController
{
#[Inject(BaseService::class)]
public BaseService $ baseService ;
#[Path( ' /index ' )]
public function index ( Request $ request ) : array
{
return [
" msg " => $ this -> baseService -> toArray ([ " Hello " , ' World ' ])
];
}
#[Path( ' /user/{name} ' ,[ ' GET ' , ' POST ' ])]
public function msg ( Request $ request ) : array
{
return [
" msg " => " hello " . $ request -> route ( ' name ' )
];
}
}注:建設方法の注入は当面はサポートされておらず、将来改善されます(穴を掘る)
直接郵便番号
<?php
namespace Watish WatishWEB Service ;
use Watish Components Attribute Async ;
use Watish Components Attribute Inject ;
use Watish Components Utils Logger ;
class TestService
{
#[Inject(BaseService::class)]
public BaseService $ baseService ;
#[Async]
public function asyncHello (): void
{
Logger:: info ( " Hello " );
}
public function hello ( string $ name ) : string
{
return " hello { $ name }" ;
}
}使用中は、依存関係の注入を引き続き実行できます。さらに、この方法は、非同期メソッドにするために、アナック注釈付け(非同期によって注釈された方法は無効なタイプでなければならないことに注意してください)を使用できます。
コマンドクラスファイルはProject/src/command/に保存されます/
注:コマンドクラスには、CommandInterfaceの実装インターフェイスが必要です
コマンドクラスは、注釈を使用してコマンドのみを登録できます
サンプルコードは次のとおりです。
<?php
namespace Watish WatishWEB Command ;
use Watish Components Attribute Command ;
use Watish Components Utils Logger ;
#[Command( " hello " , " command " )]
class HelloCommand implements CommandInterface
{
public function handle (): void
{
Logger:: info ( " Hello " );
}
}上記のコードは次の方法で実行できます
Swoole-Cli
swoole-cli ./bin/CoServer.php command:helloPhp
php ./bin/CoServer.php command:hello
コマンドの使用の注釈
Command (string $ command , string $ prefix = " command " )タスククラスはProject/SRC/TASK/に保存されます/
注:すべてのタスククラスは、TaskInterfaceを実装する必要があります
タスククラスは、 Crontabアノテーションを使用して、時限タスクの登録のみをサポートしています。
サンプルコードは次のとおりです。
<?php
namespace Watish WatishWEB Task ;
use Watish Components Attribute Crontab ;
use Watish Components Utils Logger ;
#[Crontab( " * * * * * " )]
class HelloTask implements TaskInterface
{
public function execute (): void
{
Logger:: info ( " Hello " , " HelloTask " );
}
}これは毎秒helloを出力する時限タスクです
Crontab注釈の使用方法
Crontab (string $ rule )ここで、ルールは標準的なクロンタブ式です
注:当面はmysqlだけがあります、Redis(自分で追加できます)
このフレームワークは、接続プールを使用してMySQLおよびRedis接続を維持し、スタートアップの開始時に接続プールの作成を完了します。これで、ビジネスロジックで使用するだけです。
** watish components include database :: mysql()** laravelクエリコンストラクターのカプセル化を返します(主に基礎となるPDOロジックの変更、通常の使用に違いはありません)
watish components include database :: Redis()は、 Predisクライアントを返します
最初にデータベースを構成してください!構成ファイル: project/config/database.php
フレームワークは次のコンポーネントを使用し、いくつかのコンポーネントをカプセル化します
Watish Components Constructor NameSpaceの下で、いくつかのコンポーネントの迅速な構築が提供されます
asynctaskconstructor :: make()非同期タスク配信
localfilesystemconstructor :: getFilesystem()ローカルファイルシステムコンストラクター
validatorconstructor :: make(array $ data、array $ rules) validator constructor
優れたコンポーネント開発者に感謝します
テスト環境:ubuntu22.0.4 lts
テストハードウェア:Virtual Machine(VirtualBox)6C6T、8192M、仮想化サポートを有効にする
テストツール:Apachebench
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Server Software: swoole-http-server
Server Hostname: 127.0.0.1
Server Port: 9502
Document Path: /hello/user/test
Document Length: 20 bytes
Concurrency Level: 3000
Time taken for tests: 2.040 seconds
Complete requests: 30000
Failed requests: 0
Total transferred: 7680000 bytes
HTML transferred: 600000 bytes
Requests per second: 14708.19 [#/sec] (mean)
Time per request: 203.968 [ms] (mean)
Time per request: 0.068 [ms] (mean, across all concurrent requests)
Transfer rate: 3677.05 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 83 17.3 85 125
Processing: 32 109 41.6 102 380
Waiting: 0 79 40.0 71 362
Total: 107 193 37.8 189 457
Percentage of the requests served within a certain time (ms)
50% 189
66% 200
75% 205
80% 208
90% 224
95% 236
98% 344
99% 389
100% 457 (longest request)
注:パフォーマンスにあまり注意を払わないでください。実際のビジネスロジックはしばしば複雑です。デモの圧力テストは何も示すことができません(写真)
使いやすい場合は、星をクリックできます。ご質問がある場合は、問題に言及してください。著者は積極的にそれを維持します。
2022-12-28 16:01に更新
Composerインストールを最初に実行してください