三分鐘上手基於openresty開發的kong網關係統

kong做爲非java全家桶的一員,在非java領域的網關係統中獲得了普遍的應用java

使用docker一鍵啓動kong

參考項目:github.com/Kong/docker…git

啓動過程以下:github

git clone https://github.com/Kong/docker-kong.git
cd docker-kong/compose
docker-compose up -d
複製代碼

查看操做效果: docker

能夠看到kong監控了4個端口shell

端口 做用
8000 http網關
8443 https網關
8001 管理api
8444 https的管理api

註冊服務等服務相關的操做使用管理api,普通api請求到http網關或者https網關json

註冊服務

好比說把服務 https://jsonplaceholder.typicode.com/todos/註冊到kong裏面

curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=note-service' \
  --data 'url=https://jsonplaceholder.typicode.com/todos/'
複製代碼

這個時候服務已經註冊了,可是經過訪問 http://127.0.0.1:8000/note-service/3是沒法訪問的

這是由於並無把"http://127.0.0.1"這個host映射到note-service 後端

映射host

curl -i -X POST \
  --url http://localhost:8001/services/note-service/routes \
  --data 'hosts[]=127.0.0.1'
複製代碼

查看操做效果 api

能夠看到kong網關已經成功代理這個"jsonplaceholder.typicode.com/todos/"後端服務了app

服務存活檢測

字段參考:docs.konghq.com/1.1.x/healt…dom

upstream.json

{
    "name": "mynote",
    "healthchecks": {
        "active": {
            "concurrency": 10,
            "healthy": {
                "http_statuses": [ 200, 302 ],
                "interval": 0,
                "successes": 0
            },
            "http_path": "/",
            "timeout": 1,
            "unhealthy": {
                "http_failures": 0,
                "http_statuses": [ 429, 404, 500, 501,
                                   502, 503, 504, 505 ],
                "interval": 0,
                "tcp_failures": 0,
                "timeouts": 0
            }
        },
        "passive": {
            "healthy": {
                "http_statuses": [ 200, 201, 202, 203,
                                   204, 205, 206, 207,
                                   208, 226, 300, 301,
                                   302, 303, 304, 305,
                                   306, 307, 308 ],
                "successes": 0
            },
            "unhealthy": {
                "http_failures": 0,
                "http_statuses": [ 429, 500, 503 ],
                "tcp_failures": 0,
                "timeouts": 0
            }
        }
    },
    "slots": 10
}
複製代碼
curl -i -X POST \
  -H "Content-Type: application/json" \
  -d "@upstream.json" \
  --url http://localhost:8001/upstreams
複製代碼

操做效果以下

ui界面

使用上面的curl管理kong雖然不復雜,可是看起來不直觀,能夠kong的可視化界面konga

docker一鍵啓動命令以下

docker run -p 1337:1337 \
             --rm \
             --name konga \
             -e "NODE_ENV=development" \
             -e "TOKEN_SECRET={{somerandomstring}}" \
             pantsel/konga
複製代碼

效果以下

能夠看到仍是很是直觀的

參考資料

  1. github.com/Kong/docker…
  2. docs.konghq.com/1.1.x/getti…
  3. docs.konghq.com/1.1.x/getti…
  4. docs.konghq.com/1.1.x/healt…
  5. docs.konghq.com/1.1.x/admin…
相關文章
相關標籤/搜索