架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8479|回复: 0

【微服务】网关Kong使用插件,自定义错误返回

[复制链接]
发表于 2019-2-14 16:58:19 | 显示全部楼层 |阅读模式
Kong插件

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

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

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

再添加一个Api供测试
再添加一个新接口:


{
    "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


{
    "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



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
        }
    ]
}


访问接口分别返回如下:

{
    "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
}
{
    "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插件


{
        "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插件



查看Api绑定插件

GET http://192.168.0.181:8001/apis/spring-boot-consul-service2/plugins/
# 输出:
{
    "total": 0,
    "data": []
}

添加限流插件rate-limiting


输出

{
    "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
}

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


返回

{
    "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中,打开进行编辑。



保存,然后进行编译

[root@tree81 kong]# luarocks make
kong 0.11.2-0 is now installed in /usr/local (license: MIT)

启动,重试

连续快速刷新几次地址:http://192.168.0.181:8000/user/user/getUser?id=1&name=tree&age=12 得到错误信息:

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

搞定,完美:)




上一篇:jQuery通过 AJAX 加载一段文本
下一篇:init: function () jquery对象的构造函数
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-3-29 09:28

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表