All Projects → lizhichao → One

lizhichao / One

Licence: apache-2.0
一个极简高性能php框架,支持[swoole | php-fpm ]环境

Projects that are alternatives of or similar to One

Hyperf
🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.
Stars: ✭ 4,206 (+433.08%)
Mutual labels:  framework, swoole
Ubiquity
Ubiquity framework
Stars: ✭ 480 (-39.16%)
Mutual labels:  orm, framework
Ozzo Routing
An extremely fast Go (golang) HTTP router that supports regular expression route matching. Comes with full support for building RESTful APIs.
Stars: ✭ 398 (-49.56%)
Mutual labels:  framework, router
Queryphp
🔥High Performance PHP Progressive Framework.
Stars: ✭ 299 (-62.1%)
Mutual labels:  framework, swoole
Latke
🌀 一款以 JSON 为主的 Java Web 框架。
Stars: ✭ 781 (-1.01%)
Mutual labels:  orm, framework
Lawoole
Lawoole is a high-performance PHP framework based on Laravel and Swoole
Stars: ✭ 300 (-61.98%)
Mutual labels:  framework, swoole
Gearbox
Gearbox ⚙️ is a web framework written in Go with a focus on high performance
Stars: ✭ 455 (-42.33%)
Mutual labels:  framework, router
Django
The Web framework for perfectionists with deadlines.
Stars: ✭ 61,277 (+7666.41%)
Mutual labels:  orm, framework
Phpboot
☕️ 🚀 tiny & fast PHP framework for building Microservices/RESTful APIs, with useful features: IOC, Hook, ORM, RPC, Swagger, Annotation, Parameters binding, Validation, etc.
Stars: ✭ 638 (-19.14%)
Mutual labels:  orm, framework
Gf
GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
Stars: ✭ 6,501 (+723.95%)
Mutual labels:  orm, framework
Framework
Swoole, PSR-15, PSR-7, PSR-11 lightweight modular anti-framework for REST micro-services.
Stars: ✭ 259 (-67.17%)
Mutual labels:  framework, swoole
Opulence
A simple, secure, and scalable PHP application framework
Stars: ✭ 723 (-8.37%)
Mutual labels:  orm, framework
Zen
zen is a elegant and lightweight web framework for Go
Stars: ✭ 257 (-67.43%)
Mutual labels:  framework, router
Routing Controllers
Create structured, declarative and beautifully organized class-based controllers with heavy decorators usage in Express / Koa using TypeScript and Routing Controllers Framework.
Stars: ✭ 3,557 (+350.82%)
Mutual labels:  framework, router
gene
Grace, fastest, flexibility, simple PHP extension framework!优雅、极速、灵活、简单的PHP扩展框架!
Stars: ✭ 30 (-96.2%)
Mutual labels:  orm, swoole
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+2635.99%)
Mutual labels:  framework, router
Framework
ThinkPHP Framework
Stars: ✭ 2,399 (+204.06%)
Mutual labels:  orm, framework
Aqueduct
Dart HTTP server framework for building REST APIs. Includes PostgreSQL ORM and OAuth2 provider.
Stars: ✭ 2,412 (+205.7%)
Mutual labels:  orm, framework
Gear
A lightweight, composable and high performance web service framework for Go.
Stars: ✭ 544 (-31.05%)
Mutual labels:  framework, router
Rapid.js
An ORM-like Interface and a Router For Your API Requests
Stars: ✭ 700 (-11.28%)
Mutual labels:  orm, router

One - 一个极简高性能php框架,支持[swoole | php-fpm ]环境

  • 简单 - 让你重点关心用one做什么,而不是怎么用one
  • 灵活 - 各个组件松耦合,可以灵活搭配使用,使用方法保持一致
    • 原生sql可以和模型关系with搭配使用,关系可以跨数据库类型
    • session 可以在http,websocket甚至tcp,udp和cli下使用
    • ...
  • 高效 - 运行性能,开发效率,易维护。
  • 轻量 - 无其他依赖,从路由、orm所有组件代码量一共不超过500k,若二次开发没有复杂的调用关系,可快速掌握设计原理

QQ交流群:731475644

珍爱生命,抵制996 LICENSE

hello world

安装

composer create-project lizhichao/one-app app
cd app
php App/swoole.php 

# 停止 : `php App/swoole.php -o stop`  
# 重启 : `php App/swoole.php -o reload`  
# 守护进程启动 : `php App/swoole.php -o start`  

curl http://127.0.0.1:8081/

性能

参考:

主要组件

  • 路由

    • 支持贪婪匹配和优先级
    • 支持ws/tcp/http……任意协议
    • 性能好,添加几万条路由也不会降低解析性能
    • 路由分组,中间件……该有的都有
  • ORM模型

    • 支持数据库:mysql,clickHouse,
    • 关系处理:一对一,一对多,多对一,多态…… 各种关系的都有,可以跨数据库类型关联
    • 缓存:自动刷新数据 支持配置各种缓存粒度
    • 事件:所有操作都能捕获 包括你用原生sql操作数据库
    • 数据库连接:同步、异步、阻塞、断线重连都支持
    • sql模板: 自动生成模板id,可了解项目有哪些类型sql,以及调用次数占比情况,对后期数据优化提供数据支持。
    • statement复用:提供sql执行性能
  • rpc

    • 可自动生成远程方法映射,支持ide提示
    • 直接调用映射方法 == 调用远程方法,支持链式调用
    • 支持rpc中间件,鉴权、加解密、缓存……
  • 日志

    • 信息完整:记录完整的文件名+行号可快速定位代码位置
    • requestId:可轻松查看整个请求日志信息和服务关系
  • ...

    • QQ交流群: 731475644

路由

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','[email protected]'); 
    Router::set('ws','/b','[email protected]'); 
});

// 中间件
Router::group([
    'middle' => [
        \App\Test\MixPro\TestMiddle::class . '@checkSession'
    ]
], function () {
    Router::get('/mix/ws', HttpController::class . '@ws');
    Router::get('/user/{id}', \App\Controllers\IndexController::class . '@user');
    Router::post('/mix/http/send', HttpController::class . '@httpSend');
});

orm 模型

定义模型

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模式下所有数据库操作自动切换为连接池
// 查询一条记录
$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);

// 设置缓存 1分钟过期
Cache::set('ccc',1,60);

// 获取
Cache::get('ccc');

// 或者 缓存ccc 过期10s 在tag1下面
Cache::get('ccc',function (){
    return '缓存的信息';
},10,['tag1']);

// 刷新tag1下的所有缓存
Cache::flush('tag1');

HTTP/TCP/WEBSOCKET/UDP服务器

启动一个websocket服务器, 添加http服务监听, 添加tcp服务监听

[
	 // 主服务器
    '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
            ]
        ]
    ]
];


RPC

像调用本项目的方法一样调用远程服务器的方法。跨语言,跨机器。

服务端

启动rpc服务,框架已经内置了各个协议的rpc服务,添加到到上面配置文件的action即可。列如: 支持http调用,又支持tcp调用。

// 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
    ]
],
// tcp 协议 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

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服务

// 添加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协议调用的。你也可以通过其他协议调用。例如Tcp协议

class ClientAbc extends RpcClientTcp {

    // rpc服务器地址
    protected $_rpc_server = 'tcp://127.0.0.1:8083/';

    // 远程的类 不设置 默认为当前类名
    protected $_remote_class_name = 'Abc';
}

其中类 RpcClientHttp,RpcClientTcp在框架里。
你也可以复制到任何其他地方使用。

更多

文档

TODO

QQ交流群: 731475644

我的其他开源项目

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].