# 中間件
SD框架引入了中間件過程,消息的傳遞流程如下。
message->pack->middleware1(before)->middleware2(before)->...->route->controller->...->middleware2(after)->middleware1(after)
## SD自帶的中間件
### Middleware
中間件最基礎的類,開發中間件繼承Middleware。
* before_handle 中間件before過程
* after_handle 中間件after過程
* interrupt 中斷中間件
### HttpMiddleware
http使用的基礎中間件。一般開發http中間件請繼承HttpMiddleware,提供了常用的命令。
### NormalHttpMiddleware
這個中間件是提供給Http使用的,它具備的功能是提供了默認主頁,404頁面,文件后綴查詢。
```php
$config['ports'][] = [
'socket_type' => PortManager::SOCK_HTTP,
'socket_name' => '0.0.0.0',
'socket_port' => 8081,
'route_tool' => 'NormalRoute',
'middlewares' => ['MonitorMiddleware', 'NormalHttpMiddleware']
];
```
在ports配置中請務必攜帶NormalHttpMiddleware。
### MonitorMiddleware
效率中間件,將記錄接口的運行時間,并寫入日志。
## 配置
中間件作用于每一個端口配置,也就是說不同的端口可以單獨配置中間件。
中間件的執行嚴格按照數據的順序執行。
```php
$config['ports'][] = [
'socket_type' => PortManager::SOCK_HTTP,
'socket_name' => '0.0.0.0',
'socket_port' => 8081,
'route_tool' => 'NormalRoute',
'middlewares' => ['MonitorMiddleware', 'NormalHttpMiddleware']
];
```
如上圖執行順序為MonitorMiddleware(before)->NormalHttpMiddleware(before)->...->NormalHttpMiddleware(after)->MonitorMiddleware(after).
## 注意
before流程中只要有一個中間件調用了interrupt方法,那么后續的中間件都不會被執行。
after流程中會忽略interrupt。
中間件的after_handle,before_handle方法中均支持協程。
- Introduction
- SD 3.X文檔連接
- 導言
- 用戶案例
- 基于Swoole擴展分布式全棧開發框架
- 選擇SD框架助力企業開發
- 捐贈SwooleDistributed項目
- 框架性能報告
- 更新日志
- VIP服務福利
- 安裝與配置
- 【推薦】全自動安裝部署
- 環境要求
- 使用Composer安裝/更新SD框架
- 通過Docker安裝
- 代碼結構
- 啟動命令
- 服務器配置
- 服務器基礎配置server.php
- 客戶端協議配置client.php
- business.php
- log.php
- 微服務及集群配置consul.php
- fileHeader.php
- mysql.php
- redis.php
- 定時任務配置timerTask.php
- 服務器端口配置ports.php
- catCache.php
- 驗證服務啟動成功
- 微服務-Consul
- 日志工具-GrayLog
- 集群-Cluster
- 內核優化
- 入門教學
- 開發流程
- 開發前必讀
- 開發規范
- 基本流程
- 框架入口
- Model數據模型
- Controller控制器
- 協程
- 協程基礎
- 迭代器
- 調度器
- 使用協程的優勢
- 通過協程的方法屏蔽異步同步的區別
- Select多路選擇器
- 協程Sleep
- 通用協程方法
- 設置超時
- 設置無異常
- 設置降級函數
- initAsynPools
- dump
- 封裝器與路由器
- 封裝器
- sendToUid
- 路由器
- sendToUids
- 對象池
- 擴展組件
- 中間件
- Redis使用介紹
- RedisAsynPool
- Redis具體使用
- sendToAll
- RedisRoute
- Redis+Lua
- Mysql使用介紹
- MysqlAsynPool
- Mysql返回值
- 如何獲取構建的mysql語句
- 如何執行一個SQL
- 如何執行事務
- stopTask
- Mysql具體使用
- 異步客戶端
- Loader
- MqttClient
- model
- SdTcpRpcPool
- task
- HttpClientPool
- view
- TcpClientPool
- AMQP
- initialization
- Memory
- destory
- Cache
- Lock
- Pool
- EventDispatcher
- Process
- Cluster
- TimerTask
- Reload
- Consul
- Context
- 自定義進程
- 進程間RPC
- $http_input
- CatCache
- $http_output
- TimerCallBack
- 專題
- HTTP專欄
- TCP專欄
- 基礎知識
- WebSocket專欄
- 微服務
- Consul配置
- RPC
- REST
- AMQP異步任務系統
- MQTT簡易服務器
- Docker化以及資源編排
- 快速搭建公司內部統一的開發環境
- 使用HTTPS/WSS
- 訂閱/發布
- 游戲專題
- 類介紹
- AppServer
- clearState
- onOpenServiceInitialization
- SwooleDistributedServer
- get_instance
- kickUid
- bindUid
- unBindUid
- coroutineUidIsOnline
- coroutineCountOnline
- setTemplateEngine
- isWebSocket
- isTaskWorker
- getSocketName
- initAsynPools
- addAsynPool
- getAsynPool
- getServerAllTaskMessage
- Controller
- onExceptionHandle
- send
- sendToUid
- sendToUids
- sendToAll
- sendToGroup
- close
- getContext
- defaultMethod
- $redis_pool
- $mysql_pool
- $request_type
- $fd
- $uid
- $client_data
- $request
- $response
- $loader
- $logger
- $server
- $config
- Model
- initialization
- destory
- View
- Task
- stopTask
- HttpInput
- postGet
- post
- get
- getPost
- getAllPostGet
- getAllHeader
- getRawContent
- cookie
- getRequestHeader
- server信息
- getRequestMethod
- getRequestUri
- getPathInfo
- HttpOutput
- setStatusHeader
- setContentType
- setHeader
- end
- setCookie
- endFile
- 單元測試