APISIX是一款基于Openresty的动态、实时、高性能API网关,它提供了负载均衡、服务熔断等丰富的功能。
在https://github.com/apache/apisix-docker中提供了很多很便利的docker配置,可以很方便的启动APISIX的相关服务。
~ git clone git@github.com:apache/apisix-docker.git~ cd apisix-docker/example/~ docker-compose upStarting example_etcd_1 ... doneStarting example_grafana_1 ... doneStarting example_apisix-dashboard_1 ... doneStarting example_web2_1 ... doneStarting example_web1_1 ... doneStarting example_prometheus_1 ... doneStarting example_apisix_1 ... doneAttaching to example_grafana_1, example_etcd_1, example_apisix-dashboard_1, example_web1_1, example_web2_1, example_prometheus_1, example_apisix_1
如上启动了7个容器,分别是:
接下来我们可以测试一下apisix
~ curl http://127.0.0.1:9180/apisix/admin/routes -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' {"total":0,"list":[]}
如上命令用于查看apisix的所有路由信息,使用默认的API-KEY
。
路由用于通过规则匹配客户端的请求,并根据请求加载相应的插件,最后将请求转发到指定的上游服务。由此可知路由由三部分组成
method
、host
、uri
等APISIX的Admin接口都采用了Restful风格,路由资源的地址为/apisix/admin/routes/{id}
,它的操作接口如下
方法 | 路径 | 功能 |
---|---|---|
GET | /apisix/admin/routes | 获取资源列表 |
GET | /apisix/admin/routes/{id} | 获取指定资源 |
PUT | /apisix/admin/routes/{id} | 根据指定id创建资源 |
POST | /apisix/admin/routes | 创建资源,id自动生成 |
DELETE | /apisix/admin/routes/{id} | 删除指定id的资源 |
接下来我们创建一个上游为web1的路由
~ curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -i -d '{ "methods": ["GET"], "uri": "/web1", "upstream": { "type": "roundrobin", "nodes": { "web1:80": 1 } }}'HTTP/1.1 201 Created...{"key":"/apisix/routes/1","value":{"status":1,"methods":["GET"],"id":"1","priority":0,"uri":"/web1","update_time":1706606490,"upstream":{"scheme":"http","hash_on":"vars","type":"roundrobin","nodes":{"web1:80":1},"pass_host":"pass"},"create_time":1706606490}}
返回201表示创建成功,之后我们可以访问这个新建的路由接口
~ curl http://127.0.0.1:9080/web1hello web1
除了直接使用服务之外,为了方便,我们还可以把服务的资源直接定义为一个上游(Upstream)
~ curl -X PUT http://127.0.0.1:9180/apisix/admin/upstreams/1 \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -i -d '{ "type": "roundrobin", "nodes": { "web1:80": 1 }}'HTTP/1.1 201 Created...{"key":"/apisix/upstreams/1","value":{"scheme":"http","create_time":1706606769,"hash_on":"vars","id":"1","type":"roundrobin","update_time":1706606769,"pass_host":"pass","nodes":{"web1:80":1}}}
如上我们把web1这个服务定义为上游(Upstream)1
,之后我们可以直接使用这个上游构建路由
~ curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -i -d '{ "methods": ["GET"], "uri": "/web1", "upstream_id": "1"}'HTTP/1.1 200 OK...{"key":"/apisix/routes/1","value":{"upstream_id":"1","methods":["GET"],"status":1,"priority":0,"uri":"/web1","update_time":1706606904,"id":"1","create_time":1706606490}}
之后访问地址还是一样的效果
~ curl http://127.0.0.1:9080/web1 hello web1
查看我们刚刚创建的路由和上游
~ curl http://127.0.0.1:9180/apisix/admin/upstreams -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1'{"total":1,"list":[{"key":"/apisix/upstreams/1","value":{"scheme":"http","create_time":1706607099,"hash_on":"vars","nodes":{"web1:80":1},"type":"roundrobin","id":"1","pass_host":"pass","update_time":1706607099},"createdIndex":35,"modifiedIndex":35}]}~ curl http://127.0.0.1:9180/apisix/admin/routes -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' {"total":1,"list":[{"key":"/apisix/routes/1","value":{"create_time":1706607103,"methods":["GET"],"id":"1","priority":0,"uri":"/web1","upstream_id":"1","update_time":1706607103,"status":1},"createdIndex":36,"modifiedIndex":36}]}
我们还可以删除我们刚刚创建的路由和上游
~ curl -X DELETE http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' {"key":"/apisix/routes/1","deleted":"1"}~ curl -X DELETE http://127.0.0.1:9180/apisix/admin/upstreams/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' {"key":"/apisix/upstreams/1","deleted":"1"}
APISIX支持很多插件,以常见的流控插件为例
~ curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -i -d '{ "methods": ["GET"], "uri": "/web1", "plugins": { "limit-count": { "count": 2, "time_window": 10, "rejected_code": 503, "key": "remote_addr" }, "prometheus": {} }, "upstream": { "type": "roundrobin", "nodes": { "web1:80": 1 } }}'HTTP/1.1 200 OK...
如上创建了一个路由,它使用了插件limit-count
,参数表示每个远程地址每10秒钟最多请求两次,否则就会得到503错误。我们测试可以看到请求两次之后就开始报503错误,等待一会儿之后服务又恢复了
~ curl http://127.0.0.1:9080/web1 hello web1%~ curl http://127.0.0.1:9080/web1hello web1%~ curl http://127.0.0.1:9080/web1<html><head><title>503 Service Temporarily Unavailable</title></head><body><center><h1>503 Service Temporarily Unavailable</h1></center><hr><center>openresty</center><p><em>Powered by <a href="https://apisix.apache.org/">APISIX</a>.</em></p></body></html>~ curl http://127.0.0.1:9080/web1<html><head><title>503 Service Temporarily Unavailable</title></head><body><center><h1>503 Service Temporarily Unavailable</h1></center><hr><center>openresty</center><p><em>Powered by <a href="https://apisix.apache.org/">APISIX</a>.</em></p></body></html>~ curl http://127.0.0.1:9080/web1hello web1%
与上游类似,我们也可以定义一个插件配置
,之后在路由中直接使用
~ curl http://127.0.0.1:9180/apisix/admin/plugin_configs/1 \-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '{ "desc": "enable limit-count plugin", "plugins": { "limit-count": { "count": 2, "time_window": 10, "rejected_code": 503 } }}'HTTP/1.1 201 Created...{"key":"/apisix/plugin_configs/1","value":{"create_time":1706607965,"desc":"enable limit-count plugin","plugins":{"limit-count":{"count":2,"time_window":10,"key_type":"var","rejected_code":503,"policy":"local","allow_degradation":false,"show_limit_quota_header":true,"key":"remote_addr"}},"id":"1","update_time":1706607965}}
如上定义了一个id为1
的插件,之后我们使用它构建一个路由
~ curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \ -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -i -d '{ "methods": ["GET"], "uri": "/web1", "upstream_id": "1", "plugin_config_id": "1"}'HTTP/1.1 200 OK...{"key":"/apisix/routes/1","value":{"create_time":1706607103,"plugin_config_id":"1","priority":0,"update_time":1706608060,"methods":["GET"],"uri":"/web1","upstream_id":"1","status":1,"id":"1"}}
之后访问地址http://127.0.0.1:9080/web1
的效果和前面是一样的。
apisix还可以使用Dashboard来管理路由,浏览器访问http://127.0.0.1:9000即可
默认的用户名和密码都是admin
。登录之后我们就可以进行管理了,例如要新建一个路由
我们依次设置路由的名称、path、method以及上游信息
创建完成之后就可以在路由列表看到我们刚刚创建的路由了
之后访问我们刚刚创建的路由,和使用AdminAPI创建的效果是一样的
~ curl http://127.0.0.1:9080/web2hello web2