博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【微服务】网关Kong使用插件,自定义错误返回
阅读量:6195 次
发布时间:2019-06-21

本文共 7476 字,大约阅读时间需要 24 分钟。

  hot3.png

Kong插件

Kong的插件支持四种维度,执行顺序从上到下,另需注意,如果同一个插件在不同维度都配置过,只会执行一次:

  • 应用在Api加上消费者组合;
  • 应用在消费者;
  • 应用在Api;
  • 应用在全局;

另外也不是所有插件都支持定义消费者

再添加一个Api供测试

再添加一个新接口:

POST http://192.168.0.181:8001/apis/#参数name:spring-boot-consul-service2upstream_url:http://spring-boot-consul-service2.service.dc1.consul:17004methods:GETuris:/user
{    "created_at": 1515055457572,    "strip_uri": true,    "id": "a99919d1-0770-40a1-81a9-e3c31fd9d282",    "name": "spring-boot-consul-service2",    "methods": [        "GET"    ],    "http_if_terminated": false,    "preserve_host": false,    "upstream_url": "http://spring-boot-consul-service2.service.dc1.consul:17004",    "uris": [        "/user"    ],    "upstream_send_timeout": 60000,    "upstream_connect_timeout": 60000,    "upstream_read_timeout": 60000,    "retries": 5,    "https_only": false}

添加steams

POST http://192.168.0.181:8001/upstreams/#参数name:spring-boot-consul-service2.service.dc1.consul
{    "created_at": 1515055993113,    "orderlist": [        41,        28,        44,        14,        79,        38,        30,        69,        15,        88,        72,        53,        89,        47,        81,        43,        60,        91,        92,        100,        98,        55,        12,        40,        9,        31,        84,        24,        36,        39,        96,        74,        6,        8,        93,        99,        78,        34,        49,        37,        61,        76,        33,        35,        32,        77,        70,        26,        52,        87,        73,        23,        57,        25,        17,        90,        62,        82,        19,        75,        50,        4,        51,        42,        65,        66,        63,        3,        1,        68,        94,        67,        20,        80,        54,        7,        5,        16,        85,        13,        46,        10,        97,        22,        48,        11,        27,        86,        18,        59,        83,        21,        56,        95,        2,        64,        58,        71,        45,        29    ],    "id": "6b8bc38a-d449-47bf-8b8c-19b888e282d4",    "name": "spring-boot-consul-service2.service.dc1.consul",    "slots": 100}

添加targets

POST http://192.168.0.181:8001/upstreams/spring-boot-consul-service2.service.dc1.consul/targets#参数target:192.168.0.184:17004weight:15target:192.168.0.185:17004weight:15target:192.168.0.186:17004weight:15

tagets信息

{    "total": 3,    "data": [        {            "created_at": 1515056103599,            "id": "994e7886-3e48-45cb-80ad-3f7918ede007",            "upstream_id": "6b8bc38a-d449-47bf-8b8c-19b888e282d4",            "target": "192.168.0.186:17004",            "weight": 15        },        {            "created_at": 1515056109570,            "id": "03b477ee-a69d-49ee-9892-276f451c373c",            "upstream_id": "6b8bc38a-d449-47bf-8b8c-19b888e282d4",            "target": "192.168.0.185:17004",            "weight": 15        },        {            "created_at": 1515056112730,            "id": "8ace8d83-4504-4334-ad04-f0b536963df9",            "upstream_id": "6b8bc38a-d449-47bf-8b8c-19b888e282d4",            "target": "192.168.0.184:17004",            "weight": 15        }    ]}

目前Api列表如下:

{    "total": 2,    "data": [        {            "created_at": 1515055457572,            "strip_uri": true,            "id": "a99919d1-0770-40a1-81a9-e3c31fd9d282",            "name": "spring-boot-consul-service2",            "methods": [                "GET"            ],            "http_if_terminated": false,            "https_only": false,            "upstream_url": "http://spring-boot-consul-service2.service.dc1.consul:17004",            "uris": [                "/user"            ],            "preserve_host": false,            "upstream_connect_timeout": 60000,            "upstream_read_timeout": 60000,            "upstream_send_timeout": 60000,            "retries": 5        },        {            "created_at": 1514995615974,            "strip_uri": true,            "id": "b8431831-6a77-434e-ac4a-7e379550714f",            "name": "spring-boot-consul-service",            "methods": [                "GET"            ],            "http_if_terminated": false,            "https_only": false,            "upstream_url": "http://spring-boot-consul-service.service.dc1.consul:17003",            "uris": [                "/service"            ],            "preserve_host": false,            "upstream_connect_timeout": 60000,            "upstream_read_timeout": 60000,            "upstream_send_timeout": 60000,            "retries": 5        }    ]}

访问接口分别返回如下:

