所谓网关,主要做用就是链接两个不一样网络的设备,而今天所讲的 API 网关是指承接和分发客户端全部请求的网关层。html
为何须要网关层?最初是单体服务时,客户端发起的全部请求均可以直接请求到该服务,但随着产品用户愈来愈多,单体应用存在显而易见的单点问题,除此以外,当单体应用大小升至几个 G 时,持续发布将会很是缓慢,因此服务的拆分红为了必然趋势。nginx
当服务拆分为多个以后,咱们不得不面临一个问题,就是如何控制用户请求到对应服务节点,因而网关层应运而生,它不只能够负责负载均衡,还可让它处理认证校验、请求限流、日志记录以及监控服务节点等等。git
固然,网关层并不须要咱们手动实现,市面上有不少 API 网关开源项目,好比 Zuul、Kong、Tyk 等,今天主要介绍 Kong。github
Kong 是一个在 Nginx 中运行的 Lua 程序,由 lua-nginx-module 实现,和 Openresty 一块儿打包发行,支持多种操做环境下的安装,能够用来作 HTTP 基本认证、密钥认证、TCP、UDP、文件日志、API 请求限流、请求转发等等。docker
第一步,建立一个 docker 网络。数据库
$ docker network create kong-net
建立用于存储 Kong 数据的数据库,可使用 Cassandra 或 PostgreSQL,本示例采用 Cassandra。json
Cassandra 是由 Facebook 开发的分布式 NoSQL 数据库。bootstrap
$ docker run -d --name kong-database \ --network=kong-net \ -p 9042:9042 \ cassandra:3
初始化数据到 Cassandra 数据库。api
$ 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 中。
$ curl -i http://localhost:8001/
注册名为 baidu-service 的服务到 kong,若是请求匹配到该服务会跳转至 url。
$ curl -i -X POST \ --url http://localhost:8001/services/ \ --data 'name=baidu-service' \ --data 'url=http://baidu.com'
注册成功以后会获得以下提示。
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。
$ curl -i -X POST \ --url http://localhost:8001/services/baidu-service/routes \ --data 'hosts[]=baidu.com'
以上代码的做用是当请求域名是 baidu.com 时,则将请求转发到 baidu-service 指定的 url,咱们能够经过 curl 模拟测试。
$ curl -i -X GET \ --url http://localhost:8000/ \ --header 'Host: baidu.com'
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 <html> <meta http-equiv="refresh" content="0;url=http://www.baidu.com/"> </html>
与 nginx 同理,先建立一个 upstream,名为 hello。
$ curl -X POST http://localhost:8001/upstreams --data "name=hello"
{"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 添加两个负载均衡的节点,我就拿京东和淘宝来作测试了。
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 中的
upstream hello { server jd.com weight=100; server taobao.com weight=50; }
接下来建立一个 service 指向 upstream,host 即对应 upstream 名。
curl -X POST http://localhost:8001/services --data "name=hello" --data "host=hello"
为 service 建立路由,凡是包含 /hello 的地址所有走 hello upstream。
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 不一样粒度上控制请求,配置很是简单。
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 的示例
curl -X POST http://kong:8001/routes/{route}/plugins \ --data "name=rate-limiting" --data "config.second=5" \ --data "config.hour=10000"
Consumer 的示例
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」
感谢你们的观看,若是以为文章对你有所帮助,欢迎关注公众号「平也」,聚焦Go语言与技术原理。