獲取PHP7源碼:www.php.net
php
tar -xzvf ... # 解壓命令 ./configure --prefix=/home/study/php # 安裝至某個路徑,提早安裝gcc等 make # 編譯 make install # 安裝
源碼執行文件放在:bin
目錄下html
php -m # 查看 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的配置文件
獲取swoole源碼:https://gitee.com/swoole/swoole.git
git
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
在php.ini
文件中添加:extension=swoole.so
查看是否添加成功:php -m
redis
在swoole/examples/server
下執行php echo.php
查看是否執行端口:9501
sql
netstat -anp|grep 9501
很是重要
)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
服務器方法:
netstat -anp | grep 9501
telnet
方式登陸遠程主機:telnet 127.0.0.1 9501
tcp
客戶端腳本 查看當前worker
進程數:ps -aft | grep tcp_server.php
Tips:爲了保證程序執行的完整性,當修改tcp
服務器腳本後最好設置平滑重啓worker
進程
平滑重啓worker進程
<?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;
經常使用
)
$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();
WebSocket
協議是基於TCP
的一種新的網絡協議。它實現了瀏覽器與服務器全雙工(full-duplex
)通訊--容許服務器主動發送信息給客戶端
爲何須要WebSocket
HTTP
的通訊只能由客戶端發起WebSocket特色
TCP
協議之上ws
wss
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();
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>
1. 經過WebSocket
靜態文件目錄測試
2. 經過HTTP
服務測試
使用場景
注意:
繼續往下執行
,不會等待任務執行完後再繼續向下執行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();
務必理解
) 關注的是消息通知
機制;
同步: 調用發出以後不會當即返回
,但一旦返回,則返回最終結果;異步:調用發出以後,被調用方
當即返回消息
,但返回的並不是最終結果。被調用者經過狀態、通知機制等來通知調用者,或經過回調函數來處理結果;
關注的是調用者等待被調用者返回調用結果時的狀態。
阻塞:調用結果返回以前,調用者會被掛起
,調用者只有在獲得返回結果以後才能繼續。非阻塞:調用者在結果返回以前,不會被掛起;
blocking IO:阻塞式IO nonblocking IO:非阻塞IO multiplexing IO:多路複用IO signal driven IO:事件驅動式IO asynchronous IO:異步IO
真正執行IO
過程的階段是內核內存數據
拷貝到進程內存
中
異步
高精度定時器,粒度爲毫秒級
//每隔2000ms觸發一次 swoole_timer_tick(2000, function ($timer_id) { echo "tick-2000ms\n"; }); //3000ms後執行此函數 swoole_timer_after(3000, function () { echo "after 3000ms.\n"; });
/** * 讀取文件 * __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;
$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)); });
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;
swoole使用redis的前置條件
redis
服務hiredis
庫swoole
須要加入 -enable-async-redis
編譯安裝hiredis
使用Redis
客戶端,須要安裝hiredis
庫,下載hiredis
源碼後,執行
make -j sudo make install sudo ldconfig
啓用異步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
查看PHP
的swoole
擴展:php -m
查看hiredis
是否編譯安裝成功:php --ri swoole
$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;