1 Konga UI基本使用
一般访问地址:
http://192.168.1.70:8080
1.1 Step1:创建upstream
1.1.1 【样例】
Name=>VCSAPI_SERVER
Slot=>1000
1.2 Step2:配置upstream target
1.2.1 【样例】
多个节点,配置多个target即可。
Target=>192.168.10.30:8080
Weight=>100
1.3 Step3:添加API Service
1.3.1 【样例】
Name=>VCSAPI-SERVICE
HOST=>VCSAPI_SERVER(注:对应upstream name)
port=>80
path=>/vcs(注:对应目标转发路径,根据实际部署context调整)
1.4 Step4:配置API路由
1.4.1 【样例】
Service=>VCSAPI-SERVICE
paths=>/vcs(注:客户端请求路径,根据实际部署调整)
Preserve Host:true 客户端请求的HOST通过kong透传到上游服务。
Strip path:true,是否删除匹配的url前缀字符。
1.5 Step5:配置consumer
1.5.1 【样例】
username=>icfw
1.6 Step6:配置用户JWT认证
1.6.1 【样例】
End-Point
Key(iss)
Secret
icfw
qD8WKwxQWQZVcdhc05yzqDpRLZMQjMH8
RE1NEaUcWBQFTj0Mxip1hy1PsFBoCVez
注:key secret创建时,可不用输入,由系统自动生成随机字符串。
1.7 Step7:配置服务JWT插件
1.7.1 【样例】
Apply to=>VCSAPI_SERVICE,选择service->plugins->jwt
Key_claim_name=>iss,固定写法 ,对应key
Claims_to_verify=>exp,固定写法,jwt token过期时间,unix时间数值串.(可选)
1.7.2 Jwt token 生成
通过访问https://jwt.io/,生成JWT TOKEN
1.7.3 客户端调用,设置HTTP头(Authorization ):Bearer jwtkoken字符串。
1.8 Step7:配置限流插件
1.8.1 【样例】
Apply to service=>,根据不同场景,选择servcie即可
policy=>redis,限流策略方式,此处用redis
limit_by =>ip,限流维度,此处用以ip限流
second=>10,控制在每秒并发10以内(单ip访问)
redis_password=>redis配置密码
redis_host=>192.168.1.203
redis_database=>0,选择数据库序号,此处为0
配置限流插件,注意数据库PG中的时间,是否与北京时间同步。因为是通过时间事件控制的,如果时间点不对,限流尽管配置了,也不会触发的。
1.9 Step8:服务降级实现
通过Kong实现服务降级思路如下:
1> 梳理出需要走服务降级的接口
2> 按降级时返回数据进行归类,相同的路由到同一个URI(kong配置预先配置的)
3> 访问端走网关的不同路由配置
通过Kong的请求中断插件实现服务降级,减少对后台服务器(api等)的访问压力。
按理已经开启限流策略,可以避免大部分恶意压力***,但不排除模拟真实应用场景的***方式,可以通过服务降级做为补充方案。
1.9.1 【样例】
Apply to=>service,根据不同场景选择指定的service
Status_code=>200,自定义,一般是200
Content_type=>application/json; charset=utf-8,自定义
Response body=>{"result": {},"code": 200,"message": "OK" },需要开发整理
2 访问Kong管理接口
2.1 查看service:http://192.168.20.22:8001/services
{
"next": null,
"data": [
{
"host": "VCSAPI_SERVER",
"created_at": 1551856065,
"connect_timeout": 6000,
"id": "4fc01ba9-6779-477d-8a49-76982650deed",
"protocol": "http",
"name": "VCSAPI_SERVICE",
"read_timeout": 6000,
"port": 80,
"path": "/vcs",
"updated_at": 1551944738,
"retries": 5,
"write_timeout": 6000
},
{
"host": "192.168.10.30",
"created_at": 1551851624,
"connect_timeout": 60000,
"id": "bc64c9c2-7399-4d2d-a616-2311f450160a",
"protocol": "http",
"name": "test-service",
"read_timeout": 60000,
"port": 8080,
"path": "/vcs/index.html",
"updated_at": 1551853564,
"retries": 5,
"write_timeout": 60000
}
]
}
2.2 查看路由(routes):http://192.168.20.22:8001/routes
{
"next": null,
"data": [
{
"created_at": 1551856120,
"methods": [ ],
"id": "ead9e93a-7b74-4528-8df3-fe7963006317",
"service": {
"id": "4fc01ba9-6779-477d-8a49-76982650deed"
},
"name": "vcs",
"hosts": [ ],
"updated_at": 1551861483,
"preserve_host": true,
"regex_priority": 0,
"paths": [
"/vcs"
],
"sources": [ ],
"destinations": [ ],
"snis": [ ],
"protocols": [
"http",
"https"
],
"strip_path": true
},
{
"created_at": 1551851896,
"methods": [ ],
"id": "fd37c027-3ba3-49e1-9543-a4be07d499e2",
"service": {
"id": "bc64c9c2-7399-4d2d-a616-2311f450160a"
},
"name": "test",
"hosts": [ ],
"updated_at": 1551852181,
"preserve_host": false,
"regex_priority": 0,
"paths": [
"/test1"
],
"sources": [ ],
"destinations": [ ],
"snis": [ ],
"protocols": [
"http",
"https"
],
"strip_path": true
}
]
}
2.3 查看upstreams:http://192.168.20.22:8001/upstreams
{
"next": null,
"data": [
{
"created_at": 1551943496,
"hash_on": "none",
"id": "e8910221-7302-4305-a3e6-e02ea5d673b4",
"name": "VCSAPI_SERVER",
"hash_fallback_header": null,
"hash_on_cookie": null,
"healthchecks": {
"active": {
"unhealthy": {
"http_statuses": [
429,
404,
500,
501,
502,
503,
504,
505
],
"tcp_failures": 0,
"timeouts": 0,
"http_failures": 0,
"interval": 0
},
"type": "http",
"http_path": "/",
"timeout": 1,
"healthy": {
"successes": 0,
"interval": 0,
"http_statuses": [
200,
302
]
},
"https_sni": null,
"https_verify_certificate": true,
"concurrency": 10
},
"passive": {
"unhealthy": {
"http_failures": 0,
"http_statuses": [
429,
500,
503
],
"tcp_failures": 0,
"timeouts": 0
},
"healthy": {
"http_statuses": [
200,
201,
202,
203,
204,
205,
206,
207,
208,
226,
300,
301,
302,
303,
304,
305,
306,
307,
308
],
"successes": 0
},
"type": "http"
}
},
"hash_on_cookie_path": "/",
"hash_fallback": "none",
"hash_on_header": null,
"slots": 1000
}
]
}
2.4 查看Pluginshttp://192.168.20.22:8001/plugins
{
"next": null,
"data": [
{
"created_at": 1551945986,
"config": {
"secret_is_base64": false,
"key_claim_name": "iss",
"cookie_names": [ ],
"maximum_expiration": 0,
"claims_to_verify": null,
"anonymous": null,
"run_on_preflight": true,
"uri_param_names": [ ]
},
"id": "26105b8b-90e0-4e02-89b3-758f47ff74ce",
"service": {
"id": "4fc01ba9-6779-477d-8a49-76982650deed"
},
"enabled": true,
"run_on": "first",
"consumer": null,
"route": null,
"name": "jwt"
}
]
}