随笔 - 836  文章 - 1 评论 - 40 阅读 - 102万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

参考:https://www.cnblogs.com/chenjinxi/p/8724564.html

 

 

一、简介

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/



作者:peng66cheng
链接:https://www.jianshu.com/p/f9a2210f6722
来源:简书

posted on   lshan  阅读(396)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示