撸了一个微服务(SOA) MOCK 服务 pfrock

2016-03-04 17:42:10 +08:00
 knightliao

https://github.com/knightliao/pfrock

pfrock

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

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

requirements

python >=2.7

开发进度

English readme

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

主要目标

也就是说,

通过此种方式, 我们可以以最低的侵入性, 实现为系统的部分或者全部接口进行 mock.

可用的插件

快速安装

pip install pfrock==0.2.5

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

Quick-Start

demo 目录结构

- demo
    - mocks
        - handler
            - hello_world.py
            - __init__.py
        - static
            - a.json
            - b.json
        - __init__.py
    - __init__.py
    - pfrockfile.json

配置文件

{
    "servers": [
        {
            "port": 8888,
            "routes": [
                {
                    "path": "/api1/(.*)",
                    "handler": "pfrock_static_plugin",
                    "options": {
                        "routes": [
                            {
                                "path": "json",
                                "file": "mocks/static/a.json"
                            },
                            {
                                "dir": "mocks/static"
                            }
                        ]
                    }
                },
                {
                    "path": "/api",
                    "methods": [
                        "GET"
                    ],
                    "handler": "pfrock_http_plugin",
                    "options": {
                        "handler": "mocks.handler.hello_world.HelloWorldHandler",
                        "query": "1!",
                        "pageno": 1
                    }
                },
                {
                    "path": ".*",
                    "methods": "any",
                    "handler": "pfrock_proxy_plugin",
                    "options": {
                        "url": "http://www.sov5.com"
                    }
                }
            ]
        }
    ]
}

启动

➜  pfrock git:(master) ✗
➜  pfrock git:(master) ✗ cd demo
➜  demo git:(master) ✗ pfrockpy
   _ (`-.            _  .-')                          .-. .-')
  ( (OO  )          ( \( -O )                         \  ( OO )
 _.`     \   ,------.,------.  .-'),-----.    .-----. ,--. ,--.
(__...--''('-| _.---'|   /`. '( OO'  .-.  '  '  .--./ |  .'   /
 |  /  | |(OO|(_\    |  /  | |/   |  | |  |  |  |('-. |      /,
 |  |_.' |/  |  '--. |  |_.' |\_) |  |\|  | /_) |OO  )|     ' _)
 |  .___.'\_)|  .--' |  .  '.'  \ |  | |  | ||  |`-'| |  .   \
 |  |       \|  |_)  |  |\  \    `'  '-'  '(_'  '--'\ |  |\   \
 `--'        `--'    `--' '--'     `-----'    `-----' `--' '--'
pfrock version 0.2.2
[I 2016-03-04 14:07:05,231 pfrock.core MainThread __init__:19] started server 8888 with autoreload mod

静态文件 json get 请求

➜  ~  curl http://localhost:8888/api1/json
{
  "a": "bddd33e34"
}%

静态文件 json post 请求

➜  ~  curl -X POST -d {} http://localhost:8888/api1/json
{
  "a": "bddd33e34"
}%

静态目录 json get 请求

➜  ~  curl http://localhost:8888/api1/b.json
{
    "b": "bbb"
}%

动态能力 get

➜  ~  curl 'http://localhost:8888/api'
Hello, world 1! 1%                                                                                                                                            ➜  ~

代理能力

那么, 除了上面这些 URL 之外, 没有被匹配的 URL 怎么办? 就用代理到默认的服务器上吧! 这在项目中经常会被用到.

➜  ~  curl 'http://localhost:8888/'

没有匹配到的 URL 自动被 路由到 设定的 域名和端口上.

这里是被路由到 http://www.sov5.com

Tutorial

more demos

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

敏捷开发建议

建议按以下方式进行敏捷开发

群·联系·讨论

2786 次点击
所在节点    分享创造
7 条回复
ezreal
2016-03-04 18:30:54 +08:00
赞~
ezreal
2016-03-04 18:34:41 +08:00
但是怎么产品随机的模拟数据呢
knightliao
2016-03-04 20:32:44 +08:00
@ezreal 那这个就是另外一个话题了。。关于如何产生 合格的 随机的 较大规模的 测试数据
knightliao
2016-03-04 20:33:50 +08:00
@ezreal 最近还在为它开发一个插件 ,可以比较方便的根据用户规则 引擎来返回数据
ezreal
2016-03-05 20:16:05 +08:00
@knightliao 觉得应该把这个功能整合进去,不然功能太有限了
knightliao
2016-03-05 21:16:25 +08:00
@ezreal 可以,回头我搞上
knightliao
2016-04-26 00:25:15 +08:00
支持具有参数规则引擎计算服务能力 https://github.com/knightliao/pfrock/wiki/Tutorial-5

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/261161

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX