三分钟上手基于openresty开发的kong网关系统

栏目: 编程工具 · 发布时间: 4年前

内容简介:参考项目:启动过程如下:查看操作效果:

参考项目: github.com/Kong/docker…

启动过程如下:

git clone https://github.com/Kong/docker-kong.git
cd docker-kong/compose
docker-compose up -d
复制代码

查看操作效果:

三分钟上手基于openresty开发的kong网关系统
三分钟上手基于openresty开发的kong网关系统

可以看到kong监控了4个端口

端口 作用
8000 http网关
8443 https网关
8001 管理api
8444 https的管理api

注册服务等服务相关的操作使用管理api,普通api请求到http网关或者https网关

注册服务

三分钟上手基于openresty开发的kong网关系统
比如说把服务 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/'
复制代码
三分钟上手基于openresty开发的kong网关系统
这个时候服务已经注册了,但是通过访问 http://127.0.0.1:8000/note-service/3

是无法访问的

这是因为并没有把"http://127.0.0.1"这个host映射到note-service

三分钟上手基于openresty开发的kong网关系统

映射host

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

查看操作效果

三分钟上手基于openresty开发的kong网关系统
三分钟上手基于openresty开发的kong网关系统

可以看到kong网关已经成功代理这个" jsonplaceholder.typicode.com/todos/ "后端服务了

服务存活检测

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

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
复制代码

操作效果如下

三分钟上手基于openresty开发的kong网关系统

ui界面

使用上面的curl管理kong虽然不复杂,但是看起来不直观,可以kong的可视化界面konga

docker一键启动命令如下

docker run -p 1337:1337 \
             --rm \
             --name konga \
             -e "NODE_ENV=development" \
             -e "TOKEN_SECRET={{somerandomstring}}" \
             pantsel/konga
复制代码

效果如下

三分钟上手基于openresty开发的kong网关系统

可以看到还是非常直观的

参考资料

  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…

以上所述就是小编给大家介绍的《三分钟上手基于openresty开发的kong网关系统》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

How to Design Programs

How to Design Programs

Matthias Felleisen、Robert Bruce Findler、Matthew Flatt、Shriram Krishnamurthi / The MIT Press / 2001-2-12 / 71.00美元

This introduction to programming places computer science in the core of a liberal arts education. Unlike other introductory books, it focuses on the program design process. This approach fosters a var......一起来看看 《How to Design Programs》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试