All Projects → meetbill → Butterfly

meetbill / Butterfly

Licence: gpl-3.0
🔥 蝴蝶--【简单】【稳定】【好用】的 Python web 框架🦋 除 Python 2.7,无其他依赖; 🦋 butterfly 是一个 RPC 风格 web 框架,同时也是微服务框架,自带消息队列通信机制实现分布式

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Butterfly

Javatech
☕️ 汇总 Java 开发中常见的主流技术的应用、特性、原理。
Stars: ✭ 310 (+278.05%)
Mutual labels:  rpc, orm, mq, mvc
Hunt Framework
A Web framework for D Programming Language. Full-stack high-performance.
Stars: ✭ 256 (+212.2%)
Mutual labels:  microservice, orm, web-framework, mvc
Finagle
A fault tolerant, protocol-agnostic RPC system
Stars: ✭ 8,126 (+9809.76%)
Mutual labels:  rpc, mysql, redis, memcached
Mormot
Synopse mORMot ORM/SOA/MVC framework
Stars: ✭ 607 (+640.24%)
Mutual labels:  orm, mysql, mvc
Gnomock
Test your code without writing mocks with ephemeral Docker containers 📦 Setup popular services with just a couple lines of code ⏱️ No bash, no yaml, only code 💻
Stars: ✭ 398 (+385.37%)
Mutual labels:  mysql, redis, memcached
Go Sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM.
Stars: ✭ 539 (+557.32%)
Mutual labels:  orm, mysql, lightweight
Waterdrop
💧Waterdrop is a high performance micro service framework. Waterdrop comes from (The Three Body Problem).
Stars: ✭ 305 (+271.95%)
Mutual labels:  microservice, rpc, mq
Smartsql
SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics
Stars: ✭ 775 (+845.12%)
Mutual labels:  orm, mysql, redis
Imi
imi 是基于 Swoole 的 PHP 协程开发框架,它支持 Http、Http2、WebSocket、TCP、UDP、MQTT 等主流协议的服务开发,特别适合互联网微服务、即时通讯聊天im、物联网等场景!。QQ群:17916227
Stars: ✭ 680 (+729.27%)
Mutual labels:  mysql, redis, mvc
Bestnote
👊 持续更新,Java Android 近几年最全面的技术点以及面试题 供自己学习使用
Stars: ✭ 841 (+925.61%)
Mutual labels:  mysql, redis, mvc
Ssm redis template
An template based on Maven, using Spring + Spring MVC + mybatis + spring-data-redis frames. It can be used to construct a new Java Web Appliaction quickly
Stars: ✭ 13 (-84.15%)
Mutual labels:  mysql, redis, mvc
Dotnetguide
🦸【C#/.NET/.NET Core学习、工作、面试指南】概述:C#/.NET/.NET Core基础知识,学习资料、文章、书籍,社区组织,工具和常见的面试题总结。以及面试时需要注意的事项和优秀简历编写技巧,希望能和大家一起成长进步👊。【让现在的自己不再迷漫✨】
Stars: ✭ 308 (+275.61%)
Mutual labels:  mysql, redis, memcached
Saea
SAEA.Socket is a high-performance IOCP framework TCP based on dotnet standard 2.0; Src contains its application test scenarios, such as websocket,rpc, redis driver, MVC WebAPI, lightweight message server, ultra large file transmission, etc. SAEA.Socket是一个高性能IOCP框架的 TCP,基于dotnet standard 2.0;Src中含有其应用测试场景,例如websocket、rpc、redis驱动、MVC WebAPI、轻量级消息服务器、超大文件传输等
Stars: ✭ 318 (+287.8%)
Mutual labels:  rpc, redis, mvc
Cakephp
CakePHP: The Rapid Development Framework for PHP - Official Repository
Stars: ✭ 8,453 (+10208.54%)
Mutual labels:  orm, web-framework, mvc
Netkiller.github.io
Netkiller Free ebook - 免费电子书
Stars: ✭ 861 (+950%)
Mutual labels:  mysql, redis, memcached
Web Development Interview With Java
Java 开发相关技术栈(大中厂)高频面试问题收录。
Stars: ✭ 69 (-15.85%)
Mutual labels:  microservice, mysql, redis
Zxw.framework.netcore
基于EF Core的Code First模式的DotNetCore快速开发框架,其中包括DBContext、IOC组件autofac和AspectCore.Injector、代码生成器(也支持DB First)、基于AspectCore的memcache和Redis缓存组件,以及基于ICanPay的支付库和一些日常用的方法和扩展,比如批量插入、更新、删除以及触发器支持,当然还有demo。欢迎提交各种建议、意见和pr~
Stars: ✭ 691 (+742.68%)
Mutual labels:  mysql, redis, memcached
Proteus
Lean, mean, and incredibly fast JVM framework for web and microservice development.
Stars: ✭ 178 (+117.07%)
Mutual labels:  microservice, web-framework, mvc
Go Zero
go-zero is a web and rpc framework written in Go. It's born to ensure the stability of the busy sites with resilient design. Builtin goctl greatly improves the development productivity.
Stars: ✭ 13,156 (+15943.9%)
Mutual labels:  microservice, rpc, web-framework
Treefrog Framework
TreeFrog Framework : High-speed C++ MVC Framework for Web Application
Stars: ✭ 885 (+979.27%)
Mutual labels:  orm, mysql, redis

