swoole operating mode to accelerate the application of detailed laravel

The contents of this article is to bring about swoole operating mode to accelerate laravel applications in detail, there is some reference value, a friend in need can refer to, I hope for your help.

 

A, Swoole

Swoole known redefined PHP, it is a PHP extension that PHP may use asynchronous execution, just the same node, but also the use of socket, offers a range of asynchronous IO as PHP, event-driven, parallel data structure and function.
Swoole4 supports full coroutine programming mode, the synchronization code can be used completely asynchronous program. PHP code without any additional keyword, automatically scheduling the underlying coroutine, asynchronous IO.
nodejs achievable swoole almost always realized, and higher performance than nodejs, after all, is a single-threaded nodejs not full cpu performance, swoole is multithreaded to take full advantage of cpu.
There Swoole efficient with traditional web development What is the difference, in addition to the traditional LAMP / LNMP synchronous development model, swoole asynchronous development model is how, how to stay productive?

Second, the traditional web development model
PHP web development method uses a LAMP / LNMP architecture, namely Linux, Nginx, Mysql and PHP. In nginx exemplified herein, as the general structure:

 

 

When a request comes in, web server forwards the request to the PHP-FPM, PHP-FPM is a FastCGI process pool service architecture, built PHP interpreter. FPM responsible interpreted PHP file generates a response, and ultimately returned to the web server, showing to the front. PHP file to achieve a number of business logic, including access to Mysql and Nosql, call the third-party applications and so on.
Such a structure php-fpm and nginx fit has been running well enough, but because php-fpm itself is a synchronous blocking the process model, the release of all resources (including the initial creation of a series of objects frame) at the end of the request, the process results in PHP "idle" (created <-> destroy <-> create) consume a large amount of CPU resources, resulting in limited throughput capacity stand-alone.

Every request processing process means that a PHP file parsing, environmental settings and other unnecessary time-consuming operation that is processed to destroy PHP processes can not use connection pooling technology to achieve performance optimization program in PHP.

Three, Swoole operating mode
for the problems of traditional architecture, swoole from PHP extensions to solve the above problem, for swoole process model, we have to understand before.

Compared to the traditional architecture, the biggest feature Swoole process model lies in its multi-threaded Reactor mode processing network requests, so that it can easily cope with a large number of connections.

Addition benefits include:

Full non-blocking asynchronous, small footprint cost, high efficiency of program execution

Running PHP file parsing only loaded once, to avoid repeated loading each request

Permanent process so as to achieve information transmission between the request and the possible connection pool

Fourth, why should run Laravel on Swoole?
Laravel framework to start when you need to load many files, coupled with its name out of the ecological environment, so in the development process, we will find that there are a lot of good has been made wheels , which also makes the first start of Laravel particularly high disk IO (the file is to load a lot of thing)
Laravel life cycle need every request is executed again. Because a single request to create the environment will be destroyed immediately after the request execution.
In other words, in the traditional life cycle of PHP in order to execute the script wasted a lot of time to create and destroy resources. Imagine frameworks like Laravel, how many files on each request O operations need to load? But also waste a lot of I /.

So if we use Swoole built an application-level Server, and all script files after loading time you can be kept in memory of it? That's why we need to try to run on Swoole Laravel. Swoole can provide powerful performance and elegant Laravel can use the code structure. Maybe children is really a perfect combination!

Fifth, use Laravel Swoole enhance the performance of
existing wheels, the following two feel is still very good, you can choose

  • swooletw/laravel-swoole
    garveen/laravoole

     

I chose the first one to test the
use composer installation:

composer require swooletw/laravel-swoole

 

If you are using laravel, then add the providers array config / app.php in

SwooleTW\Http\LaravelServiceProvider::class,

 

If you are using a lumen, then add the following code in the bootstrap / app.php in

$app->register(SwooleTW\Http\LumenServiceProvider::class); 

 

Export the configuration file to the config directory

