微服务领域对 API 网关有新的需求:更高的灵活性、更高的性能要求,以及云原生的贴合。
APISIX 基于 etcd 来完成配置的保存和同步,而不是 postgres 或者 MySQL 这类关系型数据库。 这样不仅去掉了轮询,让代码更加的简洁,配置同步也更加实时。同时系统也不会存在单点,可用性更高。
另外,APISIX 具备动态路由和插件热加载,特别适合微服务体系下的 API 管理。
APISIX 设计和开发的目标之一,就是业界最高的性能。具体测试数据见这里:benchmark
APISIX 是当前性能最好的 API 网关,单核 QPS 达到 2.3 万,平均延时仅有 0.6 毫秒。
是的,在 0.6 版本中我们内置了 dashboard,你可以通过 web 界面来操作 APISIX 了。
当然可以,APISIX 提供了灵活的自定义插件,方便开发者和企业编写自己的逻辑。
对于配置中心,配置存储只是最基本功能,APISIX 还需要下面几个特性:
APISIX 需要一个配置中心,上面提到的很多功能是传统关系型数据库和KV数据库是无法提供的。与 etcd 同类软件还有 Consul、ZooKeeper等,更详细比较可以参考这里:etcd why,在将来也许会支持其他配置存储方案。
遇到 luarocks 慢的问题,有以下两种可能:
git
协议进行封锁针对第一个问题,你可以使用 https_proxy 或者使用 --server
选项来指定一个你可以访问或者访问更快的 luarocks 服务。 运行 luarocks config rocks_servers
命令(这个命令在 luarocks 3.0 版本后开始支持) 可以查看有哪些可用服务。
如果使用代理仍然解决不了这个问题,那可以在安装的过程中添加 --verbose
选项来查看具体是慢在什么地方。排除前面的 第一种情况,只可能是第二种,git
协议被封。这个时候可以执行 git config --global url."https://".insteadOf git://
命令使用 https
协议替代。
比如,foo.com/product/index.html?id=204&page=2
, 根据 URL 中 query string 中的 id
作为条件来灰度发布:
可以这么做:
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/index.html", "vars": [ ["arg_id", "<=", "1000"] ], "plugins": { "redirect": { "uri": "/test?group_id=1" } } }' curl -i http://127.0.0.1:9080/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/index.html", "vars": [ ["arg_id", ">", "1000"] ], "plugins": { "redirect": { "uri": "/test?group_id=2" } } }'
更多的 lua-resty-radixtree 匹配操作,可查看操作列表: https://github.com/iresty/lua-resty-radixtree#operator-list
比如,将 http://foo.com
重定向到 https://foo.com
有几种不同的方法来实现:
redirect
插件的 http_to_https
功能:curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/hello", "host": "foo.com", "plugins": { "redirect": { "http_to_https": true } } }'
vars
和 redirect
插件一起使用:curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/hello", "host": "foo.com", "vars": [ [ "scheme", "==", "http" ] ], "plugins": { "redirect": { "uri": "https://$host$request_uri", "ret_code": 301 } } }'
serverless
插件:curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/hello", "plugins": { "serverless-pre-function": { "phase": "rewrite", "functions": ["return function() if ngx.var.scheme == \"http\" and ngx.var.host == \"foo.com\" then ngx.header[\"Location\"] = \"https://foo.com\" .. ngx.var.request_uri; ngx.exit(ngx.HTTP_MOVED_PERMANENTLY); end; end"] } } }'
然后测试下是否生效:
curl -i -H 'Host: foo.com' http://127.0.0.1:9080/hello
响应体应该是:
HTTP/1.1 301 Moved Permanently Date: Mon, 18 May 2020 02:56:04 GMT Content-Type: text/html Content-Length: 166 Connection: keep-alive Location: https://foo.com/hello Server: APISIX web server <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> <hr><center>openresty</center> </body> </html>
默认的APISIX日志等级为warn
,如果需要查看core.log.info
的打印结果需要将日志等级调整为info
。
具体步骤:
1、修改conf/config.yaml中的nginx log配置参数error_log_level: "warn"
为error_log_level: "info"
。
2、重启APISIX
之后便可以在logs/error.log中查看到info的日志了。
Apache APISIX 的插件支持热加载,如果你的 APISIX 节点打开了 Admin API,那么对于新增/删除/修改插件等场景,均可以通过调用 HTTP 接口的方式热加载插件,不需要重启服务。
curl http://127.0.0.1:9080/apisix/admin/plugins/reload -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT
如果你的 APISIX 节点并没有打开 Admin API,那么你可以通过手动 reload APISIX 的方式加载插件。
apisix reload