butterfly

蝴蝶(轻量化 Web 框架)如同蝴蝶一样,此框架小而美,简单可依赖

    __          __  __            ______
   / /_  __  __/ /_/ /____  _____/ __/ /_  __
  / __ \/ / / / __/ __/ _ \/ ___/ /_/ / / / /
 / /_/ / /_/ / /_/ /_/  __/ /  / __/ / /_/ /
/_.___/\__,_/\__/\__/\___/_/  /_/ /_/\__, /
                                    /____/

1 简介

1.1 环境

env:Python 2.7

1.2 特性

  • 快速开发
    • (1) 无需配置路由:根据 handlers package 下目录结构自动加载路由(目前不支持动态路由)
    • (2) 参数保持一致:Handler 的参数列表与 HTTP 请求参数保持一致,HTTP 接口所需参数一目了然
    • (3) 自动参数检查:自动对 HTTP 请求参数进行参数检查
    • (4) 简易调试模式:简易方便的 DEBUG
  • 方便运维
    • (1) 请求完整追溯:请求的响应 Header 中包含请求的 reqid(会记录在日志中), 便于进行 trace
    • (2) 自定义响应头:Header 函数中很方便自定义 HTTP header, 如增加固定的接口版本号
    • (3) 代码耗时打点:通过代码打点可以准确获代码执行耗时
  • 容易扩展
    • (1) 消息队列通信:启动时开启百川配置即成为一个消费者,以拉模式消费由其他实例发布的消息

2 五分钟 Butterfly 体验指南

2.1 五分钟体验之部署 Butterfly(预计 1 分钟)

部署

$ wget https://github.com/meetbill/butterfly/archive/master.zip -O butterfly.zip
$ unzip butterfly.zip
$ cd butterfly-master/butterfly

配置端口 --- 默认 8585 端口,若无需修改可进入下一项启动

conf/config.py

启动

$ bash run.sh start

访问

$ curl "http://127.0.0.1:8585/demo_api/ping"
{"stat": "OK", "randstr": "..."}

$ curl "http://127.0.0.1:8585/demo_api/hello?str_info=meetbill"
{"stat": "OK", "str_info": "meetbill"}

2.2 五分钟体验之编写 handler (预计 3 分钟)

创建 app (handlers 目录下的子目录均为 app), 直接拷贝个现有的 app 即可

$ cp -rf handlers/demo_api handlers/test_app

新增 handler (app 目录下的__init__.py 中编写 handler 函数)

$ vim handlers/test_app/__init__.py

新增如下内容:

# ------------------------------ handler
@funcattr.api
def test_handler(req, info):
    return retstat.OK, {"data": info}

重启服务

$ bash run.sh restart

访问

$ curl "http://127.0.0.1:8585/test_app/test_handler?info=helloworld"
{"stat": "OK", "data": "helloworld"}

2.3 五分钟体验之调试 handler (预计 1 分钟)

假如编写的 handler 不符合预期的时候,可以通过 test_handler.py 进行调试

调试刚才编写的 test_handler

$ python test_handler.py /test_app/test_handler helloworld
... 此处会输出彩色的  DEBUG 信息
Source path:... test_handler.py
>>>>>|19:03:53.293076 4694912448-MainThread call        66             def main():
------19:03:53.294108 4694912448-MainThread line        67                 return func(*args)
    Source path:... /Users/meetbill/butterfly-master/butterfly/handlers/test_app/__init__.py
    Starting var:.. req = <xlib.httpgateway.Request object at 0x10b4148d0>
    Starting var:.. info = 'helloworld'
    >>>>>|19:03:53.294402 4694912448-MainThread call        49 def test_handler(req, info):
    ------19:03:53.294864 4694912448-MainThread line        50     return retstat.OK, {"data": info}
    |<<<<<19:03:53.294989 4694912448-MainThread return      50     return retstat.OK, {"data": info}
    Return value:.. ('OK', {'data': 'helloworld'})
Source path:... test_handler.py
|<<<<<19:03:53.295114 4694912448-MainThread return      67                 return func(*args)
Return value:.. ('OK', {'data': 'helloworld'})
Elapsed time: 00:00:00.002140
=============================================================
('OK', {'data': 'helloworld'})
=============================================================

