Skip to content

Tutorial 5

knightliao edited this page Apr 25, 2016 · 2 revisions

Tutorial 5 具有参数规则引擎计算服务能力

在 Tutorial 1 静态配置服务能力 的基础上,增加参数规则引擎能力,可根据参数的规则不同,将请求定向到不同的静态文件上。

举个例子:

对于一个URL为"/api4/json"的请求,它含有"userId"参数,我们想要实现这样的功能:

  • 当userId==1时,返回json1
  • 当userId==2时,返回json2
  • 其它情况,返回json3

这里以 https://github.com/knightliao/pfrock-demos/tree/master/demos/static-rule-demo 为demo

demo

{
    "servers": [
        {
            "port": 8888,
            "routes": [
                {
                    "path": "/api4/json",
                    "handler": "pfrock_static_plugin",
                    "options": {
                        "files": [
                            {
                                "rule": [
                                    "=",
                                    "${userId}",
                                    "1"
                                ],
                                "file": "mocks/static/a.json"
                            },
                            {
                                "rule": [
                                    "=",
                                    "${userId}",
                                    "2"
                                ],
                                "file": "mocks/static/b.json"
                            },
                            {
                                "file": "mocks/static/c.json"
                            }
                        ]
                    }
                }
            ]
        }
    ]

}

说明

该配置文件意味着

  • 127.0.0.1:8888/api4/json?userId=1 请求的是 mocks/static/a.json
  • 127.0.0.1:8888/api4/json?userId=2 请求的是 mocks/static/b.json
  • 其它情况,请求的是 mocks/static/c.json

实践:

➜  pfrock-demos git:(master) curl -v '127.0.0.1:8888/api4/json?userId=1'
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> GET /api4/json?userId=1 HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Length: 15
< Accept-Ranges: bytes
< Server: TornadoServer/4.3
< Last-Modified: Mon, 25 Apr 2016 15:06:48 GMT
< Etag: "a101b1096326f81b0bc60f7c5e1285f9"
< Date: Mon, 25 Apr 2016 15:59:19 GMT
< Content-Type: application/json
<
{
  "a": "a1"
* Connection #0 to host 127.0.0.1 left intact
}%                                                                                                                                            ➜  pfrock-demos git:(master) curl -v '127.0.0.1:8888/api4/json?userId=2'
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> GET /api4/json?userId=2 HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Length: 17
< Accept-Ranges: bytes
< Server: TornadoServer/4.3
< Last-Modified: Mon, 25 Apr 2016 15:06:48 GMT
< Etag: "932a08287e15ea975eb519977b3b7748"
< Date: Mon, 25 Apr 2016 15:59:23 GMT
< Content-Type: application/json
<
{
    "b": "b2"
* Connection #0 to host 127.0.0.1 left intact
}%                                                                                                                                            ➜  pfrock-demos git:(master) curl -v '127.0.0.1:8888/api4/json'
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> GET /api4/json HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Length: 17
< Accept-Ranges: bytes
< Server: TornadoServer/4.3
< Last-Modified: Mon, 25 Apr 2016 15:40:05 GMT
< Etag: "e1b188d5446da3af30c8ea9da293b655"
< Date: Mon, 25 Apr 2016 15:59:27 GMT
< Content-Type: application/json
<
{
    "b": "c2"
* Connection #0 to host 127.0.0.1 left intact
}%                                                                                                                                            ➜  pfrock-demos git:(master)
Clone this wiki locally