swoole 分布式全栈框架

Overview

SwooleDistributed

官网:http://sd.youwoxing.net 今年年底历时2年多的迭代,这是SD框架硕果满满的一年,通过不断的迭代和改进SD框架已经在圈内有良好的口碑,不少新生的框架借鉴了SD的设计思想,SD框架也被不少创业型公司和大型企业使用。

SDHelp

SDHELP是SD专属的开发者工具,可以实现断点调试,代码覆盖率报告等功能。 https://github.com/SwooleDistributed/SDHelper-Bin

SD框架到底是什么技术

SD框架全称SwooleDistributed,从名称上看一个是Swoole一个是Distributed,他是基于Swoole扩展的可以分布式部署的应用服务器框架。 借助于PHP的高效开发环境,Swoole的高性能异步网络通信引擎,以及其他的高可用的扩展和工具,SD框架提供给广大开发者一个稳定的高效的而且功能强大的应用服务器框架。

入门成本

老实的说相对比目前热门的FPM框架来说,SD的入门成本相对还是比较高的,因为设计理念不同以及和传统PHP-FPM环境完全不同的运行环境,对于长时间使用LAMP(LANP)技术的开发人员来说会有一段时间的适应期,如果开发应用简单涉及到的系统复杂度低,那么SD上手还是比较容易,根据简单的例子和文档几乎立即就能开启SD的探索之旅,但是如果开发的是复杂的应用那么SD包含的众多组件还是需要你慢慢熟悉上手的。

SD框架到底包含哪些强大的功能呢

我们这里列举下SD提供的各种各样的功能以及模块组件

  • 混合协议 SD框架支持长连接协议TCP,WebSocket,短连接协议HTTP,以及UDP。 通过配置开放不同的端口开发者可以轻松管理不同的协议,并且可以共用一套业务代码,当然你可以通过智能路由进行代码的隔离。 长连接可以配置不同的数据传输协议,比如二进制协议文本协议等等,通过框架提供的封装器解包器接口可以自定义各种各种的协议封装,并且各种协议之间可以自动转换,比如你通过广播发送一个信息,该信息流向不同客户端,客户端间采用不同协议,那么框架会根据不同的端口自动转换不同的协议封装。 你也可以通过Http给所有长连接客户端发送推送消息,类似这种混合协议协作的业务在SD框架上会异常简单。
  • MVC以及智能路由 框架的设计是MVC架构,其中每一个层级都可以继续划分子层级,开发者可以将Controller继续分层通过不同文件夹进行管理,也可以将Model进行细分,划分为业务层和数据层,这都看开发者自身的系统设计。智能路由将处理解包器解包后的数据,负责将这些数据传递到Controller层。
  • 中间件 SD框架还提供了中间件,中间件可以对流入的数据进行处理,比如清理异常数据,修改数据,流量统计,搜集日志等功能。中间件可以设置多个,他们和端口进行绑定。
  • 对象池 SD框架内大多数的对象都使用了对象池技术,对象池技术有利于系统内存的稳定,减少GC的次数,提高系统的运行效率,事实证明对象池对系统稳定做出了极大的贡献,开发者也可以使用这一套对象池技术,增加对对象的复用,减少GC和NEW的频率,对系统毛刺现象和内存泄露方面都有很大的稳定性提升。
  • 异步客户端以及连接池 Mysql,Redis,Http客户端,Tcp客户端,等等其他更为复杂的客户端,在SD框架中均为异步的模式,异步解决了系统整体的并发能力,但异步客户端需要提供连接池维持,SD框架提供了连接池,开发者不需要自己管理连接池,只需要使用即可。
  • 协程 异步事件回调解决的是并发性能,但造成的是业务代码的混乱。SD框架提供了协程解决了这一问题,通过yield关键字提供对异步的同步写法,消除了业务书写上的大量回调嵌套,你可以通过yield+同步的写法实现异步的性能。 协程提供了一整套完整的体系,包括超时,异常,休眠,多路选择,以及创建用户协程等等功能。
  • 定时任务 顾名思义定时执行的任务。
  • 任务投递 支持将耗时任务投递到Task进程。
  • 自动Reload 可以开启框架的自动Reload功能,这样代码修改会被立即响应。

