网站首页 > 技术文章 正文
Kong组件构成
在真正开始使用之前,先了解了一下在Kong中的所用组件,并对他们的关系做了一个初步了解,
下面就将一下Kong中常用的组件简单做下了解
- Service:
Service 顾名思义,就是我们自己定义的上游服务,通过Kong匹配到相应的请求要转发的地方
Service 可以与下面的Route进行关联,一个Service可以有很多Route,匹配到的Route就会转发到Service中,
当然中间也会通过Plugin的处理,增加或者减少一些相应的Header或者其他信息
Service可以是一个实际的地址,也可以是Kong内部提供的upstream object
- Route:
Route 字面意思就是路由,实际就是我们通过定义一些规则来匹配客户端的请求,每个路由都会关联一个Service,
并且Service可以关联多个Route,当匹配到客户端的请求时,每个请求都会被代理到其配置的Service中
Route作为客户端的入口,通过将Route和Service的松耦合,可以通过hosts path等规则的配置,最终让请求到不同的Service中
例如,我们规定api.example.com 和 api.service.com的登录请求都能够代理到123.11.11.11:8000端口上,那我们可以通过hosts和path来路由
首先,创建一个Service s1,其相应的host和port以及协议为http://123.11.11.11:8000
然后,创建一个Route,关联的Service为s1,其hosts为[api.service.com, api.example.com],path为login
最后,将域名api.example.com和api.service.com的请求转到到我们的Kong集群上,也就是我们上面一节中通过Nginx配置的请求地址
那么,当我们请求api.example.com/login和api.service.com/login时,其通过Route匹配,然后转发到Service,最终将会请求我们自己的服务。
- Upstream:
这是指您自己的API /服务位于Kong后面,客户端请求被转发到该服务器。
相当于Kong提供了一个负载的功能,基于Nginx的虚拟主机的方式做的负载功能
当我们部署集群时,一个单独的地址不足以满足我们的时候,我们可以使用Kong的upstream来进行设置
首先在service中指定host的时候,可以指定为我们的upstream定义的hostname
我们在创建upstream时指定名字,然后指定solts(暂时不确定具体作用),upstream可以进行健康 检查等系列操作。这里先不开启(还没有研究)
然后我们可以再创建target类型,将target绑定到upstream上,那么基本上我们部署集群时,也可以使用
- Target:
target 就是在upstream进行负载均衡的终端,当我们部署集群时,需要将每个节点作为一个target,并设置负载的权重,当然也可以通过upstream的设置对target进行健康检查。
当我们使用upstream时,整个路线是 Route >> Service >> Upstream >> Target
- Consumer:
Consumer 可以代表一个服务,可以代表一个用户,也可以代表消费者,可以根据我们自己的需求来定义
可以将一个Consumer对应到实际应用中的一个用户,也可以只是作为一个Service的请求消费者
Consumer具体可以在Plugin使用时再做深入了解
- Plugin:
在请求被代理到上游API之前或之后执行Kong内的动作的插件。
例如,请求之前的Authentication或者是请求限流插件的使用
Plugin可以和Service绑定,也可以和Route以及Consumer进行关联。
具体的使用可以根据在创建Plugin以及后面的修改时,具体与Consumer,Service,Route绑定关系时,可参考
另外,还有一些其他的对象,本次没有具体使用,所以不再做具体的研究,如果使用可以到官网提供的资料具体查看
配置
- 配置Service
Service还有一些其他参数,可根据具体需求定制。
- 添加命令
curl -i -X POST \ --url http://localhost:8001/services/ \ --data 'name=example-service' \ --data 'protocol=http' \ --data 'host=api.example.service' 修改原来的host到我们自己的upstream curl -i -X PATCH \ --url http://localhost:8001/services/e860ebf3-db9f-41ed-a7cc-d32a5f2a5b46/ \ --data 'name=user-service' \ --data 'protocol=http' \ --data 'host=wechat.user.service'
- 添加结果
{
"host": "192.168.0.10",
"created_at": 1527759319,
"connect_timeout": 60000,
"id": "78adee90-dc14-418d-940c-590bc385212e",
"protocol": "http",
"name": "example-service",
"read_timeout": 60000,
"port": 9200,
"path": null,
"updated_at": 1527759319,
"retries": 5,
"write_timeout": 60000
}- 配置Route
Route还有一些其他参数,可根据具体需求定制。
- 添加命令
curl -i -X POST \ --url http://localhost:8001/routes/ \ --data 'protocols[]=http' \ --data 'protocols[]=https' \ --data 'methods[]=GET' \ --data 'methods[]=POST' \ --data 'hosts[]=api.example.com' \ --data 'paths[]=/foo' \ --data 'service.id=78adee90-dc14-418d-940c-590bc385212e' # 使用添加service产生的id
- 添加结果
{
"created_at": 1527761224,
"strip_path": true, # 此属性代表是否清除原有的path,默认是清除,如果不需要清除需要再添加的时候指定为false
"hosts": ["api.example.com"],
"preserve_host": false,
"regex_priority": 0,
"updated_at": 1527761224,
"paths": ["\/foo"],
"service": {
"id": "78adee90-dc14-418d-940c-590bc385212e"
},
"methods": ["GET", "POST"],
"protocols": ["http", "https"],
"id": "d8b2a353-fdfe-48f8-b367-5bec6eb97451"
}- 配置Upstream
- 添加命令
curl -i -X POST \ --url http://localhost:8001/upstreams/ \ --data 'name=wechat.user.service'
- 添加结果
{
"created_at": 1528355189269,
"hash_on": "none",
"id": "9ee28b3d-4c83-4046-953c-ac571f1b2acf",
"healthchecks": {
"active": {
"unhealthy": {
"http_statuses": [429, 404, 500, 501, 502, 503, 504, 505],
"tcp_failures": 0,
"timeouts": 0,
"http_failures": 0,
"interval": 0
},
"http_path": "\/",
"healthy": {
"http_statuses": [200, 302],
"interval": 0,
"successes": 0
},
"timeout": 1,
"concurrency": 10
},
"passive": {
"unhealthy": {
"http_failures": 0,
"http_statuses": [429, 500, 503],
"tcp_failures": 0,
"timeouts": 0
},
"healthy": {
"successes": 0,
"http_statuses": [200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308]
}
}
},
"name": "wechat.user.service",
"hash_fallback": "none",
"slots": 10000
}- 配置target
- 添加命令
curl -i -X POST \ --url http://localhost:8001/upstreams/9ee28b3d-4c83-4046-953c-ac571f1b2acf/targets \ --data 'target=147.94.209.20:7110' curl -i -X POST \ --url http://localhost:8001/upstreams/9ee28b3d-4c83-4046-953c-ac571f1b2acf/targets \ --data 'target=147.94.209.20:7111'
- 添加结果
{
"created_at": 1528356082786,
"weight": 100,
"upstream_id": "9ee28b3d-4c83-4046-953c-ac571f1b2acf",
"target": "147.94.209.20:7110",
"id": "34d13cca-4d93-4392-80c9-3287c77bed9c"
}
{
"created_at": 1528356100946,
"weight": 100,
"upstream_id": "9ee28b3d-4c83-4046-953c-ac571f1b2acf",
"target": "147.94.209.20:7111",
"id": "d3d24191-49d8-44ce-a3cb-c67b2cb7ed9d"
}
猜你喜欢
- 2024-10-23 微软发布6月Win11累积更新KB5039212/KB5039213
- 2024-10-23 快速体验之《gor+diffy实现线上流量复制到测试环境》
- 2024-10-23 Colbie Caillat: Try(colbiecaillattry歌词)
- 2024-10-23 基于阿里云 ASK 的 Istio 微服务应用部署初探
- 2024-10-23 浅谈ElasticSearch 集群部署(elastic集群配置)
- 2024-10-23 Python项目中跟踪系统导入Zipkin(基于python的目标跟踪算法)
- 2024-10-23 JVM参数及调优(jvm调优常用参数)
- 2024-10-23 Elasticsearch的路由routing的应用技巧
- 2024-10-23 如何将Elasticsearch的快照备份至OSS
- 2024-10-23 利用工具curl来查看http请求和https请求
- 最近发表
-
- 聊一下 gRPC 的 C++ 异步编程_grpc 异步流模式
- [原创首发]安全日志管理中心实战(3)——开源NIDS之suricata部署
- 超详细手把手搭建在ubuntu系统的FFmpeg环境
- Nginx运维之路(Docker多段构建新版本并增加第三方模
- 92.1K小星星,一款开源免费的远程桌面,让你告别付费远程控制!
- Go 人脸识别教程_piwigo人脸识别
- 安卓手机安装Termux——搭建移动服务器
- ubuntu 安装开发环境(c/c++ 15)_ubuntu安装c++编译器
- Rust开发环境搭建指南:从安装到镜像配置的零坑实践
- Windows系统安装VirtualBox构造本地Linux开发环境
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (77)
- vector线程安全吗 (73)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)