运行中产生的 logging 日志会记录在 logs/dev/ 目录

3 设计蓝图

3.1 单实例内部请求流

       +-------------------------------------------------------------+
       |                        WEB brower                           |
       +-----------------------------------^-------------------------+
     /                      |              |
    |  +--------------------V--------------|-------------------------+
    |  | +----------------HTTPServer(Threadpool&Queue)-------------+ |
    |  | |   +-------------------+ put +-----------------------+   | |
    |  | |   |ThreadPool(Queue) <------+ HTTPConnection        |   | |
    |  | |   |+---------------+  |     | +-------------------+ |   | |
    |  | |   ||WorkerThread   |  |     | |req=HTTPRequest()  | |   | |
    |  | |   ||+-+ +-+ +-+ +-+|  |     | |req.parse_request()| |   | |(把 socket 字节流,按 HTTP 协议解析)
    |  | |   ||+-+ +-+ +-+ +-+|  |     | |req.respond()#^!^  | |   | |(封装了 WSGIGateway.response)
    |  | |   |+-|---|---|---|-+  |     | +-------------------+ |   | |
    |  | |   +--|---|---|---|----+     +-----------------------+   | |
    |  | +------|---|---|---|-----------------------^--------------+ |
    |  |        |   |   |   |                       |                | WSGI server
    |  | +------V---V---V---V-WSGIGateway(response)-|--------------+ |
    |  | |                       +------------------+-------------+| |
    |  | |+----------------+     | +----------+   +-------------+ || |
    |  | ||   gen environ  |     | |header_set|   |response body| || |
    |  | |+-----+----------+     | +----------+   +-------------+ || |
    |  | |      |                +--^------^----------^-----------+| |
    |  | +------|-------------------|------|----------|------------+ |
    |  +--------|-------------------|------|----------|--------------+
    |  .........|...................|......|..........|......................
    |           |                   |      |          |
    |  +--------V--------+          |      |          |
    |  |       req       |          |      |          |        (1) 封装 environ 为 Request
    |  +-----------------+          |      |          |            生成 reqid
Butterfly       |                   |      |          |
    |           |                   |      |          |      \
    |  +--------V--------+          |      |          |       |(2) 路由
    |  |  apiname_getter |          |      |          |       |    在路由字典中匹配 environ['PATH_INFO']
    |  +-----------------+          |      |          |       |    {
    |           |                   |      |          |       |      '/apidemo/ping':
    |           |                   |      |          |       |        <xlib.protocol_json.Protocol object>,
    |  +--------V--------+ False +--+--+   |          |       |      '/{app}/{handler}':
    |  |is_protocol_exist|------>| 400 |   |          |       |        <xlib.protocol_json.Protocol object>
    |  +-----------------+       +-----+   |          |       |    }
    |           |                          |          |      /
    |           | (protocol_process)       |          |      \
    |           V                          |          |       |(3) 返回 Response
    |  +-----------------+                 |          |       |    参数:第一个参数为 Request 实例化对象 req
    |  | protocol        | Exception    +-----+       |       |          其他参数为 GET 请求参数名
    |  | +-------------+ |------------->| 500 |       |       |    例子:environ['QUERY_STRING']: 'age=16'
    |  | |/app1/handler| |              +-----+       |       |    +-------------handler demo---------------
    |  | |/app2/handler| |Normal+----------------------------+|    |@funcattr.api      # 标识 handler 类型
    |  | +--+-------+--+ |----->|httpstatus, headers, content||    |def demo(req, age):# req + HTTP 请求参数
    |  +----|-------|----+      +----------------------------+|    |   #(状态信息,数据信息,响应头列表)
    |       |       |                                         |    |   return "OK", {"data": age}, []
    |       |       |                                        /     +----------------------------------------
    |       |  +----V----------------------------------+
    |       |  |+---------+  +---------+  +-----------+|
    |       |  ||DiskCache|  |   FSM   |  |APScheduler||       基础公共库
    |       |  |+---------+  +---------+  +-----------+|
    |       |  +---------------------------------------+
    |  +----V------------------------------------------+
    |  |       (Redis ORM) / (MySQL ORM) / RAL         |       数据访问层
     \ +-----------------------------------------------+

3.2 多实例间消息队列通信

