CentOS7 安裝PHP7的swoole擴展:


一、緒

Swoole簡介

  • PHP異步網絡通信引擎

  • 最終編譯為so文件作為PHP的擴展

 

准備工作

  • Linux環境

  • PHP7 swoole2.1 redis

  • 源碼安裝PHP7 源碼安裝swoole

https://www.cnblogs.com/cshaptx4869/p/10395744.html

https://www.cnblogs.com/cshaptx4869/p/10493942.html

https://www.cnblogs.com/cshaptx4869/p/10493933.html

 

定時器

  • swoole_timer_tick 每隔一定時間執行,毫秒

  • swoole_timer_after 一定時間后執行,毫秒

  • swoole_timer_clear 清除定時器

 

異步文件系統IO(4.3版本之前)

https://wiki.swoole.com/wiki/page/183.html

  • swoole_async_readfile 異步讀取文件內容

    執行順序:1、先讀取文件 2、再執行下碼 3、最后執行回調

    讀取的文件不能超過4M

  • swoole_async_read

  • swoole_async_writefile

    執行順序和 readfile 相同

  • swoole_async_write

     

異步MySQL客戶端

https://wiki.swoole.com/wiki/page/517.html

  • 使用場景

    請求文章詳情頁 -> mysql 文章+1 -> 頁面數據展示

 

異步redis

https://wiki.swoole.com/wiki/page/p-redis.html

 

進程

https://wiki.swoole.com/wiki/page/p-process.html

進程就是正在運行的程序的一個實例。

 

內存

https://wiki.swoole.com/wiki/page/245.html

  • Table

    Table一個基於共享內存和鎖實現的超高性能,並發數據結構。用於解決多進程/多線程數據共享和同步加鎖問題。

  • Atomic

    AtomicSwoole底層提供的原子計數操作類,可以方便整數的無鎖原子增減。

 

協程

https://wiki.swoole.com/wiki/page/p-coroutine.html

 

二、安裝

 

> wget https://github.com/swoole/swoole-src/archive/v4.3.5.tar.gz

> tar xzvf v4.3.5.tar.gz

> cd swoole-src-4.3.5/

> phpize

> ./configure --with-php-config=/usr/local/php71/bin/php-config --enable-http2 --enable-openssl

> make && make install

> echo 'extension=swoole.so' >> /usr/local/php71/lib/php.ini

> systemctl restart php-fpm.service

 

如果使用hyperf框架修改php.ini配置文件

swoole.use_shortname = 'Off'

$ php --ri swoole

 

三、nginx+swoole配置

erver {
    root /data/wwwroot/;
    server_name local.swoole.com;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "keep-alive";
        proxy_set_header X-Real-IP $remote_addr;
        if (!-e $request_filename) {
             proxy_pass http://127.0.0.1:9501;
        }
    }
}

#通過讀取$request->header['x-real-ip']來獲取客戶端的真實IP

 

四、手冊

https://toxmc.github.io/swoole-cs.github.io/

demo: https://www.helloweba.net/tag/swoole.html


免責聲明!

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



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