使用 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

注意

使用 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM