App Server Framework (PHP Swoole)
App Server Framework (ASF)簡介:
-
當(dāng)前版本0.01試用版。
-
框架基于PHP-Swoole擴(kuò)展開發(fā),通過配置文件可以自定義各種應(yīng)用協(xié)議,默認(rèn)支持http協(xié)議。
-
框架本身是一個(gè)完整的tcp_server,不再需要apache,nginx,fpm這些,框架已包含log處理,mysql訪問封裝。
-
框架用fast-route庫來做http route處理,直接映射到控制器上,使用者只要寫具體的控制器方法就可以實(shí)現(xiàn)rest風(fēng)格的API。
-
至于性能,可以很低調(diào)的說:相當(dāng)高,具體可以參考swoole相關(guān)文檔: http://www.swoole.com/
安裝運(yùn)行
環(huán)境:linux2.6+、php5.5+、mysql5.5+、swoole1.7.20+
下載:https://github.com/xtjsxtj/asf
tar zxvf asf.tar.gz cd asf php ./bin/asf.php test_http start 也可以直接進(jìn)入具體server目錄直接運(yùn)行入口腳本文件: cd asf/apps/test_http php ./index.php 查看當(dāng)前server進(jìn)程狀態(tài): php asf/bin/asf.php test_http status 查看所有server運(yùn)行狀態(tài): php asf/bin/asf.php list
http_server開發(fā)
當(dāng)protocol為http(不設(shè)置則默認(rèn)為http),server運(yùn)行為http_server,這種模式下默認(rèn)不需要做任何額外的配置,系統(tǒng)會按默認(rèn)的路由規(guī)則分發(fā)到具體的控制器中處理,開發(fā)者只需要寫具體的控制器和方法就可以。
下面是http_server,test_http的開發(fā)流程:
-
server配置文件:apps/test_http/config/server_conf.php
<?php
class Swoole_conf {
public static $config=array(
'server_name' => 'test_http', //server名稱
'log_level' => NOTICE, //跟蹤級別
'listen' => 9501, //listen監(jiān)聽端口
'log_file' => '/asf/apps/test_http/index.log', //log文件
);
}
-
worker配置文件:apps/test_http/config/worker_conf.php
<?php
class Worker_conf{
public static $config=array(
'log_level' => DEBUG,
'mysql' => array(
'socket' => '/tmp/mysql.sock',
'host' => 'localhost',
'port' => 3306,
'user' => 'user',
'password' => 'password',
'database' => 'test',
'charset' => 'utf8',
),
}
-
唯一主入口腳本:apps/test_http/index.php
<?php>
define('BASE_PATH', __DIR__);
require_once BASE_PATH.'/../../lib/autoload.php';
$server = new swoole();
$server->start();
-
控制器:apps/test_http/controller/index_controller.php
<?php
class index_controller extends base_controller {
public function index() {
log::prn_log(DEBUG, json_encode($this->content));
log::prn_log(DEBUG, json_encode($this->param));
return 'ok';
}
}
-
controller基于父類base_controller實(shí)現(xiàn),而base_controller必須基于lib/controller.php的controller實(shí)現(xiàn)。
-
在這種默認(rèn)的配置下:訪問 http://localhost:9501/index/index 路由將會執(zhí)行上面index_controller控制器中的index方法,http調(diào)用返回的結(jié)果是:ok