上面描述的都是一些基础功能,大家开发应用时经常用到的,那么下面则是一些高级功能。

  • 集群以及微服务 框架提供集群部署,通过开启集群开关,部署Consul工具服务器,我们就可以开启集群之旅,框架中消息功能都是支持集群环境的。通过暴露API,监听API,我们可以实现微服务,微服务中我们又提供了健康监控,熔断,超时,负载均衡,请求迁移等等功能。 集群采用的是对等网络,没有中间节点,没有单点隐患,设计理念如下图所示。 image

  • 订阅与发布 SD提供的订阅发布功能也是支持集群环境,并且它严格的按照MQTT所定义的订阅发布规范,并且实现了所有的功能。这恐怕是最好最优秀的订阅发布功能了。

  • 事件派发 跨进程跨服务器的事件派发功能,很多SD框架的基础设施都是基于这个搭建的。

  • 用户进程管理以及进程间RPC SD框架重新封装了用户进程,开发者可以启动自己的用户进程,用户进程可以是异步的也可以是同步的,也是支持各种连接池和协程,用户进程的用处很多,同样框架也支持用户进程和Worker进程间互相RPC调用。

  • 集群下的定时任务 通过Consul可以设置定时任务,并且会同步到集群所有的服务器上去执行,集群服务器会选举出一个Leader,可以通过获取是否是Leader来决定这个任务是否被执行。

  • Context上下文 这个是在消息处理整个流程中被共享的上下文,很实用,很方便。

接下来介绍的是SD特色组件

  • 异步AMQP客户端以及分布式任务系统 消息队列协议AMQP,框架提供了一个支持AMQP协议的异步客户端,可以和RabbitMQ联动,通过框架提供的分布式任务组件,可以搭建分布式任务系统。
  • 异步MQTT客户端 异步的MQTT客户端可以和MQTT服务实现订阅与发布
  • MQTT简易集群服务器 支持QOS0级别的简易MQTT服务器,支持集群部署。
  • 服务器监控系统 提供了一个服务器监控后台,可以对集群进行监控,也可以监控某一台服务器的具体运行状况。 下面是一些截图

SD框架远远不止现在

SD框架一直在高速发展中,有更多开发者的参与才会有更好的未来。 附带SD框架的文档以及官网 官网 文档 GitHub 如果你喜欢,请打个星星支持下~

High performance, high concurrency, PHP asynchronous distributed framework,power by ext-swoole

Development communication QQ-group:569037921

Simple websocket case

Chat room: https://github.com/tmtbe/SD-todpole

Live Demo: http://114.55.253.83:8081/

The official website:http://sd.youwoxing.net

Development document:http://docs.youwoxing.net

Instructional video:http://v.qq.com/boke/gplay/337c9b150064b5e5bcfe344f11a106c5_m0i000801b66cfv.html

Install

You can install via composer

Autoload must specify app and test.

2.0.0" }, "autoload": { "psr-4": { "app\\": "src/app", "test\\": "src/test" } } }">
{
  "require": {
    "tmtbe/swooledistributed":">2.0.0"
  },
 "autoload": {
    "psr-4": {
      "app\\": "src/app",
      "test\\": "src/test"
    }
  }
}

Then execute the following code in the root directory (the vendor higher directory)

php vendor/tmtbe/swooledistributed/src/Install.php

The server can be executed in the bin at the end of the installation.

Advantage

1.High performance and high concurrency, asynchronous event driven

2.HttpClient, client, Mysql, Redis connection pooling

3.Timed task system

4.Coroutine Support

5.Using object pooling mode, optimizing memory allocation and GC

6.Many asynchronous clients, such as MQTT, AMQP, etc.

7.Support cluster deployment

8.User process management

9.Support multi port, multi protocol, automatic conversion between protocols

10.Micro service management based on Consul

11.Automatic discovery of cluster nodes based on Consul

12.Support pubish-subscribe mode

13.MQTT Server

14.Asynchronous operating system

Architecture diagram

