官網文檔
thinkphp6文檔 https://www.kancloud.cn/manual/thinkphp6_0/1037479? swoole文檔 https://wiki.swoole.com/#/ think-swoole文檔 https://www.kancloud.cn/manual/thinkphp6_0/1359700
安裝
composer require topthink/think-swoole
命令行
php think swoole [start|stop|reload|restart]
服務啟動
當你在命令行php think swoole下執行完成之后就會啟動一個http server,可以直接訪問當前的應用【推薦教程:thinkphp】
'server' => [ 'host' => env('SWOOLE_HOST', '0.0.0.0'), // 監聽地址 'port' => env('SWOOLE_PORT', 9501), // 監聽端口 'mode' => SWOOLE_PROCESS, // 運行模式 默認為SWOOLE_PROCESS 'sock_type' => SWOOLE_SOCK_TCP, // sock type 默認為SWOOLE_SOCK_TCP 'options' => [ // 服務啟動后,進程ID存放文件 'pid_file' => runtime_path() . 'swoole.pid', // swoole 的日志文件 'log_file' => runtime_path() . 'swoole.log', // 守護進程模式設置 true 后臺運行 'daemonize' => false, // 設置啟動的reactor線程數 'reactor_num' => swoole_cpu_num(), // 設置啟動的worker進程數 'worker_num' => swoole_cpu_num(), //配置Task進程的數量 'task_worker_num' => swoole_cpu_num(), //開啟靜態文件請求處理,需配合document_root 'enable_static_handler' => true, //靜態文件根目錄 'document_root' => root_path('public'), // 設置最大數據包尺寸,單位字節 'package_max_length' => 20 * 1024 * 1024, //配置發送輸出緩沖區內存尺寸 'buffer_output_size' => 10 * 1024 * 1024, //設置客戶端連接最大允許占用的內存數量 'socket_buffer_size' => 128 * 1024 * 1024, ], ],
熱更新
swoole服務器運行過程中php文件是常駐內存運行,這樣就可以避免重復的讀取磁盤,重復的解釋編譯php,以便達到最高的性能,所以修改代碼需要重啟服務
think-swoole擴展提供熱更新功能,在檢測相關文件有更新會自動重啟,不在需要手動完成重啟,方便開發調試
生產環境下不建議開始文件監控,性能損耗,正常情況下你所修改的文件需要確認無誤才能進行更新部署
.env里面設置APP_DEBUG = true會默認開啟熱更新
'hot_update' => [ 'enable' => env('APP_DEBUG', false), 'name' => ['*.php'], 'include' => [app_path()], 'exclude' => [], ],
參數說明
參數 | 說明 |
---|---|
enable | 是否開啟熱更新 |
name | 監聽哪些類型的文件變動 |
include | 監聽哪些目錄下的文件變動 |
exclude | 排除目錄 |
websocket
先來一個官方的例子
$server = new SwooleWebSocketServer("0.0.0.0", 9501); $server->on('open', function (SwooleWebSocketServer $server, $request) { echo "server: handshake success with fd{$request->fd}n"; }); $server->on('message', function (SwooleWebSocketServer $server, $frame) { echo "receive from {$frame->fd}:{$frame->data}n"; $server->push($frame->fd, "this is server"); }); $server->on('close', function ($ser, $fd) { echo "client {$fd} closedn"; }); $server->start();
開啟think-swoole的websocket功能 configswoole.php
'websocket' => [ 'enable' => true, ],
創建三個事件
php think make:listener SwWsConnect php think make:listener SwWsClose php think make:listener SwWsMessage
然后將這三個事件寫到到事件監聽中,分別有以下2中文件可以修改方式,注意二選一
thinkphp6自帶的事件綁定appevent.php
'listen' => [ ........ // 監聽鏈接 'swoole.websocket.Connect' => [ applistenerSwWsConnect::class ], //關閉連接 'swoole.websocket.Close' => [ applistenerSwWsClose::class ], //發送消息場景 'swoole.websocket.Message' => [ applistenerSwWsMessage::class ] ],
think-swoole事件綁定configswoole.php
'listen' => [ 'connect'=>applistenerSwWsConnect::class, 'close'=>applistenerSwWsClose::class, 'message'=> applistenerSwWsMessage::class ],
怎么選擇是保存在configswoole.php還是appevent.php配置中呢?首先我們 我們確定一下我們這個項目中存在有幾個實時通訊,如果只是存在一個實時通訊 個人建議 保存在configswoole.php如果是存在多個實時通訊,就保存在appevent.phpkey值 必須是swoole.websocket.事件名稱 例如 swoole.websocket.Message
開始寫事件中中方法
連接事件applistenerSwWsConnect.php
public function handle($event, thinkswoolewebsocket $ws) { // 獲取當前發送者的fd $fd = $ws->getSender(); echo "server: handshake success with fd{$fd}n"; }
關閉事件applistenerSwWsClose.php
public function handle($event, thinkswoolewebsocket $ws) { $fd = $ws->getSender(); echo "client {$fd} closedn"; }
message事件applistenerSwWsMessage.php
public function handle($event, thinkswoolewebsocket $ws) { $fd = $ws->getSender(); $data = json_encode($event); echo "receive from {$fd}:{$data}n"; $ws->emit("this is server", $fd); }
啟動php think swoole進行測試
think-swoole中的websocket方法總結
//給自己發消息 $ws->emit("this is server", $ws->getSender()); //給指定一個fd發消息 $ws->to($to)->emit("messagecallback",$data); //給指定多個人發消息 $ws->to([1,2,3])->emit("messagecallback",$data); //發送給所有的(不包含自己) $ws->broadcast()->emit("messagecallback",$data); //模擬formfd 給tofd 發送消息 $ws->setSender($formfd)->to($tofd)->emit("messagecallback",$data);
注意:在多個實時通訊場景下使用 emit
第一個參數傳入 ?傳入 事件名稱callback 例如 messagecallback
如果你發現你think-swoole中有些沒有swoole中的方法可以這么干
$sw = app('swoole.server'); $sw = app("thinkswooleManager")->getServer(); //以上二選一 $es = $sw->isEstablished($fd); //檢查連接是否為有效的WebSocket客戶端連接 var_dump($es);
聊天室room實現
前端文件參考 htmlroom.html 或 htmlroom-socket-io.html
php think make:listener SwRoomJoin php think make:listener SwRoomLeave php think make:listener SwRoomMessage
事件綁定
// 加入房間 'swoole.websocket.RoomJoin' => [ applistenerSwRoomJoin::class ], // 離開房間 'swoole.websocket.Roomleave' => [ applistenerSwRoomLeave::class ], // 在房間發消息 'swoole.websocket.RoomMessage' => [ applistenerSwRoomMessage::class ]
加入房間邏輯
public function handle($event, thinkswoolewebsocket $ws, thinkswoolewebsocketroom $room) { $fd = $ws->getSender(); //客戶端假如定的room $roomid = $event['room']; //獲取指定房間下有哪些客戶端 $roomfds = $room->getClients($roomid); // 判斷這個房間有沒有自己 如果有自己就不需要再次發送通知 if (in_array($fd, $roomfds)) { $ws->to($roomfds)->emit("roomjoincallback", "房間{$roomid}已加入"); return; } //加入房間 $ws->join($roomid); $ws->to($roomfds)->emit("roomjoincallback", "{$fd}加入房間{$roomid}成功"); }
離開房間邏輯
public function handle($event, thinkswoolewebsocket $ws, thinkswoolewebsocketRoom $room) { $roomid = $event['room']; $fd = $ws->getSender(); $roomfds = $room->getClients($roomid); if (!in_array($fd, $roomfds)) { $ws->emit("roomleavecallback", "{$fd}不在{$roomid}房間內,怎么離開~"); return; } //離開房間 $ws->leave($roomid); //獲取當前客戶端加入了哪些客戶端 $rooms = $room->getRooms($fd); $ws->to($roomfds)->emit("roomleavecallback", "{$fd}已離開了~~"); }
在房間發布聊天邏輯
public function handle($event, thinkswoolewebsocket $ws, thinkswoolewebsocketroom $room) { // $roomid = $event['room']; $text = $event['text']; $fd = $ws->getSender(); $roomfds = $room->getClients($roomid); if (!in_array($fd, $roomfds)) { $ws->emit("roommessagecallback", "{$fd}不在{$roomid}房間內,無法進入發布聊天~"); return; } $ws->to($roomfds)->emit("roommessagecallback", $text); }
事件訂閱
php think make:listener SwSubscribe
applistenerSwSubscribe.php
<?php declare (strict_types = 1); namespace applistener; class SwSubscribe { protected $ws = null; // public function __construct() // { // $this->ws = app('thinkswooleWebsocket'); // } public function __construct(thinkContainer $c) { $this->ws = $c->make(thinkswooleWebsocket::class); } public function onConnect() { $fd = $this->ws->getSender(); echo "server: handshake success with fd{$fd}n"; } public function onClose() { $fd = $this->ws->getSender(); echo "client {$fd} closedn"; } public function onMessage($event) { $fd = $this->ws->getSender(); var_dump($event); echo "server: handshake success with fd{$fd}n"; $this->ws->emit("this is server", $fd); } }
有點類似 將原生的swoole代碼改成面向對象代碼,生效方法 configswoole.php中在subscribe 加入applistenerSwSubscribe::class
'subscribe' => [ applistenerSwSubscribe::class ],在appevent.php文件中的 swoole.websocket.Connect 相當于 applistenerSwSubscribe.php文件中的onConnect函數。如果同時存在的存在的話,就會向客戶端發送2次以上的消息
Task任務投遞
https://wiki.swoole.com/#/start/start_task
生成事件
php think make:listener SwSendEmailTask
編寫發送郵件方法applistenerSwSendEmailTask.php
public function handle($event) { var_dump($event); // echo "開發發送郵件".time(); sleep(3); echo "結束發送郵件".time(); }
注冊事件appevent.php
'swoole.task'=>[ applistenerSwSendEmailTask::class ],
在控制器中投遞任務
public function doRegister() { $server = app('swoole.server'); $server->task(applistenerSwSendEmailTask::class); return "注冊成功"; } public function doRegister(thinkswooleManager $manager) { $server = $manager->getServer(); $server->task(applistenerSwSendEmailTask::class); return "注冊成功"; } public function doRegister(SwooleServer $server) { $server->task(applistenerSwSendEmailTask::class); return "注冊成功"; }
三種獲取SwooleServer,任意選其一
在swoole中還有一個事件叫finish,它的作用就是把異步任務的結果返回,在think-swool是這么處理的
定義一個發送郵件異步任務處理結果的事件
php think make:listener SwSendEmailFinish
注冊事件appevent.php
'swoole.finish'=>[ applistenerSwSendEmailFinish::class ],
在task任務中調用
public function handle($event) { var_dump($event); // echo "開發發送郵件".time(); sleep(3); echo "結束發送郵件".time(); $event->finish(applistenerSwSendEmailFinish::class); }
高性能共享內存 Table
https://wiki.swoole.com/#/mem…
先定結構在進行操作數據(原生swoole操作)
$table = new SwooleTable(1024); //創建表 $table->column("id", SwooleTable::TYPE_INT); $table->column("name", SwooleTable::TYPE_STRING); $table->column("money", SwooleTable::TYPE_FLOAT); $table->create(); //添加數據 $table->set("zq", [ 'id' => 1, 'name' => "zhiqiang", 'money' => 100, ]); //獲取一行數據 $table->get("zq"); // 修改數據 // 字段遞增 $table->incr("zq","money",2); //遞減 $table->decr("zq","money",2); // 返回 table 中存在的條目數。 $table->count(); //遍歷table中的數據 foreach($table as $item){ var_dump($item); }
think-swoole中的操作
先對table表結構進行初始化configswoole.php
'tables' => [ 'user'=>[ 'size'=>1024, 'columns'=>[ [ 'name'=>'id', 'type'=>SwooleTable::TYPE_INT ], [ 'name'=>'name', 'type'=>SwooleTable::TYPE_STRING, 'size'=>32 ], [ 'name'=>'money', 'type'=>SwooleTable::TYPE_FLOAT ], ], ], ],
操作數據
$table = app('swoole.table.user'); $table->set("zq", [ 'id' => 1, 'name' => "zhiqiang", 'money' => 100 ]); //獲取一行數據 $table->get("zq"); // 修改數據 // 字段遞增 $table->incr("zq", "money", 2); //遞減 $table->decr("zq", "money", 2); // 返回 table 中存在的條目數。 $table->count(); //遍歷table中的數據 foreach ($table as $item) { var_dump($item); } // 檢查 table 中是否存在某一個 key。 $table->exist('zq'); //獲取實際占用內存尺寸,單位字節 $table->momorySize();
RPC
RPC(Remote Procedure Call):遠程過程調用,它是一種通過網絡從遠程計算機程序上請求服務,而不需要了解底層網絡技術的思想。
詳細介紹:https://developer.51cto.com/a…
- 解決分布式系統中,服務之間的調用問題。
- 遠程調用時,要能夠像本地調用一樣方便,讓調用者感知不到遠程調用的邏輯。
- 節點角色說明:
- Server: 暴露服務的服務提供方
- Client: 調用遠程服務的服務消費方
- Registry: 服務注冊與發現的注冊中心
think-swoole實現RPC功能
服務器端
接口定義app/rpc/interfaces/UserInterface.php
<?php namespace apprpcinterfaces; interface UserInterface { public function create(); public function find(int $id); }
實現接口app/rpc/services/UserService.php
<?php namespace apprpcservices; use apprpcinterfacesUserInterface; class UserService implements UserInterface { public function create() { // TODO: Implement create() method. return "service create success"; } public function find(int $id) { // TODO: Implement find() method. return $id. "查詢數據遍歷"; } }
注冊rpc服務config/swoole.php
'rpc' => [ 'server' => [ //開啟rpc服務 'enable' => true, //rpc端口 'port' => 9000, 'services' => [ //注冊服務 apprpcservicesUserService::class ], ], // 如果填寫也是可以調用其他服務端 'client' => [ ], ],
啟動服務端
php think swoole start / php think swoole:rpc
客戶端
'rpc' => [ 'server' => [ ], 'client' => [ 'tp6'=>[ //服務端的ip地址 'host'=>'127.0.0.1', //服務端對應的端口 'port'=>'9000' ] // 更多服務端 ], ],
運行php think rpc:interface生成RPC接口文件apprpc.php
<?php /** * This file is auto-generated. */ declare(strict_types=1); namespace rpccontracttp6; interface UserInterface { public function create(); public function find(int $id); } return ['tp6' => ['rpccontracttp6UserInterface']];
在控制器調用
public function index(rpccontracttp6UserInterface $user) { // $user->find(1); // $user->create(); }
定時任務
在think-swoole 2.0版本的時候還是支持自定義定時任務配置,詳細參考https://github.com/top-think/think-swoole/tree/2.0
在3.0就不支持了,在這里介紹一個通用的命令行啟動定時任務
php think make:command SwooleTimer
加載命令行config/console.php
'commands' => [ 'swooletimer'=>appcommandSwooleTimer::class ........... ],
書寫命令腳本app/command/SwooleTimer.php
<?php declare (strict_types = 1); namespace appcommand; use thinkconsoleCommand; use thinkconsoleinputArgument; class SwooleTimer extends Command { protected function configure() { // 指令配置 $this->setName('appcommandswooletimer') ->addArgument('action', Argument::OPTIONAL, "start | stop", 'start') ->setDescription('Swoole Timer for ThinkPHP'); } public function handle() { $action = $this->input->getArgument('action'); if (in_array($action, ['start','stopall'])) { $this->app->invokeMethod([$this, $action], [], true); } else { $this->output->writeln("<error>Invalid argument action:{$action}, Expected start</error>"); } } /** * 啟動定時任務 主要任務計劃在這里書寫 */ protected function start() { // https://wiki.swoole.com/#/timer $timer_id=swoole_timer_tick(2000,function (){ echo "2s循環執行需要做的事情".time()."n"; }); $this->output->writeln("Swoole Timer_id:{$timer_id} "); } /** * 清除所有的定時任務 */ protected function stop(){ swoole_timer_clear_all(); $this->output->writeln("Swoole Timer clear all ok"); } }