使用 Swoole 来加速 Laravel应用

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

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

为什么要使用 Swoole

61.png

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

但是, 这样的生命周期需要在每次请求的时候都执行一遍。因为单个请求创建的环境在请求执行结束后会立即销毁。

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

因此如果我们利用 Swoole 内置一个应用级别的 Server, 并且所有脚本文件在加载一次之后便可以保存在内存中呢? 这就是为什么我们需要尝试在 Swoole 上运行 Laravel。 Swoole 可以提供强大性能而 Laravel 则可以提供优雅代码结构使用

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

安装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
# 安装时可以选择使用的扩展
1、enable sockets supports?
2、enable openssl support? 
   yum install -y openssl-devel
3、enable http2 support?
4、enable mysqlnd support? 5、enable postgresql coroutine client support?

或者编译安装

# 下载方法一
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

还有自动脚本

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

扫描二维码关注公众号,回复: 5542525 查看本文章

注意

使用 laravel的插件后会出现这样的问题
1、在一处登录后,所有设备均显示已登录
2、使用 jwt 时,auth 获取当前登录用户有问题
原因在于请求被缓存等问题,我们需要每次请求时重新注册服务商
在laravel-s的配置文件中已经有所解决
100.png
在laravel-swoole拓展中也可以相应配置

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 获取当前登录用户有问题

问题解决

1、编译报错

/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

2、

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

3、

/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

3、pecl未安装
84.jpg

解决方案

yum install php-pear php-devel -y

4、

 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

5、
63.jpg

解决方案

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

yum install php-devel -y

猜你喜欢

转载自www.cnblogs.com/phpk/p/10536367.html