在開始前,請確保您已經安裝了KONG服務,並且已經啟動了KONG服務。
在本節中,您可以學習到:如何在KONG層添加一個API。這是您使用KONG來管理您的API的第一步。對於此篇教程,我們將使用 http://www.baidu.com 來測試KONG。
KONG在 8001 端口上提供了一個 RESTful 形式的管理API,用於管理您的Kong實例或群集的配置。
1. 通過管理員添加您的API:
通過以下cURL來添加您的第一個API(http://www.baidu.com):
$ curl -i -X POST \ --url http://localhost:8001/apis/ \ --data 'name=example-api' \ --data 'hosts=example.com' \ --data 'upstream_url=http://www.baidu.com'
2. 確認您的API已經添加到KONG里了:
通過以上的操作,您將會看到以下的返回信息:
HTTP/1.1 201 Created Content-Type: application/json Connection: keep-alive { "created_at": 1488830759000, "hosts": [ "example.com" ], "http_if_terminated": true, "https_only": false, "id": "6378122c-a0a1-438d-a5c6-efabae9fb969", "name": "example-api", "preserve_host": false, "retries": 5, "strip_uri": true, "upstream_connect_timeout": 60000, "upstream_read_timeout": 60000, "upstream_send_timeout": 60000, "upstream_url": "http://httpbin.org" }
現在,KONG里已經添加了您的API並做好了代理的准備。
3. 通過Kong轉發您的請求:
發出以下cURL請求以驗證Kong是否將請求正確的轉發到了您的API。請注意,默認情況下,Kong是在 8000 端口上處理代理請求的:
$ curl -i -X GET \ --url http://localhost:8000/ \ --header 'Host: example.com'
一個成功的相應,意味着KONG已經將(http://localhost:8000)請求轉發到了我們在第一步中配置的 upstream_url 里,並將response轉發給我們了。
KONG通過上述請求中,定義的URL頭來做到這一點:
- Host: <given host>
