网维服务器返回错误信息,【微服务】网关Kong使用插件,自定义错误返回-Go语言中文社区...

Kong插件

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

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

应用在消费者;

应用在Api;

应用在全局;

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

再添加一个Api供测试

再添加一个新接口:

POST http://192.168.0.181:8001/apis/

#参数

name:spring-boot-consul-service2

upstream_url:http://spring-boot-consul-service2.service.dc1.consul:17004

methods:GET

uris:/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:17004

weight:15

target:192.168.0.185:17004

weight:15

target:192.168.0.186:17004

weight: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-limiting

config.second:2

config.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 make

kong 0.11.2-0 is now installed in /usr/local (license: MIT)

启动,重试

{

"code": 429,

"msg": "API rate limit exceeded"

}

搞定,完美:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值