1. 程式人生 > >API 閘道器 Kong

API 閘道器 Kong

## 什麼是 API 閘道器? 所謂閘道器,主要作用就是連線兩個不同網路的裝置,而今天所講的 API 閘道器是指承接和分發客戶端所有請求的閘道器層。 ![](https://img2020.cnblogs.com/other/1471773/202005/1471773-20200516220827943-107175378.png) 為什麼需要閘道器層?最初是單體服務時,客戶端發起的所有請求都可以直接請求到該服務,但隨著產品使用者越來越多,單體應用存在顯而易見的單點問題,除此之外,當單體應用大小升至幾個 G 時,持續釋出將會非常緩慢,所以服務的拆分成為了必然趨勢。 ![](https://img2020.cnblogs.com/other/1471773/202005/1471773-20200516220828293-787809777.png) 當服務拆分為多個之後,我們不得不面臨一個問題,就是如何控制使用者請求到對應服務節點,於是閘道器層應運而生,它不僅可以負責負載均衡,還可以讓它處理認證校驗、請求限流、日誌記錄以及監控服務節點等等。 ![](https://img2020.cnblogs.com/other/1471773/202005/1471773-20200516220828573-2120919608.png) 當然,閘道器層並不需要我們手動實現,市面上有很多 API 閘道器開源專案,比如 Zuul、Kong、Tyk 等,今天主要介紹 Kong。 ## 安裝 Kong Kong 是一個在 Nginx 中執行的 Lua 程式,由 lua-nginx-module 實現,和 Openresty 一起打包發行,支援多種操作環境下的安裝,可以用來做 HTTP 基本認證、金鑰認證、TCP、UDP、檔案日誌、API 請求限流、請求轉發等等。 第一步,建立一個 docker 網路。 ```bash $ docker network create kong-net ``` 建立用於儲存 Kong 資料的資料庫,可以使用 Cassandra 或 PostgreSQL,本示例採用 Cassandra。 > Cassandra 是由 Facebook 開發的分散式 NoSQL 資料庫。 ```bash $ docker run -d --name kong-database \ --network=kong-net \ -p 9042:9042 \ cassandra:3 ``` 初始化資料到 Cassandra 資料庫。 ```bash $ docker run --rm \ --network=kong-net \ -e "KONG_DATABASE=cassandra" \ -e "KONG_PG_HOST=kong-database" \ -e "KONG_PG_PASSWORD=kong" \ -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ kong:latest kong migrations bootstrap ``` 啟動 Kong 容器連線資料庫。 ```bash $ docker run -d --name kong \ --network=kong-net \ -e "KONG_DATABASE=cassandra" \ -e "KONG_PG_HOST=kong-database" \ -e "KONG_PG_PASSWORD=kong" \ -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \ -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \ -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \ -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \ -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \ -p 8000:8000 \ -p 8443:8443 \ -p 0.0.0.0:8001:8001 \ -p 0.0.0.0:8444:8444 \ kong:latest ``` 通過 curl 模擬請求本地 8001 埠可以獲取 Kong 的詳細資訊。截止目前整個 kong 服務就跑起來了,接下來可以註冊服務節點到 kong 中。 ```bash $ curl -i http://localhost:8001/ ``` ## 註冊服務 註冊名為 baidu-service 的服務到 kong,如果請求匹配到該服務會跳轉至 url。 ```bash $ curl -i -X POST \ --url http://localhost:8001/services/ \ --data 'name=baidu-service' \ --data 'url=http://baidu.com' ``` 註冊成功之後會得到如下提示。 ```bash HTTP/1.1 201 Created Date: Sat, 16 May 2020 06:35:56 GMT Content-Type: application/json; charset=utf-8 Connection: keep-alive Access-Control-Allow-Origin: * Server: kong/2.0.4 Content-Length: 292 X-Kong-Admin-Latency: 103 {"host":"baidu.com","created_at":1589610956,"connect_timeout":60000,"id":"6660aaa7-5afa-4f02-85f8-11dfb81fba84","protocol":"http","name":"baidu-service","read_timeout":60000,"port":80,"path":null,"updated_at":1589610956,"retries":5,"write_timeout":60000,"tags":null,"client_certificate":null} ``` ## 服務配置路由 服務新增成功後,需要告知 kong 什麼樣的請求才使用該服務,而這個規則被稱為 route,也就是路由,路由的作用是按規則匹配客戶端的請求,然後轉發到對應的 service,每個 route 都對應一個 service,一個 service 可能有多個 route。 ```bash $ curl -i -X POST \ --url http://localhost:8001/services/baidu-service/routes \ --data 'hosts[]=baidu.com' ``` 以上程式碼的作用是當請求域名是 baidu.com 時,則將請求轉發到 baidu-service 指定的 url,我們可以通過 curl 模擬測試。 ```bash $ curl -i -X GET \ --url http://localhost:8000/ \ --header 'Host: baidu.com' ``` ```bash HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Content-Length: 81 Connection: keep-alive Date: Sat, 16 May 2020 06:45:12 GMT Server: Apache Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT ETag: "51-47cf7e6ee8400" Accept-Ranges: bytes Cache-Control: max-age=86400 Expires: Sun, 17 May 2020 06:45:12 GMT X-Kong-Upstream-Latency: 92 X-Kong-Proxy-Latency: 17 Via: kong/2.0.4
``` ## 負載均衡配置 與 nginx 同理,先建立一個 upstream,名為 hello。 ```bash $ curl -X POST http://localhost:8001/upstreams --data "name=hello" ``` ```bash {"created_at":1589633009,"hash_on":"none","id":"3bab80bb-7e62-40c8-8b7c-7efdcc329675","algorithm":"round-robin","name":"hello","tags":null,"hash_fallback_header":null,"hash_fallback":"none","hash_on_cookie":null,"host_header":null,"hash_on_cookie_path":"\/","healthchecks":{"threshold":0,"active":{"https_verify_certificate":true,"type":"http","http_path":"\/","timeout":1,"unhealthy":{"http_statuses":[429,404,500,501,502,503,504,505],"tcp_failures":0,"timeouts":0,"http_failures":0,"interval":0},"healthy":{"http_statuses":[200,302],"interval":0,"successes":0},"https_sni":null,"concurrency":10},"passive":{"unhealthy":{"http_failures":0,"http_statuses":[429,500,503],"tcp_failures":0,"timeouts":0},"healthy":{"http_statuses":[200,201,202,203,204,205,206,207,208,226,300,301,302,303,304,305,306,307,308],"successes":0},"type":"http"}},"hash_on_header":null,"slots":10000} ``` 為 upstream 新增兩個負載均衡的節點,我就拿京東和淘寶來做測試了。 ```bash curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=jd.com" --data "weight=100" curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=taobao.com" --data "weight=50" ``` 如上配置就相當於 nginx 中的 ```bash upstream hello { server jd.com weight=100; server taobao.com weight=50; } ``` 接下來建立一個 service 指向 upstream,host 即對應 upstream 名。 ```bash curl -X POST http://localhost:8001/services --data "name=hello" --data "host=hello" ``` 為 service 建立路由,凡是包含 /hello 的地址全部走 hello upstream。 ```bash curl -i -X POST --url http://localhost:8001/services/hello/routes --data 'paths[]=/hello' curl -X POST --url http://localhost:8001/routes --data 'paths[]=/hello' --data 'service.id=8ad06aa5-be0a-4763-a84d-90b8046765f5' ``` 現在訪問 localhost:8000 就可以看到已經成功做了負載均衡。 ## 限流 因為伺服器不能處理無限量的請求,為了避免請求數高到伺服器無法處理,必須做限流操作。關於限流,kong 使用了 rate-limiting 外掛,它可以在 service、route、consumer 不同粒度上控制請求,配置非常簡單。 ```bash curl -X POST http://kong:8001/services/{service}/plugins \ --data "name=rate-limiting" --data "config.second=5" \ --data "config.hour=10000" ``` config.second 代表一秒鐘內處理的最大請求次數,config.hour 表示一小時內最大請求次數。 Route 的示例 ```bash curl -X POST http://kong:8001/routes/{route}/plugins \ --data "name=rate-limiting" --data "config.second=5" \ --data "config.hour=10000" ``` Consumer 的示例 ```bash curl -X POST http://kong:8001/consumers/{consumer}/plugins \ --data "name=rate-limiting-advanced" \ --data "config.second=5" \ --data "config.hour=10000" ``` >
原文首發:https://pingyeaa.com/2020/05/16/architecture/gateway-kong/ --- > 我是平也,這有一個專注Gopher技術成長的開源專案[「go home」](https://github.com/pingyeaa/go-home) --- 感謝大家的觀看,如果覺得文章對你有所幫助,歡迎關注公眾號「平也」,聚焦Go語言與技術原理。 ![關注我](https://img2020.cnblogs.com/other/1471773/202005/1471773-20200516220831048-311146