简介
Kong 是开源的高性能软件网关, 同样基于OpenResty,本质上是在 Nginx 中运行的 Lua 应用程序,通过 lua-nginx 模块实现
安装部署
安装部署请参看官方文档,传送门:kong
8000 #该端口为客户端访问的
8001 #该端口为管理控制台UI入口
web管理控制台方面,官方提供了Kong dashboard(收费). 建议使用konga 传送门:konga 方便日常运维跟操作
核心概念
- Route:配置路由规则,用来转发请求
- Service: 路由对应的微服务
- Upstream: 微服务对应的虚拟域名
- Target: 虚拟域名对应的物理机/容器ip
插件的使用
插件相当于spring中的切面,每个插件有其生效的生命周期,举个例子: auth插件,在请求发完后端前生效. log插件一般是后端处理完请求后生效.
配置在Route上,就仅针对该Route有效.
配置在Service上针对该Service的所有Route.
全局配置,就是全局有效
使用举例
1. 先配置Service, Service名字:demo
管理控制台的端口为8001
curl --location --request POST 'http://localhost:8001/services/' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "demo",
"host": "localhost",
"port": 8001
}'
2. 在Service下面配置route
restful风格,services/demo表示为demo[资源]这个service配置route
建议使用步骤1返回的uuid作为资源
curl --location --request POST 'http://localhost:8001/services/demo/routes' \
--header 'Content-Type: application/json' \
--data-raw '{
"paths": ["/test"]
}'
3. 发个请求验证一下, 客户端访问的端口为8000
curl localhost:8000/test