CentOS7 安装PHP7的swoole扩展:

时间:2023-03-09 06:46:15
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

CentOS7 安装PHP7的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