Swoole入门到实战(一):PHP7&Swoole源码安装、玩转网络通信引擎、异步非堵塞IO场景

1,953 阅读7分钟
原文链接: segmentfault.com

一、PHP7源码安装和Swoole源码编译安装

1.1 PHP7源码安装

1.1.1 获取源码与安装

    获取PHP7源码:www.php.net

tar -xzvf ... # 解压命令

./configure --prefix=/home/study/php # 安装至某个路径,提前安装gcc等
make # 编译
make install # 安装

    源码执行文件放在:bin目录下

php -m  # 查看 PHP 安装的扩展

1.1.2 简化PHP执行命令

    alias 命令=命令的绝对路径

vim /.bash_profile
alias php=/home/work/soft/php/bin/php # 添加
source /.bash_profile # 注意

    source FileName
    作用:在当前bash环境下读取并执行FileName中的命令。    用于重新执行刚修改的初始化文档,如 .bash_profile.profile 等等
    注:该命令通常用命令“.”来替代
    如:source /etc/profile. /etc/profile是等效的

php -i | grep php.ini # 查找PHP的配置文件

1.2 Swoole源码编译安装

获取swoole源码:https://gitee.com/swoole/swoole.git

phpize是用来扩展php模块的,通过phpize可以建立php的外挂模块,解决没有configure问题

/usr/local/php/bin/phpize # 在需要执行的目录执行这行代码即可
./configure --with-php-config=/usr/local/php/bin/php-config   
 make
 make install

    最后可以在PHP的扩展目录中看见swoole.so 扩展文件

    clipboard.png

1.3 双剑合璧,PHP7支持swoole

    在php.ini文件中添加:extension=swoole.so
    查看是否添加成功:php -m

    在swoole/examples/server下执行php echo.php
    查看是否执行端口:9501

netstat -anp|grep 9501

二、玩转网络通信引擎(非常重要)

2.1 TCP服务&TCP客户端

2.1.1 TCP服务

Swoole官网文档:创建TCP服务器 | 创建UDP服务器

//创建Server对象,监听 127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);
//swoole_server->set函数用于设置swoole_server运行时的各项参数
$serv->set([
    'worker_num' => 6 , // worker进程数,cpu 1-4倍
    'max_request' => 10000,
]);
/**
 * 监听连接进入事件
 * $fd 客户端连接的唯一标示
 * $reactor_id 线程id
 */
$serv->on('connect', function ($serv, $fd, $reactor_id) {
    echo "Client: {$reactor_id} - {$fd}-Connect.\n";
});
/**
 * 监听数据接收事件
 * $reactor_id = $from_id
 */
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
    $serv->send($fd, "Server: {$reactor_id} - {$fd}".$data);
});
//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
    echo "Client: Close.\n";
});
//启动服务器
$serv->start();

    测试tcp服务器方法:

  1. netstats -anp | grep 9501
  2. 通过telnet方式登录远程主机:telnet 127.0.0.1 9501
  3. tcp客户端脚本

    查看当前worker进程数:ps -aft | grep tcp_server.php

    clipboard.png

    Tips:为了保证程序执行的完整性,当修改tcp服务器脚本后最好设置平滑重启worker进程
    平滑重启worker进程

2.1.2 TCP客户端

阿里云服务器巨坑----端口未对外打开!!!websocket连接不上服务器,提示Provisional headers are shown

    clipboard.png

<?php
// 连接 swoole tcp 服务
$client = new swoole_client(SWOOLE_SOCK_TCP);

if(!$client->connect("127.0.0.1", 9501)) {
    echo "连接失败";
    exit;
}

// php cli常量
fwrite(STDOUT, "请输入消息:");
$msg = trim(fgets(STDIN));

// 发送消息给 tcp server服务器
$client->send($msg);

// 接受来自server 的数据
$result = $client->recv();
echo $result;

2.2 HTTP服务(常用

    clipboard.png

$http = new swoole_http_server("0.0.0.0", 8811);

//添加测试一:获取参数并打印出来
//$http->on('request', function ($request, $response) {
//    $response->cookie("singwa",'xsssss', time() + 1800);
//    $response->end('sss'.json_encode($request->get));
//});
/**
 * https://wiki.swoole.com/wiki/page/783.html
 * 配置静态文件根目录,与enable_static_handler配合使用。
 * 设置document_root并设置enable_static_handler为true后,
 * 底层收到Http请求会先判断document_root路径下是否存在此文件,
 * 如果存在会直接发送文件内容给客户端,不再触发onRequest回调。
 */
$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => "/home/work/hdtocs/swoole_mooc/data",
    ]
);
$http->on('request', function($request, $response) {
    //print_r($request->get);
    $content = [
        'date:' => date("Ymd H:i:s"),
        'get:' => $request->get,
        'post:' => $request->post,
        'header:' => $request->header,
    ];
    swoole_async_writefile(__DIR__."/access.log", json_encode($content).PHP_EOL, function($filename){
        // todo
    }, FILE_APPEND);
    $response->cookie("singwa", "xsssss", time() + 1800);
    $response->end("sss". json_encode($request->get));
});

$http->start();

2.3 WebSocket服务(重点

2.3.1 基本概述

    WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信--允许服务器主动发送信息给客户端

    为什么需要WebSocket

  • 缺陷:HTTP的通信只能由客户端发起

    WebSocket特点

  1. 建立在TCP协议之上
  2. 性能开销小通信高效
  3. 客户端可以与任意服务器通信
  4. 协议标识符ws wss
  5. 持久化网络通信协议

2.3.2 案例实现

2.3.2.1 服务端实现

    1. 面向过程:procedure_ws_server.php

$server = new swoole_websocket_server("0.0.0.0", 9912);
//配置静态文件根目录,可选
$server->set(
    [
        'enable_static_handler' => true,
        'document_root' => "/home/wwwroot/www.lingyuan88.com/public/swoole/data",
    ]
);
//监听websocket连接打开事件
$server->on('open', 'onOpen');
function onOpen($server, $request) {
    print_r($request->fd);
}
// 监听ws消息事件
$server->on('message', function (swoole_websocket_server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "singwa-push-secesss");
});
$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();

    2. WebSocket服务优化,基础类库面向对象:object_ws_server.php

class Ws {

    CONST HOST = "0.0.0.0";
    CONST PORT = 9912;
    public $ws = null;
    public function __construct() {
        $this->ws = new swoole_websocket_server(self::HOST, self::PORT);
        //配置静态文件根目录,可选
        $this->ws->set(
            [
                'enable_static_handler' => true,
                'document_root' => "/home/wwwroot/www.lingyuan88.com/public/swoole/data",
            ]
        );
        $this->ws->on("open", [$this, 'onOpen']);
        $this->ws->on("message", [$this, 'onMessage']);
        $this->ws->on("close", [$this, 'onClose']);

        $this->ws->start();
    }
    /**
     * 监听ws连接事件
     * @param $ws
     * @param $request
     */
    public function onOpen($ws, $request) {
        print_r($request->fd);
    }
    /**
     * 监听ws消息事件
     * @param $ws
     * @param $frame
     */
    public function onMessage($ws, $frame) {
        echo "ser-push-message:{$frame->data}\n";
        $ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
    }
    /**
     * close
     * @param $ws
     * @param $fd
     */
    public function onClose($ws, $fd) {
        echo "clientid:{$fd}\n";
    }
}
$obj = new Ws();

2.3.2.2 客户端实现

    ws_client.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
<h1>singwa-swoole-ws测试</h1>
  <script>
    var wsUrl = "ws://120.77.206.215:9912";
    var websocket = new WebSocket(wsUrl);
    //实例对象的onopen属性
    websocket.onopen = function(evt) {
      websocket.send("hello-sinwa");
      console.log("conected-swoole-success");
    }
    // 实例化 onmessage
    websocket.onmessage = function(evt) {
      console.log("ws-server-return-data:" + evt.data);
    }
    //onclose
    websocket.onclose = function(evt) {
      console.log("close");
    }
    //onerror
    websocket.onerror = function(evt, e) {
      console.log("error:" + evt.data);
    }
 </script>
</body>
</html>

2.3.2.3 测试

    1. 通过WebSocket静态文件目录测试

    clipboard.png

    2. 通过HTTP服务测试

    clipboard.png

2.4 异步Task任务使用(重点

    使用场景

  • 执行耗时的操作(发送邮件 广播等)

    注意:

  • 投递异步任务之后程序会继续往下执行,不会等待任务执行完后再继续向下执行
class Ws {
    CONST HOST = "0.0.0.0";
    CONST PORT = 9912;
    public $ws = null;
    public function __construct() {
        $this->ws = new swoole_websocket_server(self::HOST, self::PORT);
        $this->ws->set(
            [
                'worker_num' => 2,
                'task_worker_num' => 2,
            ]
        );
        //注册Server的事件回调函数
        $this->ws->on("open", [$this, 'onOpen']);
        $this->ws->on("message", [$this, 'onMessage']);
        $this->ws->on("task", [$this, 'onTask']);
        $this->ws->on("finish", [$this, 'onFinish']);
        $this->ws->on("close", [$this, 'onClose']);
        $this->ws->start();
    }
    /**
     * 监听ws连接事件
     * @param $ws
     * @param $request
     */
    public function onOpen($ws, $request) {
        var_dump($request->fd);
    }
    /**
     * 监听ws消息事件
     * @param $ws
     * @param $frame
     */
    public function onMessage($ws, $frame) {
        echo "ser-push-message:{$frame->data}\n";
        // todo 10s
        $data = [
            'task' => 1,
            'fd' => $frame->fd,
        ];
        //投递异步任务
        //注意:程序会继续往下执行,不会等待任务执行完后再继续向下执行
        $ws->task($data);
        //客户端会马上收到以下信息
        $ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
    }
    /**
     * @param $serv
     * @param $taskId
     * @param $workerId
     * @param $data
     * @return string
     */
    public function onTask($serv, $taskId, $workerId, $data) {
        print_r($data);
        // 耗时场景 10s
        sleep(10);
        return "on task finish"; // 告诉worker,并返回给onFinish的$data
    }
    /**
     * @param $serv
     * @param $taskId
     * @param $data
     */
    public function onFinish($serv, $taskId, $data) {
        echo "taskId:{$taskId}\n";
        echo "finish-data-sucess:{$data}\n";
    }
    /**
     * close
     * @param $ws
     * @param $fd
     */
    public function onClose($ws, $fd) {
        echo "clientid:{$fd}\n";
    }
}
$obj = new Ws();

三、异步非堵塞IO场景

3.1 异步、阻塞和IO模型(务必理解

3.1.1 同步和异步

    关注的是消息通知机制;

同步:调用发出之后不会立即返回,但一旦返回,则返回最终结果;

异步:调用发出之后,被调用方立即返回消息但返回的并非最终结果。被调用者通过状态、通知机制等来通知调用者,或通过回调函数来处理结果;

3.1.2 阻塞(block)和非阻塞(nonblock)

    关注的是调用者等待被调用者返回调用结果时的状态。

阻塞:调用结果返回之前,调用者会被挂起,调用者只有在得到返回结果之后才能继续。

非阻塞:调用者在结果返回之前,不会被挂起;

3.1.3 IO模型

blocking IO:阻塞式IO 
nonblocking IO:非阻塞IO
multiplexing IO:多路复用IO 
signal driven IO:事件驱动式IO 
asynchronous IO:异步IO 

    真正执行IO过程的阶段是内核内存数据拷贝到进程内存

3.2 Swoole异步毫秒定时器

    异步高精度定时器,粒度为毫秒级

//每隔2000ms触发一次
swoole_timer_tick(2000, function ($timer_id) {
    echo "tick-2000ms\n";
});

//3000ms后执行此函数
swoole_timer_after(3000, function () {
    echo "after 3000ms.\n";
});

3.3 异步文件系统IO

    Swoole官网文档:异步文件系统IO

3.3.1 异步读

/**
 * 读取文件
 * __DIR__
 * 文件不存在会返回false
 * 成功打开文件立即返回true
 * 数据读取完毕后会回调指定的callback函数。
 */
//函数风格
$result = swoole_async_readfile(__DIR__."/1.txt", function($filename, $fileContent) {
    echo "filename:".$filename.PHP_EOL;  // \n \r\n
    echo "content:".$fileContent.PHP_EOL;
});
//命名空间风格
$result = Swoole\Async::readfile(__DIR__."/1.txt", function($filename, $fileContent) {
    echo "filename:".$filename.PHP_EOL;  // \n \r\n
    echo "content:".$fileContent.PHP_EOL;
});
var_dump($result);
echo "start".PHP_EOL;

3.3.2 异步写(如日志)

$http->on('request', function($request, $response) {
    $content = [
        'date:' => date("Ymd H:i:s"),
        'get:' => $request->get,
        'post:' => $request->post,
        'header:' => $request->header,
    ];
    swoole_async_writefile(__DIR__."/access.log", json_encode($content).PHP_EOL, function($filename){
        // todo
    }, FILE_APPEND);
    $response->end("response:". json_encode($request->get));
});

3.4 异步MySQL详解

class AsyncMySql {
    /**
     * @var string
     */
    public $dbSource = "";
    /**
     * mysql的配置
     * @var array
     */
    public $dbConfig = [];
    public function __construct() {
        //new swoole_mysql;
        $this->dbSource = new Swoole\Mysql;

        $this->dbConfig = [
            'host' => '127.0.0.1',
            'port' => 3306,
            'user' => 'root',
            'password' => 'test',
            'database' => 'test',
            'charset' => 'utf8',
        ];
    }
    public function update() {}
    public function add() {}
    /**
     * mysql 执行逻辑
     * @param $id
     * @param $username
     * @return bool
     */
    public function execute($id, $username) {
        $this->dbSource->connect($this->dbConfig, function($db, $result) use($id, $username)  {
            echo "mysql-connect".PHP_EOL;
            if($result === false) {
                var_dump($db->connect_error);
                // todo
            }
            $sql = "select * from cmf_user where id=1";
            //$sql = "update test set `username` = '".$username."' where id=".$id;
            // insert into
            // query (add select update delete)
            $db->query($sql, function($db, $result){
                // select => result返回的是 查询的结果内容
                if($result === false) {
                    // todo
                    var_dump($db->error);
                }elseif($result === true) {// add update delete
                    // todo
                    var_dump($db->affected_rows);
                }else {
                    print_r($result);
                }
                $db->close();
            });

        });
        return true;
    }
}
$obj = new AsyncMySql();
$flag = $obj->execute(1, 'singwa-111112');
var_dump($flag).PHP_EOL;
echo "start".PHP_EOL;

    clipboard.png

3.5 异步Redis

3.5.1 环境准备

    swoole使用redis的前置条件

  • redis服务
  • hiredis
  • 编译swoole需要加入 -enable-async-redis

    编译安装hiredis

使用Redis客户端,需要安装hiredis库,下载hiredis源码后,执行
make -j
sudo make install
sudo ldconfig

    hiredis下载地址

    启用异步Redis客户端

编译swoole时,在configure指令中加入--enable-async-redis
[root@izwz93ee3z8wdxsujiec2oz swoole]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-async-redis
make clean
make -j
sudo make install

    查看PHPswoole扩展:php -m
    查看hiredis是否编译安装成功:php --ri swoole

3.5.2 代码测试

$redisClient = new swoole_redis;// Swoole\Redis
$redisClient->connect('127.0.0.1', 6379, function(swoole_redis $redisClient, $result) {
    echo "connect".PHP_EOL;
    var_dump($result);

    // 同步 redis (new Redis())->set('key',2);
    /*$redisClient->set('singwa_1', time(), function(swoole_redis $redisClient, $result) {
        var_dump($result);
    });*/

    /*$redisClient->get('singwa_1', function(swoole_redis $redisClient, $result) {
        var_dump($result);
        $redisClient->close();
    });*/
    $redisClient->keys('*gw*', function(swoole_redis $redisClient, $result) {
        var_dump($result);
        $redisClient->close();
    });

});
echo "start".PHP_EOL;

未完待续!下一章:Swoole入门到实战(二):进程,内存和协程、赛事直播--登录模块、赛事直播 – 直播模块

参考教程:韩天峰力荐 Swoole入门到实战打造高性能赛事直播平台