三分钟上手基于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…
相关文章
相关标签/搜索