App Server Framework (PHP Swoole)

联合创作 · 2023-09-29 12:37

App Server Framework (ASF)简介:

  • 当前版本0.01试用版。

  • 框架基于PHP-Swoole扩展开发,通过配置文件可以自定义各种应用协议,默认支持http协议。

  • 框架本身是一个完整的tcp_server,不再需要apache,nginx,fpm这些,框架已包含log处理,mysql访问封装。

  • 框架用fast-route库来做http route处理,直接映射到控制器上,使用者只要写具体的控制器方法就可以实现rest风格的API。

  • 至于性能,可以很低调的说:相当高,具体可以参考swoole相关文档: http://www.swoole.com/

安装运行

环境: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  

也可以直接进入具体server目录直接运行入口脚本文件:  
cd asf/apps/test_http
php ./index.php

查看当前server进程状态:
php asf/bin/asf.php test_http status

查看所有server运行状态:
php asf/bin/asf.php list

http_server开发

当protocol为http(不设置则默认为http),server运行为http_server,这种模式下默认不需要做任何额外的配置,系统会按默认的路由规则分发到具体的控制器中处理,开发者只需要写具体的控制器和方法就可以。

下面是http_server,test_http的开发流程:

  • 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监听端口
        '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实现,而base_controller必须基于lib/controller.php的controller实现。

  • 在这种默认的配置下:访问 http://localhost:9501/index/index 路由将会执行上面index_controller控制器中的index方法,http调用返回的结果是:ok

=>
=>
浏览 1
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报