一、簡介
?
前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
Kong,是由Mashape公司開源的,基于Nginx的API gateway。
二、特點
可擴展:支持分布式
模塊化 功能:授權、日志、ip限制、限流、api 統計分析(存在商業插件Galileo等、也可自己研發)、請求轉化、跨域(CORS)、其他功能通過lua編寫插件實現。
...
三、調用流程
Once Kong is running, every request being made to the API will hit Kong first, and then it will be proxied to the final API. In between requests and responses Kong will execute any plugin that you decided to install, empowering your APIs. Kong is effectively going to be the entry point for every API request.
譯文:Kong啟動之后,每個請求先經過Kong,然后由Kong代理 訪問最終的API。在請求和響應之間,Kong可以執行任何已配置的插件,達到增強APIs的目的。Kong作為每個API請求的入口。
// 四、Kong技術架構圖
五、測試環境搭建
1 安裝kong
https://getkong.org/install/
2 postgresql安裝配置
http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html
kong的postgresql數據庫連接命令:psql -U kong -d kong -h 127.0.0.1 -p 5432
3 kong配置
https://getkong.org/docs/0.9.x/configuration/
4 kong啟動
kong start -c /etc/kong/kong.conf --vv
遇到錯誤:Error: /usr/local/share/lua/5.1/pgmoon-mashape/init.lua:239: missing password, required for connect
解決方法:檢查配置文件的是否存在pg_password配置。
?六、使用示例
說明:Kong Admin API 默認口為8001
1 在kong中新增api
1.1命令
curl -i -X POST \
--urlhttp://localhost:8001/apis/\
-d 'name=getTeacherById' \
-d 'upstream_url=http://www.daydays.com/' \
-d 'request_path=/**/**/teacher.do'
1.2 原接口請
curl -i -X GET \
--urlhttp://www.daydays.com/**/**/teacher.do?fmid=1031
1.3 通過kong進行接口請求
curl -i -X GET \
--urlhttp://localhost:8000/**/**/teacher.do?fmid=1031
2 增加限速插件
2.1 命令
curl -X POSThttp://localhost:8001/apis/getTeacherById/plugins\
--data "name=rate-limiting" \
--data "config.second=2" \
--data "config.minute=2" \
--data "config.hour=10000"
//2.2 訪問頻率超過限制后,接口返回結果如下圖:
3 訪問控制
3.1 添加key-auth插件,命令如下:
curl -i -X POST \
--urlhttp://localhost:8001/apis/getTeacherById/plugins/\
--data 'name=key-auth'
//此時直接訪問接口,將返回以下錯誤:
3.2 增加消費者:
curl -i -X POST \
--urlhttp://localhost:8001/consumers/\
--data "username=daydaysTeachApp"
3.3 為消費者配置證書
curl -i -X POST \
--urlhttp://localhost:8001/consumers/daydaysTeachApp/key-auth/\
--data 'key=daydaysTeachApp_randomNum123456'
通過key訪問請求
curl -i -X GET ? --urlhttp://localhost:8000/**/**/teacher?fmid=1031\
"apikey: daydaysTeachApp_randomNum123456"
4 在kong中刪除api
curl -i -X DELETE \
--urlhttp://localhost:8001/apis/getTeacherById
七、參考資料
中文資料介紹:https://www.sdk.cn/news/1596
kong官網:https://getkong.org
github:https://github.com/Mashape/kong/
?
?
轉自:https://www.jianshu.com/p/f9a2210f6722
?
?
?