使用 Swoole 来加速 Laravel应用

Swoole 是为 PHP 开发的生产级异步编程框架。 他是一个纯 C 开发的扩展, 他容许 PHP 开发者在 PHP 中写 高性能,可扩展的并发 TCP, UDP, Unix socket, HTTP, WebSocket 服务, 而不须要拥有太多的非阻塞 I/O 编程和低级别的 Linux 内核知识。 你能够把 Swoole 想象成 NodeJS, 但对于 PHP 来讲将有更高性能php

文章转自微笑大神博客:https://badwritten.cn/article/detail?operate=true&id=62

为何要使用 Swoole

61.png

上图展现了 PHP 的生命周期。正如你所看到的那样,当你每次运行 PHP 脚本的时候,PHP都须要初始化模块并为你的运行环境启动Zend引擎。而且将 PHP 脚本编译为 OpCodes 以便 Zend引擎执行。mysql

可是, 这样的生命周期须要在每次请求的时候都执行一遍。由于单个请求建立的环境在请求执行结束后会当即销毁。laravel

在传统的 PHP 生命周期中, 为了脚本执行而浪费了大量的时间去建立和销毁资源。想象一下像 Laravel 这样的框架, 在每次请求中须要加载多少文件? 同时也浪费了大量的 I/O 操做c++

所以若是咱们利用 Swoole 内置一个应用级别的 Server, 而且全部脚本文件在加载一次以后即可以保存在内存中呢? 这就是为何咱们须要尝试在 Swoole 上运行 Laravel。 Swoole 能够提供强大性能而 Laravel 则能够提供优雅代码结构使用git

首先感谢布欧大神对个人帮助,在布欧大神的帮助下才有幸知道Swoole而且安装使用github

安装PHP7.2

yum -y install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php72w php72w-cli php72w-fpm php72w-mysqlnd php72w-devel php72w-gd php72w-xml php72w-mbstring -y

安装所须要依赖

yum install glibc-headers -y yum install gcc-c++ -y

安装 Swoole

pecl install swoole
# 安装时能够选择使用的扩展 一、enable sockets supports? 二、enable openssl support? yum install -y openssl-devel 三、enable http2 support? 四、enable mysqlnd support? 五、enable postgresql coroutine client support?

或者编译安装web

# 下载方法一 git clone https://gitee.com/swoole/swoole.git # 下载方法二 wget http://pecl.php.net/get/swoole-4.2.13.tgz # 下载方法三 git clone https://github.com/swoole/swoole-src/releases/tag/v4.2.13 cd swoole sudo phpize (ubuntu 没有安装phpize可执行命令:sudo apt-get install php-dev来安装phpize) sudo ./configure sudo make sudo make install

还有自动脚本redis

mkdir -p ~/build && \ cd ~/build && \ rm -rf ./swoole-src && \ curl -o ./tmp/swoole.tar.gz https://github.com/swoole/swoole-src/archive/master.tar.gz -L && \ tar zxvf ./tmp/swoole.tar.gz && \ mv swoole-src* swoole-src && \ cd swoole-src && \ phpize && \ ./configure \ --enable-coroutine \ --enable-openssl \ --enable-http2 \ --enable-async-redis \ --enable-sockets \ --enable-mysqlnd && \ make clean && make && sudo make install

配置 php.ini

# 查看 php.ini 位置 php -i | grep php.ini # 写入配置(建议在文件头部写入,小编试过在末尾加入可是 php -m 中没有swoole扩展) echo "extension=swoole.so" >> /etc/php.ini # 检查是否开始 Swoole 扩展 php -m | grep swoole

检验安装结果

# 方法一 php -m | grep swoole # 方法二 php -i | grep swoole # 方法三 php --ri swoole

laravel 扩展方法一

安装 laravel-swoole

composer require swooletw/laravel-swoole

配置 laravel-swoole

# 在 config/app.php 服务提供者数组添加该服务提供者 SwooleTW\Http\LaravelServiceProvider::class,

运行

php artisan swoole:http start

90.png

laravel 扩展方法二

安装 laravel-s

composer require "hhxsv5/laravel-s:~3.4.0" -vvv

配置 laravel-s

# 修改config/app.php 'providers' => [ //... Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class, ],

发布配置及二进制文件

php artisan laravels publish
# 配置文件:config/laravels.php # 二进制文件:bin/laravels bin/fswatch

运行

