三、Kong Admin API
部署好 Kong 之后,則需要將我們自己的接口加入到 Kong 的中管理,Kong 提供了比較全面的RESTful API,每個版本會有所不同,詳細可以參考官網(wǎng):docs.konghq.com/2.0.x/admin… 。Kong 管理 API 的端口是 8001(8444),服務(wù)、路由、配置都是通過這個端口進行管理,所以部署好之后頁面可以直接訪問 http://102.168.1.200:8001
這里我們先來了解一下如何使用 RESTful 管理接口來管理 Service (服務(wù))、Route(路由)。
-
添加一個Service
curl -i -X POST http://localhost:8001/services --data 'name=hello-service' --data 'url=http://localhost:9000/hello'
curl -i -X POST --url http://localhost:8001/services/ --data 'name=example-service' --data 'url=http://www.baidu.com/'
2.查詢Service
curl http://192.168.1.200:8001/services/hello-service
curl -i -X POST --url http://localhost:8001/services/example-service/routes --data 'hosts[]=example.com'
3.為 Service 添加一個 Route
curl -i -X POST --url http://192.168.1.200:8001/services/hello-service/routes --data 'paths[]=/hello' --data name=hello-route
4.測試
我們可以通過訪問 http://192.168.1.200:8000/hello 來驗證一下配置是否正確。
Kong代理:
url http://192.168.1.200:8000/hello
Hello world !
真實服務(wù): http://192.168.1.200:9000/hello/
前面的操作就等效于配置 nginx.conf:
server {
listen 8000;
location /hello {
proxy_pass http://192.168.1.200:9000/hello;
}
}
不過,前面的配置操作都是動態(tài)的,無需像 Nginx一樣需要重啟。
Service是抽象層面的服務(wù),它可以直接映射到一個物理服務(wù),也可以指向一個Upstream(同Nginx中的Upstream,是對上游服務(wù)器的抽象)。Route是路由的抽象,它負責(zé)將實際的請求映射到 Service。除了Serivce、Route之外,還有 Tag、Consumer、Plugin、Certificate、SNI、Upstream、Target等,讀者可以從 官網(wǎng)的介紹文檔 中了解全貌。
下面在演示一個例子,修改 Service,將其映射到一個 Upstream:
添加 name為 hello-upstream 的 Upstream
curl -i -X POST http://192.168.1.200:8001/upstreams --data name=hello-upstream
為 mock-upstream 添加 Target,Target 代表了一個物理服務(wù)(IP地址/hostname + port的抽象),一個Upstream可以包含多個Targets
curl -i -X POST http://192.168.1.200:8001/upstreams/hello-upstream/targets --data target="192.168.1.200:9000"
修改 hello-service,為其配置
curl -i -X PATCH http://192.168.1.200:8001/services/hello-service --data url='http://hello-upstream/hello'
上面的配置等同于 Nginx 中的nginx.conf配置 :
upstream hello-upstream{
server 192.168.1.200:8001;
}
server {
listen 8000;
location /hello {
proxy_pass http://hello-upstream/hello;
}
}
這里的配置我們也可以通過管理界面來操作。上面操作完之后,在Konga中也有相關(guān)信息展示出來:
四、Kong Plugins
Kong通過插件Plugins實現(xiàn)日志記錄、安全檢測、性能監(jiān)控和負載均衡等功能。下面我將演示一個例子,通過啟動 apikey 實現(xiàn)簡單網(wǎng)關(guān)安全檢驗。
-
配置 key-auth 插件
curl -i -X POST http://192.168.1.200:8001/routes/hello-route/plugins --data name=key-aut
這個插件接收 config.key_names 定義參數(shù),默認參數(shù)名稱 ['apikey']。在 HTTP 請求中 header 和 params 參數(shù)中包含 apikey 參數(shù),參數(shù)值必須 apikey 密鑰,Kong 網(wǎng)關(guān)將堅持密鑰驗證通過才可以訪問后續(xù)服務(wù)。
此時我們使用 curl -i http://192.168.1.200:8000/hello/ 來驗證一下是否生效,如果如下所示,訪問失敗(HTTP/1.1 401 Unauthorized,"No API key found in request" ),說明 Kong 安全機制生效了。
在 Konga 中我們也可以看到相關(guān)記錄:
2.為Service添加服務(wù)消費者(Consumer)
定義消費者訪問 API Key, 讓他擁有訪問 hello-service 的權(quán)限。
創(chuàng)建消費者 Hidden:
curl -i -X POST http://192.168.1.200:8001/consumers/ --data username=Hidden
創(chuàng)建成功之后,返回:
{"custom_id":null,"created_at":1678268549,"id":"e097fc26-ccd0-4fa7-b370-f6eec4797d5f","tags":null,"username":"Hidden"}
之后為消費者 Hidden 創(chuàng)建一個 api key,輸入如下命令:
curl -i -X POST http://192.168.1.200:8001/consumers/Hidden/key-auth/ --data key=123456
{"created_at":1678268632,"consumer":{"id":"e097fc26-ccd0-4fa7-b370-f6eec4797d5f"},"id":"d560969d-ff8b-4204-a09f-3474217a4a29","tags":null,"ttl":null,"key":"123456"}
現(xiàn)在我們再來驗證一下 http://192.168.1.200:8000/hello:
curl -i http://192.168.1.200:8000/hello/ --header "apikey:123456"
返回:
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 14
Connection: keep-alive
Date: Tue, 18 Oct 2022 01:32:30 GMT
Server: WSGIServer/0.1 Python/2.7.5
X-Frame-Options: SAMEORIGIN
X-Kong-Upstream-Latency: 13
X-Kong-Proxy-Latency: 19
Via: kong/1.5.1
Hello world !
其他的插件可選
或者進入插件倉庫去了解
https://docs.konghq.com/hub/
Kong 網(wǎng)關(guān)插件概括為如下:
- 身份認證插件:Kong 提供了 Basic Authentication、Key authentication、OAuth2.0 authentication、HMAC authentication、JWT、LDAP authentication 認證實現(xiàn)。
- 安全控制插件:ACL(訪問控制)、CORS(跨域資源共享)、動態(tài) SSL、IP 限制、爬蟲檢測實現(xiàn)。
- 流量控制插件:請求限流(基于請求計數(shù)限流)、上游響應(yīng)限流(根據(jù) upstream 響應(yīng)計數(shù)限流)、請求大小限制。限流支持本地、Redis 和集群限流模式
- 分析監(jiān)控插件:Galileo(記錄請求和響應(yīng)數(shù)據(jù),實現(xiàn) API 分析)、Datadog(記錄 API Metric 如請求次數(shù)、請求大小、響應(yīng)狀態(tài)和延遲,可視化 API Metric)、Runscope(記錄請求和響應(yīng)數(shù)據(jù),實現(xiàn) API 性能測試和監(jiān)控)。
- 協(xié)議轉(zhuǎn)換插件:請求轉(zhuǎn)換(在轉(zhuǎn)發(fā)到 upstream 之前修改請求)、響應(yīng)轉(zhuǎn)換(在 upstream 響應(yīng)返回給客戶端之前修改響應(yīng))。
- 日志應(yīng)用插件:TCP、UDP、HTTP、File、Syslog、StatsD、Loggly 等。
五、總結(jié)
Kong 作為 API 網(wǎng)關(guān)提供了 API 管理功能及圍繞 API 管理實現(xiàn)了一些默認的插件,另外還具備集群水平擴展能力,從而提升整體吞吐量。Kong 本身是基于 OpenResty,可以在現(xiàn)有 Kong 的基礎(chǔ)上進行一些擴展,從而實現(xiàn)更復(fù)雜的特性。雖然有一些特性 Kong 默認是缺失的,如 API 級別的超時、重試、fallback 策略、緩存、API 聚合、AB 測試等,這些功能插件需要企業(yè)開發(fā)人員通過 Lua 語言進行定制和擴展。綜上所述,Kong API 網(wǎng)關(guān)默認提供的插件比較豐富, 適應(yīng)針對企業(yè)級的 API 網(wǎng)關(guān)定位。
-
網(wǎng)關(guān)
+關(guān)注
關(guān)注
9文章
5225瀏覽量
52406 -
API
+關(guān)注
關(guān)注
2文章
1559瀏覽量
63490 -
nginx
+關(guān)注
關(guān)注
0文章
163瀏覽量
12481 -
go語言
+關(guān)注
關(guān)注
1文章
158瀏覽量
9276
發(fā)布評論請先 登錄
API信息全掌控,方便你的日志管理——阿里云推出API網(wǎng)關(guān)打通日志服務(wù)
AI邊緣計算網(wǎng)關(guān)介紹
介紹ZigBee網(wǎng)關(guān)的架構(gòu)
介紹LoRaWAN網(wǎng)關(guān)的技術(shù)基礎(chǔ)及其工作原理
基于Python的商標(biāo)信息查詢api調(diào)用代碼實例的詳細資料免費下載

ESP8266 Node MCU-API 函數(shù)的詳細中文說明
什么是API網(wǎng)關(guān)為什么需要API網(wǎng)關(guān)

Service Mesh和API網(wǎng)關(guān)正在逐步融合
關(guān)于API網(wǎng)關(guān)策略的知識分享
API 網(wǎng)關(guān)詳細介紹(上)

為什么需要 API 網(wǎng)關(guān)?

企業(yè)怎么選擇API網(wǎng)關(guān)

api網(wǎng)關(guān) kong 教程入門

評論