http://192.168.0.181:8000/user/user/getUser?id=1&name=tree&age=12
{    "msg": "ok",     "date": {        "score": 42,         "name": "tree",         "id": 1,         "age": 12,         "nowTime": 1515060166929,         "info": "/checkUser, host:192.168.0.186, service_id:application-1700417004"    },     "code": 200}
http://192.168.0.181:8000/service/user/getUser?id=1&name=tree&age=12
{    "msg": "ok",    "date": {        "score": 505,        "name": "tree",        "id": 1,        "age": 12,        "nowTime": 1515060402392,        "info": "/checkUser, host:192.168.0.184, service_id:application-1700317003"    },    "code": 200}

添加Key-Auth插件

开启key-auth插件
POST http://192.168.0.181:8001/apis/spring-boot-consul-service2/plugins#参数name:key-auth
{	"created_at": 1515066239000,	"config": {		"key_in_body": false,		"run_on_preflight": true,		"anonymous": "",		"hide_credentials": false,		"key_names": [			"apikey"		]	},	"id": "78c519d3-11f7-4cdf-b038-9f17f1a83427",	"enabled": true,	"api_id": "a99919d1-0770-40a1-81a9-e3c31fd9d282",	"name": "key-auth"}

然后访问接口提示错误如下错误:

GET http://192.168.0.181:8000/user/user/getUser?id=1&name=tree&age=12#输出{    "message": "No API key found in request"}
删除key-auth插件
DELETE http://192.168.0.181:8001/apis/spring-boot-consul-service2/plugins/78c519d3-11f7-4cdf-b038-9f17f1a83427
查看Api绑定插件
GET http://192.168.0.181:8001/apis/spring-boot-consul-service2/plugins/# 输出:{    "total": 0,    "data": []}

添加限流插件rate-limiting

POST http://192.168.0.181:8001/apis/spring-boot-consul-service2/plugins#参数name:rate-limitingconfig.second:2config.minute:10

输出

{    "created_at": 1515068825000,    "config": {        "minute": 10,        "policy": "cluster",        "redis_timeout": 2000,        "hide_client_headers": false,        "second": 2,        "limit_by": "consumer",        "redis_port": 6379,        "redis_database": 0,        "fault_tolerant": true    },    "id": "482c627f-4041-4320-9b2f-0cc7722b92ea",    "name": "rate-limiting",    "api_id": "a99919d1-0770-40a1-81a9-e3c31fd9d282",    "enabled": true}

访问接口超过限制时会有如下返回:

GET  http://192.168.0.181:8000/user/user/getUser?id=1&name=tree&age=12

返回

{    "message": "API rate limit exceeded"}

但还有些问题,由于消息格式固定,与后端服务的输出格式不统一,需要支持自定义消息格式。

Kong修改异常返回值

比如服务统一消息格式为,如下格式:

{    "code": 200,    "msg": "ok",	"data": {xxx}}

我们也希望kong的异常返回也能保持如上格式。那么我们可以直接修改kong的源码,其实很简单。

首先下载源码
#下载kong插件源码git clone git@github.com:Kong/kong.git
找到输出代码进行修改

输出代码位于:kong/kong/tools/responses.lua中,打开进行编辑。

encoded, err = cjson.encode(type(content) == "table" and content or {message = content})#调整为:encoded, err = cjson.encode(type(content) == "table" and content or {msg = content, code = status_code})

保存,然后进行编译

[root@tree81 kong]# luarocks makekong 0.11.2-0 is now installed in /usr/local (license: MIT)
启动,重试

连续快速刷新几次地址: 得到错误信息:

{    "code": 429,    "msg": "API rate limit exceeded"}

搞定,完美:)

转载于:https://my.oschina.net/tree/blog/1603474

你可能感兴趣的文章
移动医疗大发展 英特尔能做的很多
查看>>
浪潮NFV助力运营商网络转型
查看>>
领跑全球安全行业,为什么是以色列?
查看>>
云计算如何测“benchmark”?
查看>>
10个典型的大数据案例
查看>>
搭载高德地图车机版的天之眼智能后视镜开售 高德用户可享免单
查看>>
2017全球智慧城市战略指数分析
查看>>
Salesforce将收购市场营销数据软件公司Krux
查看>>
《中国人工智能学会通讯》——1.37 快速增长的禁止呼声
查看>>
存储极客 | 4KB扇区硬盘来了,RAID、VMware兼容不?
查看>>
微服务实战:从架构到发布(二)
查看>>
震惊!误删数据也上瘾?原来有和力记易这个法宝
查看>>
互联网巨头加码云计算,出现了这四个微妙的现象
查看>>
没有这个黑客 就没有开源软件
查看>>
扎克伯格又被黑,黑客还是同一人?
查看>>
爱立信牵头欧洲5GCAR项目
查看>>
来真格的!IBM开出500万美元巨奖推Watson
查看>>
安全自动化是企业安全“一劳永逸”之法吗?
查看>>
多系统管理问题显现 一体化管理走俏
查看>>
最新解决Discuz!X3.4以下版本前台打开卡慢、后台登陆卡死或504错误等问题
查看>>