
基于Openresty和NGINX的高性能API网关。
当前项目已达到生产就绪状态。
bash$ docker run --name="apigateway" \ -p 80:80 \ -e "LOG_LEVEL=info" \ adobeapiplatform/apigateway:latest
bash$ docker run --name="apigateway" \ -p 80:80 \ -e "MARATHON_HOST=http://<marathon_host>:<port>" \ -e "LOG_LEVEL=info" \ adobeapiplatform/apigateway:latest
此命令启动的API网关会自动发现Marathon中运行的服务,并通过独立虚拟主机(VHost)暴露这些服务,具体配置可参见https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/conf.d/marathon_apis.conf#L36%E3%80%82
本地访问Marathon应用
例如,若有一个名为hello-world的应用,可通过以下两种方式访问其虚拟主机:
/etc/hosts,添加<docker_host_ip> hello-world.api.localhost,然后在浏览器中访问http://hello-world.api.localhostcurl -H "Host:hello-world.api.localhost" http://<docker_host_ip>https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/marathon-service-discovery.sh%E4%BB%85%E4%BD%9C%E4%B8%BA%E5%BF%AB%E9%80%9F%E5%90%AF%E5%8A%A8%E7%A4%BA%E4%BE%8B%EF%BC%8C%E5%8F%AF%E6%9B%BF%E6%8D%A2%E4%B8%BA%E5%85%B6%E4%BB%96%E6%9C%8D%E5%8A%A1%E5%8F%91%E7%8E%B0%E6%9C%BA%E5%88%B6%E3%80%82%E8%AF%A5%E8%84%9A%E6%9C%AC%E4%BC%9A%E6%9B%B4%E6%96%B0%E5%8C%85%E5%90%AB%E6%89%80%E6%9C%89NGINX%E4%B8%8A%E6%B8%B8%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/environment.conf.d/api-gateway-upstreams.http.conf%EF%BC%8C%E4%BE%9Bhttps://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/conf.d/marathon_apis.conf#L36%E4%BD%BF%E7%94%A8%E3%80%82
远程访问Marathon应用
只需创建类似*.api.example.com或*.gw.example.com的DNS记录,使其解析到运行网关的节点即可。
假设Marathon中部署了名为hello-world的应用,可通过URL http://hello-world.api.example.com访问,网关会自动将请求代理到Marathon中的hello-world应用。
若访问http://my-custom-app.api.example.com,网关会代理到名为my-custom-app的Marathon应用。若该应用未部署,将返回5xx错误(此行为可配置)。
使用不同的配置文件夹
简化网关配置的一种方式是将api-gateway-config文件夹复制到S3,让所有节点从该位置同步配置。目前仅支持AWS S3,计划添加对其他云平台的支持。
要使用S3,需通过环境变量REMOTE_CONFIG指定存储桶:
bashdocker run --name="apigateway" \ -p 80:80 \ -e "MARATHON_HOST=http://<marathon_host>:<port>" \ -e "REMOTE_CONFIG=s3://api-gateway-config-bucket" \ -e "LOG_LEVEL=info" \ adobeapiplatform/apigateway:latest
s3://api-gateway-config-bucket应包含与https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/%E6%96%87%E4%BB%B6%E5%A4%B9%E7%B1%BB%E4%BC%BC%E7%9A%84%E5%86%85%E5%AE%B9%EF%BC%8C%E5%BB%BA%E8%AE%AE%E5%9F%BA%E4%BA%8E%E8%AF%A5%E6%96%87%E4%BB%B6%E5%A4%B9%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E3%80%82%E4%BB%A5%E4%B8%8B%E4%B8%A4%E4%B8%AA%E6%96%87%E4%BB%B6%E4%B8%8D%E4%BC%9A%E8%A2%AB%E5%90%8C%E6%AD%A5%EF%BC%9A
/etc/api-gateway/conf.d/includes/resolvers.conf(详见下文说明)默认通过IAM角色访问S3存储桶,也可通过环境变量指定AWS凭证:
bashdocker run --name="apigateway" \ -p 80:80 \ -e "MARATHON_HOST=http://<marathon_host>:<port>" \ -e "REMOTE_CONFIG=s3://api-gateway-config-bucket" \ -e "AWS_ACCESS_KEY_ID=--change-me--" \ -e "AWS_SECRET_ACCESS_KEY=--change-me--" \ -e "LOG_LEVEL=info" \ adobeapiplatform/apigateway:latest
默认每10s检查一次远程配置变更,可通过REMOTE_CONFIG_SYNC_INTERVAL环境变量调整间隔。修改时需考虑:
自定义同步命令
可通过REMOTE_CONFIG_SYNC_CMD环境变量控制用于下载配置文件的同步命令,此变量会覆盖REMOTE_CONFIG。
解析器
容器启动时会自动根据/etc/resolv.conf创建/etc/api-gateway/conf.d/includes/resolvers.conf配置文件。关于NGINX的resolver指令,详见文档。
在Marathon和Mesos之外运行API网关
除依赖Marathon的服务发现部分外,API网关也可独立运行。Marathon服务发现通过-e "MARATHON_HOST=http://<marathon_host>:<port>/"启用。
| 模块 | 版本 | 详情 |
|---|---|---|
| https://github.com/openresty/ | 1.13.6.1 | 安装路径:/usr/local/sbin/api-gateway |
带--with-debug编译的https://github.com/openresty/ | 1.13.6.1 | 安装路径:/usr/local/sbin/api-gateway-debug,支持调试日志 |
| https://github.com/openresty/test-nginx | https://github.com/openresty/test-nginx/releases/tag/v0.24 | 用于在容器内执行集成测试,安装路径:/usr/local/test-nginx-0.24/,Docker构建时用于对Lua模块执行make test |
| PCRE | 8.37 | 支持PCRE JIT |
| ZeroMQ | 4.0.5 | ZeroMQ |
| CZMQ | 2.2.0 | ZeroMQ的高级C绑定 |
| 模块 | 版本 | 描述 |
|---|---|---|
| https://github.com/adobe-apiplatform/api-gateway-config-supervisor | https://github.com/adobe-apiplatform/api-gateway-config-supervisor/releases/tag/1.0.3 | 从Amazon S3同步配置文件并重载网关 |
| https://github.com/adobe-apiplatform/api-gateway-cachemanager | https://github.com/adobe-apiplatform/api-gateway-cachemanager/releases/tag/1.0.1 | 用于管理多个缓存存储的Lua库 |
| https://github.com/adobe-apiplatform/api-gateway-hmac | https://github.com/adobe-apiplatform/api-gateway-hmac/releases/tag/1.0.0 | 通过OpenSSL和FFI支持多种算法的Lua HMAC实现 |
| https://github.com/adobe-apiplatform/api-gateway-aws | https://github.com/adobe-apiplatform/api-gateway-aws/releases/tag/1.7.1 | Nginx的Lua AWS SDK |
| https://github.com/adobe-apiplatform/api-gateway-request-validation | https://github.com/adobe-apiplatform/api-gateway-request-validation/releases/tag/1.2.4 | API请求验证框架 |
| https://github.com/adobe-apiplatform/api-gateway-async-logger | https://github.com/adobe-apiplatform/api-gateway-async-logger/releases/tag/1.0.1 | 高性能异步日志器 |
| https://github.com/adobe-apiplatform/api-gateway-zmq-logger | https://github.com/adobe-apiplatform/api-gateway-zmq-logger/releases/tag/1.0.0 | 基于FFI和CZMQ的Lua ZMQ日志器 |
| https://github.com/adobe-apiplatform/api-gateway-request-tracking | https://github.com/adobe-apiplatform/api-gateway-request-tracking/releases/tag/1.0.1 | API网关的使用和跟踪处理器 |
| 模块 | 版本 | 描述 |
|---|---|---|
| https://github.com/pintsized/lua-resty-http | https://github.com/pintsized/lua-resty-http/releases/tag/v0.07 | OpenResty/ngx_lua的Lua HTTP客户端cosocket驱动 |
| https://github.com/hamishforbes/lua-resty-iputils | https://github.com/hamishforbes/lua-resty-iputils/releases/tag/v0.2.0 | Openresty中处理IP地址的工具函数 |
以下性能测试结果在8核CPU、4GB内存的虚拟机上获得。
API网关容器使用4核CPU和net=host模式启动:
bashdocker run --cpuset-cpus=0-3 --net=host --name="apigateway" -e "LOG_LEVEL=notice" adobeapiplatform/apigateway:latest
WRK测试使用4核CPU和net=host模式:
bashdocker run --cpuset-cpus=4-7 --net=host williamyeh/wrk:4.0.1 -t4 -c1000 -d30s http://<docker_host_ip>/health-check Running 30s test @ http://192.168.75.158/health-check 4 threads and 1000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 30.38ms 73.80ms 1.16s 90.90% Req/Sec 35.26k 11.98k 83.70k 68.72% 4214013 requests in 30.06s, 1.28GB read Requests/sec: 140165.09 Transfer/sec: 43.57MB
bashmake docker
bashmake docker-ssh
bashmake docker-run
API网关主进程暴露在80端口。可通过health-check测试网关是否正常工作:
bash$ curl http://<docker_host_ip>/health-check API-Platform is running!
如需快速性能测试,可使用Docker版Apache Benchmark:
bashdocker run jordi/ab ab -k -n 200000 -c 500 http://<docker_host_ip>/health-check
要将本地api-gateway-config目录挂载到容器的/etc/api-gateway/:
bash$ make docker-debug
调试模式下,容器启动带--with-debug编译的api-gateway,提供详细调试信息。使用-e "LOG_LEVEL=info"时输出会很详细,更多信息参见NGINX文档。
停止镜像:
bashmake docker-stop
此命令启动两个Docker容器:redis和gateway
bashmake docker-compose
bashmake docker-attach
向http://<marathon-host>/v2/apps发送HTTP POST请求, payload如下。为获得最佳性能,建议将network设为HOST模式,详见Docker 文档。
javascript{ "id": "api-gateway", "container": { "type": "DOCKER", "docker": { "image": "adobeapiplatform/apigateway:latest", "forcePullImage": true, "network": "HOST" } }, "cpus": 4, "mem": 4096.0, "env": { "MARATHON_HOST": "http://<marathon_host>:<marathon_port>" }, "constraints": [ [ "hostname","UNIQUE" ] ], "ports": [ 80 ], "healthChecks": [ { "protocol": "HTTP", "portIndex": 0, "path": "/health-check", "gracePeriodSeconds": 3, "intervalSeconds": 10, "timeoutSeconds": 10 } ], "instances": 1 }
如需仅在标记为slave_public的节点上运行网关,可在主JSON对象中添加:
json"acceptedResourceRoles": [ "slave_public" ]
在网关中自动发现和注册Marathon任务
要在Mesos+Marathon框架中启用自动发现,需定义以下环境变量:
MARATHON_URL=http://<marathon-url-1> MARATHON_TASKS=ws-.* (当前未使用,如需过滤任务可后续扩展)
对应的Docker命令:
bashdocker run --name="apigateway" \ -p 8080:80 \ -e "MARATHON_HOST=http://<marathon_host>:<port>/" \ -e "LOG_LEVEL=info" \ adobeapiplatform/apigateway:latest
您可以使用以下命令拉取该镜像。请将 <标签> 替换为具体的标签版本。如需查看所有可用标签版本,请访问 标签列表页面。





探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
无需登录使用专属域名
Kubernetes 集群配置 Containerd
K3s 轻量级 Kubernetes 镜像加速
VS Code Dev Containers 配置
Podman 容器引擎配置
HPC 科学计算容器配置
ghcr、Quay、nvcr 等镜像仓库
Harbor Proxy Repository 对接专属域名
Portainer Registries 加速拉取
Nexus3 Docker Proxy 内网缓存
需要其他帮助?请查看我们的 常见问题Docker 镜像访问常见问题解答 或 提交工单
docker search 限制
站内搜不到镜像
离线 save/load
插件要用 plugin install
WSL 拉取慢
安全与 digest
新手拉取配置
镜像合规机制
不支持 push
manifest unknown
no matching manifest(架构)
invalid tar header(解压)
TLS 证书失败
DNS 超时
域名连通性排查
410 Gone 排查
402 与流量用尽
401 认证失败
429 限流
D-Bus 凭证提示
413 与超大单层
来自真实用户的反馈,见证轩辕镜像的优质服务