php artisan vendor:publish --provider="SwooleTW\Http\HttpServiceProvider"

 

Then go config / swoole_http.php configuration information

'server' => [

        'host' => env('SWOOLE_HTTP_HOST', '0.0.0.0'),//监听任意ip

        'port' => env('SWOOLE_HTTP_PORT', '1215'),

        'options' => [

            'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),

            'the log_file ' => the env ( ' SWOOLE_HTTP_LOG_FILE ' , base_path ( ' Storage / logs / swoole_http.log ' )), 

            ' to daemonize ' => the env ( ' SWOOLE_HTTP_DAEMONIZE ' , . 1 ), // l- program into the background as a daemon run 

        ], 

],

 

swoole_http.php there is also configured to provide an array of providers,

'providers' => [

    // App\Providers\AuthServiceProvider::class,

]

 

Because after use swoole as http, these providers will be saved into memory, so the configuration of here every request wants to re-register and re-start of providers.

Now, you can execute the following command to start Swoole HTTP service.

$ php artisan swoole:http start

 

Then you can see the following information:

Starting swoole http server...

Swoole http server started: <http://0.0.0.0:1215>

 

Now you can access through   to enter Laravel applications.
Note: This expansion does not support hot start, so every time code update should restart the service php artisan swoole: http restart

Six performance tests
using Apache's ab testing tools

ab -n 1000 -c 10 http://127.0.0.1:1215/

 

Parameters: -n 1000 -c 10 concurrent requests Number

 

 

 

 

Figure One use swoole as an application server, apache server Figure II is
testing environment in a virtual machine, computer configuration is also poor performance has not fully played out, you can see only 197 times apache complete request could not carry on the pressure, swoole HTTP service completed pressure test, dysfunctional fully rolled apache server.

Seven, using Nginx proxy

swoole also mentioned in the official website: swoole_http_server support Http protocol is not complete, it is recommended only as an application server. And increases as the front end Nginx agent.
So, we need to configure nginx.conf increase in the server:

 1 server {
 2 
 3     listen 80;
 4 
 5     server_name your.domain.com;
 6 
 7     root /path/to/laravel/public;
 8 
 9     index index.php;
10 
11  
12 
13     location = /index.php {
14 
15         # Ensure that there is no such file named "not_exists"
16 
17         # in your "public" directory.
18 
19         try_files /not_exists @swoole;
20 
21     }
22 
23  
24 
25     location / {
26 
27         try_files $uri $uri/ @swoole;
28 
29     }
30 
31  
32 
33     location @swoole {
34 
35         set $suffix "";
36 
37  
38 
39         if ($uri = /index.php) {
40 
41             set $suffix "/";
42 
43         }
44 
45  
46 
47         proxy_set_header Host $host;
48 
49         proxy_set_header SERVER_PORT $server_port;
50 
51         proxy_set_header REMOTE_ADDR $remote_addr;
52 
53         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
54 
55  
56 
57         # IF https
58 
59         # proxy_set_header HTTPS "on";
60 
61  
62 
63         proxy_pass http://127.0.0.1:1215$suffix;
64 
65     }
66 
67 }

 

配置可参考swoole方文档官 Nginx/Apache配置
至此,大功告成,你可以像平常一样访问你的网站了。

八、使用swoole和传统php开发的缺点
本文主要介绍了使用swoole作为laravel的应服务器,最后说下使用swoole和传统php开发的缺点。
1、更难上手。这要求开发人员对于多进程的运行模式有更清晰的认识
2、更容易内存泄露。在处理全局变量,静态变量的时候一定要小心,这种不会被GC清理的变量会存在整个生命周期中,如果没有正确的处理,很容易消耗完所有的内存。在php-fpm下,php代码执行完内存就会被完全释放。

以上就是swoole运行模式加速laravel应用的详细介绍的详细内容

Guess you like

Origin www.cnblogs.com/a609251438/p/12078354.html