php bin/laravels {start|stop|restart|reload|info|help}

99.png

注意

使用 laravel的插件后会出现这样的问题
一、在一处登陆后,全部设备均显示已登陆
二、使用 jwt 时,auth 获取当前登陆用户有问题
缘由在于请求被缓存等问题,咱们须要每次请求时从新注册服务商
在laravel-s的配置文件中已经有所解决
100.png
在laravel-swoole拓展中也能够相应配置sql

Illuminate \ Auth \ AuthServiceProvider :: class, Illuminate \ Broadcasting \ BroadcastServiceProvider :: class, Illuminate \ Bus \ BusServiceProvider :: class, Illuminate \ Cache \ CacheServiceProvider :: class, Illuminate \ Foundation \ Providers \ ConsoleSupportServiceProvider :: class, Illuminate \ Cookie \ CookieServiceProvider :: class, Illuminate \ Database \ DatabaseServiceProvider :: class, Illuminate \ Encryption \ EncryptionServiceProvider :: class, Illuminate \ Filesystem \ FilesystemServiceProvider :: class , Illuminate \ Foundation \ Providers \ FoundationServiceProvider :: class, Illuminate \ Hashing \ HashServiceProvider :: class, Illuminate \ Mail \ MailServiceProvider :: class, Illuminate \ Notifications \ NotificationServiceProvider :: class, Illuminate \ Pagination \ PaginationServiceProvider :: class, Illuminate \ Pipeline \ PipelineServiceProvider :: class, Illuminate \ Queue \ QueueServiceProvider :: class, Illuminate \ Redis \ RedisServiceProvider :: class, Illuminate \ Auth \ Passwords \ PasswordResetServiceProvider :: class, Illuminate \ Session \ SessionServiceProvider :: class, Illuminate \ Translation \ TranslationServiceProvider :: class, Illuminate \ Validation \ ValidationServiceProvider :: class, Illuminate \ View \ ViewServiceProvider :: class , App \ Providers \ AppServiceProvider :: class, App \ Providers \ AuthServiceProvider :: class, // App \ Providers \ BroadcastServiceProvider :: class, App \ Providers \ EventServiceProvider :: class, App \ Providers \ RouteServiceProvider :: class,

参考连接
在一处登陆后,全部设备均显示已登陆
使用 jwt 时,auth 获取当前登陆用户有问题编程

问题解决

一、编译报错

/usr/include/php/Zend/zend_portability.h:312:52: note: in definition of macro 'UNEXPECTED' # define UNEXPECTED(condition) __builtin_expect(!!(condition), 0) ^ /usr/include/php/Zend/zend_operators.h: In function 'void fast_long_sub_function(zval*, zval*, zval*)': /usr/include/php/Zend/zend_operators.h:657:80: error: '__builtin_ssubl_overflow' was not declared in this scope if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) { ^ /usr/include/php/Zend/zend_portability.h:312:52: note: in definition of macro 'UNEXPECTED' # define UNEXPECTED(condition) __builtin_expect(!!(condition), 0) ^ make: *** [swoole_async.lo] Error 1 ERROR: `make' failed

64.jpg

解决方案:

# 升级 gcc yum install centos-release-scl -y yum install devtoolset-7 -y scl enable devtoolset-7 bash

二、

running: phpize
Can't find PHP headers in /usr/include/php
The php-devel package is required for use of this command.
ERROR: `phpize' failed

65.jpg

解决方案:

yum install php-devel -y yum install php-pear -y yum install gcc gcc-c++ autoconf automake -y

三、

/var/tmp/swoole/include/swoole.h:471:25: fatal error: openssl/ssl.h: No such file or directory #include <openssl/ssl.h> 

66.jpg

解决方案

yum install openssl-devel -y

三、pecl未安装
84.jpg

解决方案

yum install php-pear php-devel -y

四、

error: #error "Enable http2 support, require nghttp2 library." #error "Enable http2 support, require nghttp2 library."

85.jpg

解决方案

wget https://github.com/nghttp2/nghttp2/releases/download/v1.30.0/nghttp2-1.30.0.tar.bz2 
tar -jxvf nghttp2-1.30.0.tar.bz2 
cd nghttp2-1.30.0 
./configure make make install

五、
63.jpg

解决方案

 

出现这种错误,你的机器还须要安装php-devel

yum install php-devel -y
相关文章
相关标签/搜索