## **一,HTTP服務器創建**
```
<?php
// +----------------------------------------------------------------------
// | najing [ 通用后臺管理系統 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火隊隊長
// +----------------------------------------------------------------------
namespace server;
/**
* 概要描述:HTTP服務器
* @author: 救火隊隊長
* @since: 2020-05-23 22:45
*/
class HttpServer
{
protected $serv = null; //Swoole\Server對象
protected $host = '0.0.0.0'; //監聽對應外網的IP 0.0.0.0監聽所有ip
protected $port = 9603; //監聽端口號
public function __construct()
{
$this->serv = new \Swoole\Http\Server($this->host, $this->port);
//設置參數
//如果業務代碼是全異步 IO 的,worker_num設置為 CPU 核數的 1-4 倍最合理
//如果業務代碼為同步 IO,worker_num需要根據請求響應時間和系統負載來調整,例如:100-500
//假設每個進程占用 40M 內存,100 個進程就需要占用 4G 內存
$this->serv->set(array(
'worker_num' => 4, //設置啟動的worker進程數。【默認值:CPU 核數】
'max_request' => 10000, //設置每個worker進程的最大任務數。【默認值:0 即不會退出進程】
'daemonize' => 0, //開啟守護進程化【默認值:0】
));
//監聽服務器啟動事件
$this->serv->on('start', function ($server) {
echo "Swoole http server is started";
});
//監聽請求,HTTP服務器只需要關注請求響應即可
//當有新的 HTTP 請求進入就會觸發此事件
//$request 對象,包含了請求的相關信息
//$response 對象,對 request 的響應可以通過操作 response 對象來完成。
//$response->end() 方法表示輸出一段 HTML 內容,并結束此請求
$this->serv->on('request', function ($request, $response) {
// 使用 Chrome 瀏覽器訪問服務器,會產生額外的一次請求,/favicon.ico,可以在代碼中響應 404 錯誤。
if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
$response->end();
return;
}
//打印請求的header
var_dump($request->header);
//打印請求的get參數
var_dump($request->get);
//輸出返回內容
$response->header("Content-Type", "text/html; charset=utf-8");
$response->end("我是Swoole HTTP服務器輸出的返回內容\n");
});
//啟動服務
$this->serv->start();
}
}
$httpServer = new HttpServer();
```
## **二,運行HTTP服務**
>[danger] **需要用php CLI模式運行**

## **三,訪問HTTP服務**
瀏覽器訪問應用地址,會輸出HTTP服務返回的內容

HTTP服務端會打印客戶端request的數據,并輸出響應內容

## **四,URL路由**
應用程序可以根據`$request->server['request_uri']`實現路由。如:`http://najingquan.com:9603/test/index/?a=1`,代碼中可以這樣實現`URL`路由。
```
<?php
// +----------------------------------------------------------------------
// | najing [ 通用后臺管理系統 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火隊隊長
// +----------------------------------------------------------------------
namespace server;
/**
* 概要描述:HTTP服務器
* @author: 救火隊隊長
* @since: 2020-05-23 22:45
*/
class HttpServer
{
protected $serv = null; //Swoole\Server對象
protected $host = '0.0.0.0'; //監聽對應外網的IP 0.0.0.0監聽所有ip
protected $port = 9603; //監聽端口號
public function __construct()
{
$this->serv = new \Swoole\Http\Server($this->host, $this->port);
//設置參數
//如果業務代碼是全異步 IO 的,worker_num設置為 CPU 核數的 1-4 倍最合理
//如果業務代碼為同步 IO,worker_num需要根據請求響應時間和系統負載來調整,例如:100-500
//假設每個進程占用 40M 內存,100 個進程就需要占用 4G 內存
$this->serv->set(array(
'worker_num' => 4, //設置啟動的worker進程數。【默認值:CPU 核數】
'max_request' => 10000, //設置每個worker進程的最大任務數。【默認值:0 即不會退出進程】
'daemonize' => 0, //開啟守護進程化【默認值:0】
));
//監聽服務器啟動事件
$this->serv->on('start', function ($server) {
echo "Swoole http server is started";
});
//監聽請求,HTTP服務器只需要關注請求響應即可
//當有新的 HTTP 請求進入就會觸發此事件
//$request 對象,包含了請求的相關信息
//$response 對象,對 request 的響應可以通過操作 response 對象來完成。
//$response->end() 方法表示輸出一段 HTML 內容,并結束此請求
$this->serv->on('request', function ($request, $response) {
// 使用 Chrome 瀏覽器訪問服務器,會產生額外的一次請求,/favicon.ico,可以在代碼中響應 404 錯誤。
if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
$response->end();
return;
}
//打印請求的header
var_dump($request->header);
//打印請求的get參數
var_dump($request->get);
//輸出返回內容
$response->header("Content-Type", "text/html; charset=utf-8");
list($controller, $action) = explode('/', trim($request->server['request_uri'], '/'));
//根據 $controller, $action 映射到不同的控制器類和方法
// 控制器首字母大寫
$controller = ucfirst($controller);
//判斷控制器類是否存在
if (file_exists(__DIR__.'/'.str_replace('\\', '/', $controller).'.php')) {
require_once __DIR__.'/'.str_replace('\\', '/', $controller).'.php';
$obj= new $controller;
//判斷控制器方法是否存在
if (!method_exists($obj, $action)) {
$response->status(404);
$response->end("<meta charset='UTF-8'>兄弟,方法不存在!");
} else {
//如果存在此方法,輸出結果
$response->end($obj->$action($request));
}
} else {
$response->status(404);
$response->end("<meta charset='UTF-8'>兄弟,方法不存在!");
}
});
//啟動服務
$this->serv->start();
}
}
$httpServer = new HttpServer();
```
## **五、創建Test控制器**
```
<?php
// +----------------------------------------------------------------------
// | najing [ 通用后臺管理系統 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火隊隊長
// +----------------------------------------------------------------------
/**
* 概要描述:Test控制器
* @author: 救火隊隊長
* @since: 2020-05-23 22:45
*/
class Test
{
public function index($request)
{
//返回請求結果
return "<h1>Hello Swoole. #".rand(1000, 9999)."</h1>";
}
}
```
瀏覽器訪問應用地址,會輸出HTTP服務返回的內容

請求不存在的方法時,會提示方法不存在

- 安裝Swoole
- swoole基礎-TCP服務
- swoole基礎-UDP服務
- swoole基礎-HTTP服務
- swoole基礎-WebSocket服務
- swoole基礎-TASK異步任務
- swoole基礎-一鍵協程
- swoole基礎-協程 MySQL 客戶端
- swoole基礎-協程 Redis 客戶端
- swoole基礎-毫秒定時器
- swoole基礎-高性能內存操作table
- think-swoole應用-HTTP請求和熱更新
- think-swoole應用-進程設置
- think-swoole應用-啟用數據庫連接池
- think-swoole應用-異步TASK發送短信任務
- think-swoole應用-集成think-queue消息隊列,優化異步發短信任務,支持任務重試機制
- think-swoole應用-毫秒定時器取消超時訂單
- think-swoole應用-高性能共享內存table應用
- think-swoole應用-微服務之RPC遠程調用通信實戰
- think-swoole應用-websocket消息、群發廣播
- Nginx負載均衡部署-轉發swoole服務