Kong系列(二)——Kong組件使用

Kong組件構成

Kong系列(二)——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還有一些其他參數,可根據具體需求定製。

  1. 添加命令
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'
  1. 添加結果
{
 "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還有一些其他參數,可根據具體需求定製。

  1. 添加命令
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
  1. 添加結果
{
 "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
  1. 添加命令
curl -i -X POST \
 --url http://localhost:8001/upstreams/ \
 --data 'name=wechat.user.service'
  1. 添加結果
{
 "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
  1. 添加命令
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'
  1. 添加結果
{
 "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"
}


分享到:


相關文章: