Swoole入門到實戰(一):PHP7&Swoole源碼安裝、玩轉網絡通訊引擎、異步非堵塞IO場景

1、PHP7源碼安裝和Swoole源碼編譯安裝

1.1 PHP7源碼安裝

1.1.1 獲取源碼與安裝

    獲取PHP7源碼:www.php.netphp

tar -xzvf ... # 解壓命令

./configure --prefix=/home/study/php # 安裝至某個路徑,提早安裝gcc等
make # 編譯
make install # 安裝

    源碼執行文件放在:bin目錄下html

php -m  # 查看 PHP 安裝的擴展

1.1.2 簡化PHP執行命令

    alias 命令=命令的絕對路徑mysql

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是等效的react

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

1.2 Swoole源碼編譯安裝

獲取swoole源碼:https://gitee.com/swoole/swoole.gitgit

phpize是用來擴展php模塊的,經過phpize能夠創建php的外掛模塊,解決沒有configure問題github

/usr/local/php/bin/phpize # 在須要執行的目錄執行這行代碼便可
./configure --with-php-config=/usr/local/php/bin/php-config
make
 make install

    最後能夠在PHP的擴展目錄中看見swoole.so 擴展文件web

    clipboard.png

1.3 雙劍合璧,PHP7支持swoole

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

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

netstat -anp|grep 9501

2、玩轉網絡通訊引擎(很是重要)

2.1 TCP服務&TCP客戶端

2.1.1 TCP服務

Swoole官網文檔:建立TCP服務器 | 建立UDP服務器json

//建立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. netstat -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();

3、異步非堵塞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完美支持ThinkPHP五、分發Task異步任務機制實現

參考教程:韓天峯力薦 Swoole入門到實戰打造高性能賽事直播平臺

相關文章
相關標籤/搜索