使用 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
相關文章
相關標籤/搜索