默认实例只能被动接受请求,然后返回响应,通过开启【百川】,实例将主动消费 MQ 中消息

  • 开启方式
    • Butterfly 服务配置文件中,CACHES 配置开启 "baichuan" 的 Redis 连接配置
  • Butterfly 行为
    • (1) 每分钟发送心跳到 MQ(Redis)
    • (2) 每 15 分钟进行一次清理 MQ 中过期任务,运行此任务时会加锁
    • (3) 启动单独线程检查相关的任务,以 模式从队列中拉取消息并进行处理,处理结果也会存储在此 Redis 中
  • MQ
    • (1) Redis 服务,支持 Twemproxy 组成的 Redis 集群
    • (2) 每个 /{app}/{handler} 是个单独的队列
      • topic: /{app}/{handler}
      • message: 消息为 json(butterfly 消费消息时,会将 json 转为字典以参数传给 handler)
    • (3) 消息的四种状态 (queued: 还在队列中,started: 执行中,failed: 执行失败,finished: 完成)
                    +Message Queue ------------------------------------------+
+---------------+   |                                                        |    +----------------+
|  other  app   |   |                                                        |    |   other app    |
|     ...       |-->| +Canghai Dashboard ---+------+-------+--------+------+ |<---|      ...       |
+---------------+   | |Queue                |Queued|Started|Finished|Failed| |    +----------------+
                    | +---------------------+------+-------+--------+------+ |
                    | |/demo_api/hello      |  0   |   0   |  91    |  0   | |
                    | |/demo_api/ping       |71731 |  50   | 71736  |  27  | |
                    | |/{app}/{handler}     |  X   |   X   |   X    |  X   | |
+ruqi-----------+   | +---------------------+------+-------+--------+------+ |    +baichuan/canghai--+
|port:4100      |   |                                                        |    |port:4300         |
|handlers/ruqi  |-->| +Redis Arch -----------------------------------------+ |<---|handlers/baichuan |
+---------------+   | |               +---------+                          | |    |handlers/canghai  |
                    | |               |Twemproxy| \                        | |    +------------------+
                    | |               +--+---+--+  +----------+            | |
                    | |                  |   |     |Metaserver|            | |
+wuxing---------+   | |               +--V---V--+  +----------+            | |
|port:4200      |   | |               |  Redis  | /                        | |
|handlers/wuxing|-->| |               +---------+                          | |
+---------------+   | +----------------------------------------------------+ |
                    +--------------------------------------------------------+
                                                ^
                                                |
                                                |
                                    +sinan--------------------+
                                    |port:4800                |
                                    |handlers/sinan           |
                                    +-------------------------+

通用服务能力:

  • 如期而至
    • 预期:定时服务高可用
  • 五行化一
    • 预期:数据统一管理
  • 百川归海
    • 预期:实例间通过消息队列通信

4 了解更多

4.1 传送门

4.2 自带 app 介绍

看文档不过瘾,还可以通过了解当前 handler 的实现,进而实现自己的需求:

  • 【例子】/handlers/demo_api: 简单 api handler demo
  • 【例子】/handlers/demo_download: 文件下载 handler demo
  • 【例子】/handlers/demo_httpapi: 自定义 HTTP 返回码 handler demo
  • 【例子】/handlers/demo_log: 日志级别调整 handler demo
  • 【例子】/handlers/demo_template: 后端模板 handler demo, 本例子用于输出访问日志统计状态图
  • 【例子】/handlers/demo_async_job: 异步任务 handler demo
  • 【火眼】/handlers/huoyan: 用于后端接口认证,仅仅抛砖引玉,删除了具体实现
  • 【如期】/handlers/ruqi: 高可用定时任务,可发起 HTTP POST 请求或者执行 Shell/Python 脚本
  • 【五行】/handlers/wuxing: 用于存储配置类 / 监控类 / 巡检类数据,可配合状态机进行使用

备注:

handlers 目录下 APP, 均可移除,具体操作就是将对应 app 目录进行删除

4.3 报告错误

如果您想报告错误,请在 GitHub 上创建一个新问题

5 版本信息

本项目的各版本信息和变更历史可以在这里 查看。

6 参加步骤

  • 在 GitHub 上 fork 到自己的仓库,然后 clone 到本地,并设置用户信息。
$ git clone https://github.com/meetbill/butterfly.git
$ cd butterfly
$ git config user.name "yourname"
$ git config user.email "your email"
  • 修改代码后提交,并推送到自己的仓库。
$ #do some change on the content
$ git commit -am "Fix issue #1: change helo to hello"
$ git push
  • 在 GitHub 网站上提交 pull request。
  • 定期使用项目仓库内容更新自己仓库内容。
# 配置原仓库地址
$ git remote add upstream https://github.com/meetbill/butterfly.git
# 查看当前仓库的远程仓库地址和原仓库地址
$ git remote -v
# 获取原仓库的更新,使用 fetch 更新,fetch 后会被存储在一个本地分支 upstream/master 上
$ git fetch upstream
# 切换到本地 master 分支
$ git checkout master
# 合并
$ git rebase upstream/master
# 查看更新
$ git log
# 推送到自己仓库
$ git push -f origin master
# 把本地全部存在的 tag 推送到服务器
$ git push origin --tags
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].