With this article, I hope to make Swoole Amway available to more people. Swoole may be currently positioned as a number of high-end phper toys, so that the low and middle classes are terrified, and may be confused about some application scenarios, but it is not so difficult.

Swoole differs from our traditional PHP development, and there are concepts to understand. Using some current Swoole based framework development, from the development habit and the traditional TP, LV framework is similar.

So why use Swoole?

Yu Run believes that there are the following points:

  • It is resident in memory to avoid performance loss caused by repeated loading and improve massive performance

  • Coroutine asynchrony, improve the concurrent processing capability of I/ O-intensive scenarios (such as: wechat development, payment, login, etc.)

  • It is convenient to develop Http, WebSocket, TCP, UDP and other applications, and can communicate with hardware
  • PHP high-performance microservices architecture becomes a reality

Permanent memory


As shown in the figure below, this is a scenario where the same thread handles concurrent requests, such as when you need to call another API or read or write a large file in one of your interfaces. Traditional synchronous blocking and coroutine asynchro are the advantages.

Coroutines are implemented on a single-threaded basis, which maximizes CPU resources without wasting them waiting for I/O. Of course, the more coroutines you have, the more memory you use, but this is acceptable, and the usage is relatively small compared to processes and threads.

Examples of coroutine code:

<? php use Swoole\Coroutine as co; $time = microtime(true); For ($I = 0; $I = 0; $i < 10; $I + +) {/ / create coroutines go (function () use ($I) {co: : sleep (1.0); Echo $I, PHP_EOL; }); } swoole_event_wait(); echo 'co time:', microtime(true) - $time, ' s', PHP_EOL; $time = microtime(true); For ($I = 0; $I = 0; $i < 10; ++$i) { sleep(1); Echo $I, PHP_EOL; } echo 'sync time:', microtime(true) - $time, ' s', PHP_EOL;Copy the code

0 9 87 65 4 3 2 1 co time:1.0087130069733 s 01 2 3 4 5 6 7 8 9 Sync time: 10.010055065155sCopy the code

Creating an Http Service

It’s not as hard as you think. Look at the code:

$http = new swoole_http_server("", 9501);
$http->on('request', function ($request, $response) {
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
$http->start();Copy the code

Micro service

Tars is the TAF (Total Application Framework) of background logic layer that Tencent has been using since 2008. It currently supports C++,Java,PHP and Nodejs languages. The framework provides users with a set of solutions involving development, operation and maintenance, and testing, to help a product or service quickly develop, deploy, test, and launch. It integrates extensible protocol codec, high-performance RPC communication framework, name routing and discovery, publication monitoring, log statistics, and configuration management. Through it, we can quickly build our own stable and reliable distributed applications in the way of micro-service, and realize complete and effective service governance.

See: segmentfault.com/a/119000001…

If there is any mistake, you are welcome to point out, I sincerely want to recommend Swoole to everyone!