What is Swoft?
Swoft It's based on Swoole Extend the implementation of the PHP Microsoft Services Consortium Framework. Like Go, Swoft has built-in protocol network servers and commonly used protocol clients and resides in memory, independent of traditional PHP-FPM. There are cooperative operation modes similar to Go language, flexible annotations like Spring Cloud framework, powerful global dependency injection container, perfect service governance, flexible and powerful AOP, standard PSR specification implementation and so on.
Swoft has built Swoft into a Spring Cloud in the PHP world through three years of accumulation and direction exploration. It is the best choice for PHP high performance framework and microservice governance.
process
The process component includes the following three functions:
- Process operation
- User processes
- Process pool
User processes
Some service scenarios such as Http/RPC/Websocket/TCP require a background running process to monitor, report or other special operations. At this time, a user-defined work process can be added to the corresponding service when it starts. Custom user process starts with service and exits with service closure. If the custom user process is accidentally closed, the service will restart a new custom user process to ensure that the custom user process always exists.
/** * 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); } } }
Process pool
Process pools are commonly used in scenarios where programs are required to run all the time, such as queue consumption and data computation. Swoft framework, based on the Woole process pool model encapsulated again, to facilitate developers to use the process pool quickly and simply.
To configure
return [ 'processPool' => [ 'class' => ProcessPool::class, 'workerNum' => 3 ] ];
Process definition
/** * 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); } } }
command
$ 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 component is based on the original swoole server and encapsulates and refines the use of functions.
To configure
'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, ],
Controller
/** * 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'); } }
command
$ 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
Update content
Enhancement:
- Swoft Http Message Request Add getHeaderLines()( 74a2a91)
- Aop adds getArgsMap() and getClassName() methods( c47e785)
- Added srun() function for cooperative scheduling( 3c4a6a4)
- Optimize server events (onStart / onWorkStart / onWorkStop / onShutdown), with event support protocols( a8d5a8d)
- New Delivery Synchronized Blocking Tasks( ec938e5)
- Added Redis call method to use the same connection operation( 92456987)
- Compatible with Swoole 4.4.x
Fixed:
- Repair migration class name is too long, resulting in incomplete record class name( 58314b8)
- Update update is invalid after fixing entity queries and updating field values with Setter( caadf0e)
- Return error of deleting pid file after fixing stop, resulting in restart failure( 2be450bf11)
- Fixed the problem of i18n setting default language not working( b401a504e)
- Fixed ws server with multiple workers, unable to actively shut down other worker connections( 271b6398)
- Fixed content type mismatch when http server received xml request( 2ff9a4e61)
- Repair using Database, json operation is invalid( 92456987)
- Repair limiter Speed limiter Redis with Prefix Unusable( 7b54d4c)
Update:
- Updating ws server can disable ws modules by configuring disabled modules( fa31111d)
Extra:
- Increase case display on official website, welcome to submit cases to the official case warehouse swoft-cloud/swoft-case
- Modifications to documents on GitHub will be automatically updated to official documents and no manual refreshment is required.
New
Upgrade Notes:
- Remove the Runtime::enanbleCoroutine() setting in bin/swoft
- Make sure that the swoole.use_shortname of swoole is On
Resources
- Gitee: https://gitee.com/swoft/swoft
- GitHub: https://github.com/swoft-cloud/swoft
- Official website: https://www.swoft.org
- File: https://www.swoft.org/docs