Class inheritance structure

image

Process structure

image

Cluster structure

image

Donation

If you like the project, I hope you donate this project so that the project will get better development, Thank you.

Alipay:

image

Comments
  • PHP 7.2 版本不兼容

    PHP 7.2 版本不兼容

    • sd version: 3.2.6
    • PHP version:7.2.7
    • error info
    Fatal error: Uncaught Whoops\Exception\ErrorException: count(): Parameter must be an array or an object that implements Countable in /wwwroot/netbar/vendor/tmtbe/swooledistributed/src/Server/SwooleDistributedServer.php:774
    
    • root cause

    Documentfunction.count

    |Version| Description| |---|---| |7.2.0 | count() will now yield a warning on invalid countable types passed to the array_or_countable parameter.|

    opened by yann0917 4
  • 按照要求安装后启动报错

    按照要求安装后启动报错

    Press Ctrl-C to quit. Start success. ERR Client sent AUTH, but no password is set PHP Fatal error: Uncaught Server\CoreBase\SwooleException: ERR Client sent AUTH, but no password is set in /home/lyl/SwooleDistributed/src/Server/Asyn/Redis/RedisAsynPool.php:414 Stack trace: #0 /home/lyl/SwooleDistributed/src/Server/SwooleDistributedServer.php(147): Server\Asyn\Redis\RedisAsynPool->getSync() #1 /home/lyl/SwooleDistributed/src/Server/SwooleServer.php(569): Server\SwooleDistributedServer->start() #2 /home/lyl/SwooleDistributed/src/Server/SwooleServer.php(263): Server\SwooleServer::startSwooles() #3 /home/lyl/SwooleDistributed/bin/start_swoole_server.php(11): Server\SwooleServer::run() #4 {main} thrown in /home/lyl/SwooleDistributed/src/Server/Asyn/Redis/RedisAsynPool.php on line 414 服务器发生崩溃事件 WORKER EXIT UNEXPECTED Uncaught Server\CoreBase\SwooleException: ERR Client sent AUTH, but no password is set in /home/lyl/SwooleDistributed/src/Server/Asyn/Redis/RedisAsynPool.php:414 Stack trace: #0 /home/lyl/SwooleDistributed/src/Server/SwooleDistributedServer.php(147): Server\Asyn\Redis\RedisAsynPool->getSync() #1 /home/lyl/SwooleDistributed/src/Server/SwooleServer.php(569): Server\SwooleDistributedServer->start() #2 /home/lyl/SwooleDistributed/src/Server/SwooleServer.php(263): Server\SwooleServer::startSwooles() #3 /home/lyl/SwooleDistributed/bin/start_swoole_server.php(11): Server\SwooleServer::run() #4 {main} thrown (/home/lyl/SwooleDistributed/src/Server/Asyn/Redis/RedisAsynPool.php:414) Stack trace: #0 unknown(0): app\AppServer->checkErrors()

    opened by 372572571 4
  • 定时器

    定时器

    我开了一个定时器,是从上午9点到晚上12点的范围,没有规定最大次数,然后每300秒去做一些事情,可是到第二天的9点开始,每跑一次定时器,数据库报一次错, [2017-08-04 09:08:01] SD.ERROR: WORKER Error PDO::prepare(): MySQL server has gone away (/var/www/SwooleDistributed/src/Server/Asyn/Mysql/Miner.php:2487) [] [] [2017-08-04 09:08:01] SD.ERROR: WORKER Error PDO::prepare(): MySQL server has gone away (/var/www/SwooleDistributed/src/Server/Asyn/Mysql/Miner.php:2487) [] [] 请问是什么原因呢?

    opened by yangw1990 3
  • Cannot unpack array with string keys

    Cannot unpack array with string keys

    • sd version: 3.2.0
    • error info:
     > [EX] --------------------------[报错指南]----------------------------2018-05-22 04:11:43
     > [EX] 异常消息:Cannot unpack array with string keys
     > [EX] 文件:/wwwroot/admin/vendor/tmtbe/swooledistributed/src/Server/CoreBase/Controller.php
     > [EX] 位置:192行
     > [EX] #
     > [EX] #0 /wwwroot/admin/vendor/tmtbe/swooledistributed/src/Server/CoreBase/Controller.php(172): Server\CoreBase\Controller->execute('NetbarQrBatchCo...', 'view', Array)
     > [EX] #1 /wwwroot/admin/vendor/tmtbe/swooledistributed/src/Server/SwooleHttpServer.php(163): Server\CoreBase\Controller->setRequestResponse(Object(Swoole\Http\Request), Object(Swoole\Http\Response), 'NetbarQrBatchCo...', 'view', Array)
     > [EX] #2 {main}
     > [EX] --------------------------------------------------------------
    

    line 192 source code:

    $this->getProxy()->$method_name(...$params);
    

    The problem is that the "splat" operator (array unpacking operator or ... ) does not work with associative arrays.

    $this->getProxy()->$method_name(...array_values($params));
    
    opened by yann0917 2
  • redis_pool sAddArray出错

    redis_pool sAddArray出错

    版本3.1.10 swoole版本2.1.2 PHP Fatal error: Uncaught Whoops\Exception\ErrorException: Array to string conversion in /data/sd/vendor/tmtbe/swooledistributed/src/Server/Asyn/Redis/RedisAsynPool.php:359 Stack trace: #0 [internal function]: Server\SwooleServer->displayErrorHandler(8, 'Array to string...', '/data/sd/vendor...', 359, Array) #1 /data/sd/vendor/tmtbe/swooledistributed/src/Server/Asyn/Redis/RedisAsynPool.php(359): Swoole\Redis->__call('sAddArray', Array) #2 /data/sd/vendor/tmtbe/swooledistributed/src/Server/Asyn/AsynPool.php(157): Server\Asyn\Redis\RedisAsynPool->execute(Array) #3 /data/sd/vendor/tmtbe/swooledistributed/src/Server/Asyn/Redis/RedisAsynPool.php(389): Server\Asyn\AsynPool->pushToPool(Object(Swoole\Redis)) #4 [internal function]: Server\Asyn\Redis\RedisAsynPool->Server\Asyn\Redis{closure}(Object(Swoole\Redis), 'OK') #5 /data/sd/vendor/tmtbe/swooledistributed/src/Server/Coroutine/CoroutineBase.php(131): Swoole\Coroutine\Channel::select(Array, NULL, 5) #6 /data/sd/vendor/tmtbe/swooledistributed/src/Server/Coroutine/CoroutineBase.php in /data/sd/vendor/tmtbe/swooledistributed/src/Server/Asyn/Redis/RedisAsynPool.php on line 359

    opened by GreatSir 2
  • miner不支持自写sql字符串

    miner不支持自写sql字符串

    版本2.1.5

    1.在设置where条件时只支持某个字段或者数组的形式。不支持在已有sql直接拼接一段字符串形成新的sql

    例如: 不固定 (status = 1 and dec in ("ss", "cc")) or name = "kk"

    2.解决方案 /Server/Asyn/Mysql/Miner.php

    /** * AND logical operator. 参考laravel起名 */ (1) const LOGICAL_RAW = "RAW";

    (2) 方法里的getCriteriaString 从原先的 case self::IS: case self::IS_NOT: 改成 case self::LOGICAL_RAW: case self::IS: case self::IS_NOT:

    (3) 在方法里 从原先 $statement .= $criterion['column'] . " " . $criterion['operator'] . " " . $value; 改成 if ($criterion['operator'] == self::LOGICAL_RAW) { $statement .= " " . $value . " "; } else { $statement .= $criterion['column'] . " " . $criterion['operator'] . " " . $value; }

    3.调用效果支持 $selectMiner = $selectMiner->openWhere(); //wheres 代表 不固定 (status = 1 and dec in ("ss", "cc")) or name = "kk" $selectMiner = $selectMiner->where('', $wheres, 'RAW'); $selectMiner = $selectMiner->closeWhere();

    opened by lixiaohong0212 2
  • 3.x 版本启动后报错

    3.x 版本启动后报错

    经测试,3.6.2 版本启动时报错,错误如下:

    root@22be1eecd514:/www/bin# php start_swoole_server.php start
    
    Warning: Illegal string offset 'config_version' in /www/vendor/hassankhan/config/src/Parser/Php.php(37) : eval()'d code on line 8
    
    Fatal error: Uncaught Noodlehaus\Exception\UnsupportedFormatException: PHP string does not return an array in /www/vendor/hassankhan/config/src/Parser/Php.php:54
    Stack trace:
    #0 /www/vendor/hassankhan/config/src/Config.php(97): Noodlehaus\Parser\Php->parse('3/**\n * Created...')
    #1 /www/vendor/hassankhan/config/src/Config.php(62): Noodlehaus\Config->loadFromFile('/www/bin/../src...', Object(Noodlehaus\Parser\Php))
    #2 /www/vendor/tmtbe/swooledistributed/src/Server/Console/ChannelCmd.php(31): Noodlehaus\Config->__construct('/www/bin/../src...')
    #3 /www/vendor/tmtbe/swooledistributed/src/Server/Start.php(124): Server\Console\ChannelCmd->__construct('ChannelCmd')
    #4 /www/vendor/tmtbe/swooledistributed/src/Server/Start.php(102): Server\Start::addDirCommand('/www/bin/../ven...', 'Server', Object(Symfony\Component\Console\Application))
    #5 /www/bin/start_swoole_server.php(11): Server\Start::run()
    #6 {main}
      thrown in /www/vendor/hassankhan/config/src/Parser/Php.php on line 54
    
    opened by jormin 1
  • 安装出错

    安装出错

    Root package 'tmtbe/swooledistributed' cannot require itself in its composer.json Did you accidentally name your root package after an external package?

    opened by jinboxiu 1
  • 3.x  Asyn\TcpClient\TcpClientPool 发现BUG一枚

    3.x Asyn\TcpClient\TcpClientPool 发现BUG一枚

    自定义pack里package_max_length无论怎么设置,包都被拆成了固定长度的

    检查代码才发现 43行 $this->set = $this->pack->getProbufSet();

    然后在115行 $client->set($this->config->get('tcpClient.set', []));

    $this->set 根本没有被调用.....白瞎了getProbufSet各种设置~

    tcpClient.set 其实也没有~

    $client->set($this->config->get('tcpClient.set', $this->set));

    opened by ql2005 1
