联系
Knight's Tale » 我的作品

pfrock : 提供微服务(SOA)架构能力的 统一mock服务

2016-02-27 10:29

pfrock

https://github.com/knightliao/pfrock

A plugin-based server for running fake HTTP and socket services (especially SOA service) using Python.

   _ (`-.            _  .-')                          .-. .-')
  ( (OO  )          ( \( -O )                         \  ( OO )
 _.`     \   ,------.,------.  .-'),-----.    .-----. ,--. ,--.
(__...--''('-| _.---'|   /`. '( OO'  .-.  '  '  .--./ |  .'   /
 |  /  | |(OO|(_\    |  /  | |/   |  | |  |  |  |('-. |      /,
 |  |_.' |/  |  '--. |  |_.' |\_) |  |\|  | /_) |OO  )|     ' _)
 |  .___.'\_)|  .--' |  .  '.'  \ |  | |  | ||  |`-'| |  .   \
 |  |       \|  |_)  |  |\  \    `'  '-'  '(_'  '--'\ |  |\   \
 `--'        `--'    `--' '--'     `-----'    `-----' `--' '--'

English readme

https://github.com/knightliao/pfrock/blob/master/README-en.md

主要目标

  • 为微服务架构(SOA)而生。可以mock微服务架构(SOA)中各式各样的服务接口请求
  • 提供统一的mock服务。通过提供统一的router入口和自定义的sub-routers来实现
  • 为敏捷开发提供可行的方案,只需更改配置文件,无须重启即可快速生效快速mock。建议按以下方式进行敏捷开发
    • 服务间定好接口
    • 使用pfrock来提供统一mock服务
    • 通过mock接口,修改配置文件,无须重启pfrock,快速进行接口式开发
    • 联调过程和测试过程,亦可通过pfrock来进行部分接口mock
  • 插件式开发,为可扩展性提供良好支持。支持 静态/动态/自定义 的Mock服务能力

可用的插件

demos

https://github.com/knightliao/pfrock-demos

pypi

https://pypi.python.org/pypi/pfrock

  • dev(develop branch): 0.2.1.dev16
  • master(stable branch):0.2.1.dev16

安装

pip install pfrock==0.2.1.dev16

使用指南

pfrock-demos

demos for 微服务mock服务 using python. https://github.com/knightliao/pfrock

demo

install pfrock

pip install pfrock==0.2.1.dev16

run demo

cd demos/demo
pfrockpy

运行结果

knightliao@mynode03:~/tmp/pfrock-demos/demos/demo$ pfrockpy -d
   _ (`-.            _  .-')                          .-. .-')
  ( (OO  )          ( \( -O )                         \  ( OO )
 _.`     \   ,------.,------.  .-'),-----.    .-----. ,--. ,--.
(__...--''('-| _.---'|   /`. '( OO'  .-.  '  '  .--./ |  .'   /
 |  /  | |(OO|(_\    |  /  | |/   |  | |  |  |  |('-. |      /,
 |  |_.' |/  |  '--. |  |_.' |\_) |  |\|  | /_) |OO  )|     ' _)
 |  .___.'\_)|  .--' |  .  '.'  \ |  | |  | ||  |`-'| |  .   \
 |  |       \|  |_)  |  |\  \    `'  '-'  '(_'  '--'\ |  |\   \
 `--'        `--'    `--' '--'     `-----'    `-----' `--' '--'
pfrock version 0.2.1.dev16
[D 2016-02-27 00:12:44,256 pfrock.core MainThread routes:74] add : (u'/api1/people2', <class 'pfrock_static_plugin.handlers.file.FrockStaticFileHandler'>, {'path': u'mocks/static/a.json', 'methods': []})
[D 2016-02-27 00:12:44,256 pfrock.core MainThread routes:74] add : (u'/api1/(.*)', <class 'pfrock_static_plugin.handlers.dir.FrockStaticDirHandler'>, {'path': u'mocks/static', 'methods': []})
[D 2016-02-27 00:12:44,257 pfrock.core MainThread routes:74] add : (u'/api2/people', <class 'pfrock_static_plugin.handlers.file.FrockStaticFileHandler'>, {'path': u'mocks/static/b.json', 'methods': []})
[D 2016-02-27 00:12:44,257 pfrock.core MainThread routes:74] add : (u'/api3/(.*)', <class 'pfrock_static_plugin.handlers.dir.FrockStaticDirHandler'>, {'path': u'mocks/static', 'methods': []})
[D 2016-02-27 00:12:44,258 pfrock.core MainThread routes:74] add : (u'/api', <class 'mocks.handler.hello_world.HelloWorldHandler'>, {u'query': u'1!', u'handler': u'mocks.handler.hello_world.HelloWorldHandler', 'methods': [u'GET'], u'pageno': 1})
[D 2016-02-27 00:12:44,258 pfrock.core MainThread routes:74] add : (u'/api/persistence', <class 'mocks.handler.persistence.PersistenceHandler'>, {u'query': u'1!', u'handler': u'mocks.handler.persistence.PersistenceHandler', 'methods': [u'GET'], u'pageno': 1})
[D 2016-02-27 00:12:44,261 pfrock.core MainThread routes:74] add : (u'.*', <class 'pfrock_proxy_plugin.proxy.ProxyHandler'>, {'header_host': '', 'proxy_url': u'http://www.sov5.com', 'methods': []})
[I 2016-02-27 00:12:44,261 pfrock.core MainThread __init__:19] started server 8888 with autoreload mode

配置文件

{
  "servers": [
    {
      "port": 8888,
      "routes": [
        {
          "path": "/api1/(.*)",
          "handler": "pfrock_static_plugin",
          "options": {
            "routes": [
              {
                "path": "people2",
                "file": "mocks/static/a.json"
              },
              {
                "dir": "mocks/static"
              }
            ]
          }
        },
        {
          "path": "/api2/people",
          "handler": "pfrock_static_plugin",
          "options": {
            "file": "mocks/static/b.json"
          }
        },
        {
          "path": "/api3/(.*)",
          "handler": "pfrock_static_plugin",
          "options": {
            "dir": "mocks/static"
          }
        },
        {
          "path": "/api",
          "methods": [
            "GET"
          ],
          "handler": "pfrock_http_plugin",
          "options": {
            "handler": "mocks.handler.hello_world.HelloWorldHandler",
            "query": "1!",
            "pageno": 1
          }
        },
        {
          "path": "/api/persistence",
          "methods": [
            "GET"
          ],
          "handler": "pfrock_http_plugin",
          "options": {
            "handler": "mocks.handler.persistence.PersistenceHandler",
            "query": "1!",
            "pageno": 1
          }
        },
        {
          "path": ".*",
          "methods": "any",
          "handler": "pfrock_proxy_plugin",
          "options": {
            "url": "http://www.sov5.com"
          }
        }         
      ]
    }
  ]
}
  • 静态服务能力:
    • /api1/(.*)
      • /api1/people2 --> mocks/static/a.json
      • /api/(.*) --> mocks/static
    • /api2/people --> mocks/static/b.json
    • /api3/(.*) --> mocks/static
  • 动态服务能力:
    • /api --> mocks.handler.hello_world.HelloWorldHandler
    • /api/persistence --> mocks.handler.persistence.PersistenceHandler
  • 代理服务能力:
    • 其它接口 --> http://www.sov5.com