在用过laravel
框架,发现它的路由
和数据库ORM
确实很是好用,可是总体确实有点慢,执行到控制器大于须要耗时60ms左右。因而打算作一个拥有很是好用的路由和orm又很是简单的框架。因此你会发现one框的路由
和ORM
有laravel的影子。但也有一些本身的特点,例如ORM
支持自动化缓存(自动化读、写、刷新)保持与数据库同步,对外使用无感知。one框架也支持在fpm下运行,在fpm
下框架自身总体耗时在1ms左右。php
安装mysql
composer create-project lizhichao/one-app app cd app php App/swoole.php
测试laravel
curl http://127.0.0.1:8081/
Router::get('/', \App\Controllers\IndexController::class . '@index'); // 带参数路由 Router::get('/user/{id}', \App\Controllers\IndexController::class . '@user'); // 路由分组 Router::group(['namespace'=>'App\\Test\\WebSocket'],function (){ // websocket 路由 Router::set('ws','/a','TestController@abc'); Router::set('ws','/b','TestController@bbb'); }); // 中间件 Router::group([ 'middle' => [ \App\Test\MixPro\TestMiddle::class . '@checkSession' ] ], function () { Router::get('/mix/ws', HttpController::class . '@ws'); Router::get('/mix/http', HttpController::class . '@http'); Router::post('/mix/http/loop', HttpController::class . '@httpLoop'); Router::post('/mix/http/send', HttpController::class . '@httpSend'); });
namespace App\Model; use One\Database\Mysql\Model; // 模型里面不须要指定主键,框架会缓存数据库结构 // 自动匹配主键,自动过滤非表结构里的字段 class User extends Model { // 定义模型对应的表名 CONST TABLE = 'users'; // 定义关系 public function articles() { return $this->hasMany('id',Article::class,'user_id'); } // 定义事件 // 是否开启自动化缓存 // …… }
在fpm
下数据库链接为单列,
在swoole
模式下数据库链接自动切换为链接池git
// 查询一条记录 $user = User::find(1); // 关联查询 $user_list = User::whereIn('id',[1,2,3])->with('articles')->findAll()->toArray(); // 更新 $r = $user->update(['name' => 'aaa']); // 或者 $r = user::where('id',1)->update(['name' => 'aaa']); // $r 为影响记录数量
// 设置缓存 Cache::set('ccc',1); // 获取 Cache::get('ccc'); // 或者 缓存ccc 过时10s 在tag1下面 Cache::get('ccc',function (){ return '缓存的信息'; },10,['tag1']); // 刷新tag1下的全部缓存 Cache::flush('tag1');
启动一个websocket服务器,
添加http服务监听,
添加tcp服务监听github
[ // 主服务器 'server' => [ 'server_type' => \One\Swoole\OneServer::SWOOLE_WEBSOCKET_SERVER, 'port' => 8082, // 事件回调 'action' => \One\Swoole\Server\WsServer::class, 'mode' => SWOOLE_PROCESS, 'sock_type' => SWOOLE_SOCK_TCP, 'ip' => '0.0.0.0', // swoole 服务器设置参数 'set' => [ 'worker_num' => 5 ] ], // 添加监听 'add_listener' => [ [ 'port' => 8081, // 事件回调 'action' => \App\Server\AppHttpPort::class, 'type' => SWOOLE_SOCK_TCP, 'ip' => '0.0.0.0', // 给监听设置参数 'set' => [ 'open_http_protocol' => true, 'open_websocket_protocol' => false ] ], [ 'port' => 8083, // 打包 解包协议 'pack_protocol' => \One\Protocol\Text::class, // 事件回调 'action' => \App\Test\MixPro\TcpPort::class, 'type' => SWOOLE_SOCK_TCP, 'ip' => '0.0.0.0', // 给监听设置参数 'set' => [ 'open_http_protocol' => false, 'open_websocket_protocol' => false ] ] ] ];
像调用本项目的方法同样调用远程服务器的方法。跨语言,跨机器。web
启动rpc服务,框架已经内置了各个协议的rpc服务,添加到到上面配置文件的action
便可。列如: 支持http
调用,又支持tpc
调用。redis
// http 协议 rpc服务 [ 'port' => 8082, 'action' => \App\Server\RpcHttpPort::class, 'type' => SWOOLE_SOCK_TCP, 'ip' => '0.0.0.0', 'set' => [ 'open_http_protocol' => true, 'open_websocket_protocol' => false ] ], // tpc 协议 rpc服务 [ 'port' => 8083, 'action' => \App\Server\RpcTcpPort::class, 'type' => SWOOLE_SOCK_TCP, 'pack_protocol' => \One\Protocol\Frame::class, // tcp 打包 解包协议 'ip' => '0.0.0.0', 'set' => [ 'open_http_protocol' => false, 'open_websocket_protocol' => false, 'open_length_check' => 1, 'package_length_func' => '\One\Protocol\Frame::length', 'package_body_offset' => \One\Protocol\Frame::HEAD_LEN, ] ]
添加具体服务到rpc,
例若有个类Abc
sql
class Abc { private $a; // 初始值 public function __construct($a = 0) { $this->a = $a; } // 加法 public function add($a, $b) { return $this->a + $a + $b; } public function time() { return date('Y-m-d H:i:s'); } // 从新设初始值 public function setA($a) { $this->a = $a; return $this; } }
把Abc
添加到rpc服务shell
// 添加Abc到rpc服务 RpcServer::add(Abc::class); // 若是你不但愿把Abc下的全部方法都添加到rpc服务,也能够指定添加。 // 未指定的方法客户端没法调用. //RpcServer::add(Abc::class,'add'); // 分组添加 //RpcServer::group([ // // 中间件 在这里能够作 权限验证 数据加解密 等等 // 'middle' => [ // TestMiddle::class . '@aa' // ], // // 缓存 若是设置了 当以一样的参数调用时 会返回缓存信息 不会真正调用 单位:秒 // 'cache' => 10 //], function () { // RpcServer::add(Abc::class); // RpcServer::add(User::class); //});
为了方便调用咱们创建一个映射类(one框架可自动生成)数据库
class ClientAbc extends RpcClientHttp { // rpc服务器地址 protected $_rpc_server = 'http://127.0.0.1:8082/'; // 远程的类 不设置 默认为当前类名 protected $_remote_class_name = 'Abc'; }
调用rpc服务的远程方法, 和调用本项目的方法同样的。你能够想象这个方法就在你的项目里面。
$abc = new ClientAbc(5); // $res === 10 $res = $abc->add(2,3); // 链式调用 $res === 105 $res = $abc->setA(100)->add(2,3); // 若是把上面的模型的User添加到rpc // RpcServer::add(User::class); // 下面运行结果和上面同样 // $user_list = User::whereIn('id',[1,2,3])->with('articles')->findAll()->toArray();
上面是经过http协议调用的。你也能够经过其余协议调用。例如Tpc协议
class ClientAbc extends RpcClientTcp { // rpc服务器地址 protected $_rpc_server = 'tcp://127.0.0.1:8083/'; // 远程的类 不设置 默认为当前类名 protected $_remote_class_name = 'Abc'; }
其中类 RpcClientHttp
,RpcClientTcp
在框架里。
你也能够复制到任何其余地方使用。
QQ交流群: 731475644