Releases(3.7.4)
Owner
null
💫 Vega is a CLI mode HTTP web framework written in PHP support Swoole, WorkerMan / Vega 是一个用 PHP 编写的 CLI 模式 HTTP 网络框架,支持 Swoole、WorkerMan

Mix Vega 中文 | English Vega is a CLI mode HTTP web framework written in PHP support Swoole, WorkerMan Vega 是一个用 PHP 编写的 CLI 模式 HTTP 网络框架,支持 Swoole、Work

Mix PHP 46 Apr 28, 2022
A easy way to install your basic yii projetc, we have encrypt database password in phpfile, my class with alot funtions to help you encrypt and decrypt and our swoole server install just run ./yii swoole/start and be happy!

Yii 2 Basic Project Template with swoole and Modules Yii 2 Basic Project Template is a skeleton Yii 2 application best for rapidly creating small proj

null 3 Apr 11, 2022
swoole,easyswoole,swoole framework

EasySwoole - A High Performance Swoole Framework EasySwoole is a distributed, persistent memory PHP framework based on the Swoole extension. It was cr

null 4.6k Jan 2, 2023
swoole and golang ipc, use goroutine complete swoole coroutine

swoole and golang ipc demo swoole process module exec go excutable file as sider car, use goroutine complete swoole coroutine hub.php <?php require '

null 2 Apr 17, 2022
一个极简高性能php框架,支持[swoole | php-fpm ]环境

One - 一个极简高性能php框架,支持[swoole | php-fpm ]环境 快 - 即使在php-fpm下也能1ms内响应请求 简单 - 让你重点关心用one做什么,而不是怎么用one 灵活 - 各个组件松耦合,可以灵活搭配使用,使用方法保持一致 原生sql可以和模型关系with搭配使用,

vic 862 Jan 1, 2023
球球大作战(PHP+Swoole)

球球大作战 在线多人H5游戏(H5客户端, 服务端) W A S D 控制 技术栈: PHP7.4+ Swoole4.6(多进程, WebSocket, 共享内存) SpriteJS v3(2D Canvas渲染) 演示Demo 安装: 环境要求:Linux,PHP7.4+(启用Swoole拓展)

Vacant 15 May 9, 2022
基于 swoole 的多进程队列系统,低延时(最低毫秒级)、低资源占用, 支持一键化协程、超时控制、失败重试。可与 laravel thinkphp 等框架配合使用

multi-process-queue 基于swoole的多进程队列系统,manage进程管理子进程,master进程监听队列分发任务,worker进程执行任务, 多进程、低延时(最低毫秒级)、低资源占用。可与 laravel thinkphp 等框架配合使用 版本要求: php>=7.1 swoo

yuntian 55 Dec 12, 2022
swoole worker

SwooleWorker SwooleWorker is a distributed long connection development framework based on Swoole4. 【Github】 【HomePage】 Manual 【ENGLISH】 【简体中文】 Usage s

null 123 Jan 9, 2023
🚀 Developing Rocketseat's Next Level Week (NLW#05) Application using PHP/Swoole + Hyperf

Inmana PHP ?? Developing Rocketseat 's Next Level Week (NLW#05) Application using Swoole + Hyperf. This is the app of the Elixir track. I know PHP/Swo

Leo Cavalcante 18 Jun 1, 2022
Simple live support server with PHP Swoole Websocket and Telegram API

Telgraf Simple live support server with PHP Swoole Websocket and Telegram API. Usage Server Setup Clone repository with following command. git clone h

Adem Ali Durmuş 6 Dec 30, 2022
Library for Open Swoole extension

Open Swoole Library This library works with Open Swoole since release version v4.7.1. WIP Table of Contents How to Contribute Code Requirements Develo

Open Swoole 3 Dec 22, 2022
Hyperf instant messaging program based on swoole framework

Hyperf instant messaging program based on swoole framework

null 20 Aug 12, 2022
Redis watcher for PHP-Casbin in Swoole.

Redis watcher for PHP-Casbin in Swoole Redis watcher for PHP-Casbin in Swoole , Casbin is a powerful and efficient open-source access control library.

PHP-Casbin 1 Nov 22, 2021
Socks5 proxy server written in Swoole PHP

php-socks This is a Socks5 proxy server implementation built with PHP & Swoole. To start the proxy server, clone this repo, run composer install to in

Nazmul Alam 3 Jan 23, 2022
LaravelS is an out-of-the-box adapter between Swoole and Laravel/Lumen.

?? LaravelS is an out-of-the-box adapter between Swoole and Laravel/Lumen.

Biao Xie 3.7k Dec 29, 2022
DoraRPC is an RPC For the PHP MicroService by The Swoole

Dora RPC 简介(Introduction) Dora RPC 是一款基础于Swoole定长包头通讯协议的最精简的RPC, 用于复杂项目前后端分离,分离后项目都通过API工作可更好的跟踪、升级、维护及管理。 问题提交: Issue For complex projects separation

Chang Long Xu 468 Jan 5, 2023
Hprose asynchronous client & standalone server based on swoole

Hprose for Swoole Introduction Hprose is a High Performance Remote Object Service Engine. It is a modern, lightweight, cross-language, cross-platform,

Hprose 186 Sep 9, 2022
PHP Kafka client is used in PHP-FPM and Swoole. PHP Kafka client supports 50 APIs, which might be one that supports the most message types ever.

longlang/phpkafka Introduction English | 简体中文 PHP Kafka client is used in PHP-FPM and Swoole. The communication protocol is based on the JSON file in

Swoole Project 235 Dec 31, 2022
This package provides a high performance HTTP server to speed up your Laravel/Lumen application based on Swoole.

This package provides a high performance HTTP server to speed up your Laravel/Lumen application based on Swoole.

Swoole Taiwan 3.9k Jan 8, 2023