public-api
public-api 插件用于暴露内部 API 端点,使其可公开访问。该插件的主要用途之一是暴露由其他插件创建的内部端点。
示例
以下示例展示了如何在不同场景下配置 public-api。
在自定义端点暴露 Prometheus 指标
以下示例演示了如何禁用默认在 9091 端口暴露端点的 Prometheus 导出服务器,并在 APISIX 用于监听其他客户端请求的 9080 端口上,通过新的公共 API 端点暴露 APISIX Prometheus 指标。
你还需要配置路由,使得内部端点 /apisix/prometheus/metrics 通过自定义端点暴露。
如果收集的指标数量较大,该插件可能会占用大量 CPU 资源进行指标计算,并对常规请求的处理产生负面影响。
为解决此问题,APISIX 使用 特权进程 并将指标计算卸载到单独的进程。如果你使用配置文件中 plugin_attr.prometheus.export_addr 下配置的指标端点,此优化将自动应用。如果通过 public-api 插件暴露指标端点,则无法享受此优化。
在配置文件中禁用 Prometheus 导出服务器,并重新加载 APISIX 使更改生效:
plugin_attr:
prometheus:
enable_export_server: false
接下来,创建一个使用 public-api 插件的路由,为 APISIX 指标暴露一个公共 API 端点:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "prometheus-metrics",
"uri": "/prometheus_metrics",
"plugins": {
"public-api": {
"uri": "/apisix/prometheus/metrics"
}
}
}'
❶ 将路由 uri 设置为自定义端点路径。
❷ 将插件 uri 设置为要暴露的内部端点。
向自定义指标端点发送请求:
curl "http://127.0.0.1:9080/prometheus_metrics"
你应该看到类似如下的输出:
# HELP apisix_http_requests_total The total number of client requests since APISIX started
# TYPE apisix_http_requests_total gauge
apisix_http_requests_total 1
# HELP apisix_nginx_http_current_connections Number of HTTP connections
# TYPE apisix_nginx_http_current_connections gauge
apisix_nginx_http_current_connections{state="accepted"} 1
apisix_nginx_http_current_connections{state="active"} 1
apisix_nginx_http_current_connections{state="handled"} 1
apisix_nginx_http_current_connections{state="reading"} 0
apisix_nginx_http_current_connections{state="waiting"} 0
apisix_nginx_http_current_connections{state="writing"} 1
...
暴露批量请求端点
以下示例演示了如何使用 public-api 插件为 batch-requests 插件暴露端点,该插件用于在向网关发送请求之前将多个请求组装成一个请求。
创建一个示例路由指向 httpbin 的 /anything 端点,用于验证目的:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "httpbin-anything",
"uri": "/anything",
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
创建一个使用 public-api 插件的路由,并将路由 uri 设置为要暴露的内部端点:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "batch-requests",
"uri": "/apisix/batch-requests",
"plugins": {
"public-api": {}
}
}'
向暴露的批量请求端点发送一个由 GET 和 POST 请求组成的管道请求:
curl "http://127.0.0.1:9080/apisix/batch-requests" -X POST -d '
{
"pipeline": [
{
"method": "GET",
"path": "/anything"
},
{
"method": "POST",
"path": "/anything",
"body": "a post request"
}
]
}'
你应该会收到两个请求的响应,类似如下:
[
{
"reason": "OK",
"body": "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Host\": \"127.0.0.1\", \n \"User-Agent\": \"curl/8.6.0\", \n \"X-Amzn-Trace-Id\": \"Root=1-67b6e33b-5a30174f5534287928c54ca9\", \n \"X-Forwarded-Host\": \"127.0.0.1\"\n }, \n \"json\": null, \n \"method\": \"GET\", \n \"origin\": \"192.168.107.1, 43.252.208.84\", \n \"url\": \"http://127.0.0.1/anything\"\n}\n",
"headers": {
...
},
"status": 200
},
{
"reason": "OK",
"body": "{\n \"args\": {}, \n \"data\": \"a post request\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Content-Length\": \"14\", \n \"Host\": \"127.0.0.1\", \n \"User-Agent\": \"curl/8.6.0\", \n \"X-Amzn-Trace-Id\": \"Root=1-67b6e33b-0eddcec07f154dac0d77876f\", \n \"X-Forwarded-Host\": \"127.0.0.1\"\n }, \n \"json\": null, \n \"method\": \"POST\", \n \"origin\": \"192.168.107.1, 43.252.208.84\", \n \"url\": \"http://127.0.0.1/anything\"\n}\n",
"headers": {
...
},
"status": 200
}
]
如果你想在自定义端点暴露批量请求端点,可以如下创建带有 public-api 插件的路由:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "batch-requests",
// 注释 1
"uri": "/batch-requests",
"plugins": {
"public-api": {
// 注释 2
"uri": "/apisix/batch-requests"
}
}
}'
❶ 将路由 uri 设置为自定义端点路径。
❷ 将插件 uri 设置为要暴露的内部端点。
现在批量请求端点应该暴露为 /batch-requests,而不是 /apisix/batch-requests。
向暴露的批量请求端点发送一个由 GET 和 POST 请求组成的管道请求:
curl "http://127.0.0.1:9080/batch-requests" -X POST -d '
{
"pipeline": [
{
"method": "GET",
"path": "/anything"
},
{
"method": "POST",
"path": "/anything",
"body": "a post request"
}
]
}'
你应该会收到两个请求的响应,类似如下:
[
{
"reason": "OK",
"body": "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Host\": \"127.0.0.1\", \n \"User-Agent\": \"curl/8.6.0\", \n \"X-Amzn-Trace-Id\": \"Root=1-67b6e33b-5a30174f5534287928c54ca9\", \n \"X-Forwarded-Host\": \"127.0.0.1\"\n }, \n \"json\": null, \n \"method\": \"GET\", \n \"origin\": \"192.168.107.1, 43.252.208.84\", \n \"url\": \"http://127.0.0.1/anything\"\n}\n",
"headers": {
...
},
"status": 200
},
{
"reason": "OK",
"body": "{\n \"args\": {}, \n \"data\": \"a post request\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Content-Length\": \"14\", \n \"Host\": \"127.0.0.1\", \n \"User-Agent\": \"curl/8.6.0\", \n \"X-Amzn-Trace-Id\": \"Root=1-67b6e33b-0eddcec07f154dac0d77876f\", \n \"X-Forwarded-Host\": \"127.0.0.1\"\n }, \n \"json\": null, \n \"method\": \"POST\", \n \"origin\": \"192.168.107.1, 43.252.208.84\", \n \"url\": \"http://127.0.0.1/anything\"\n}\n",
"headers": {
...
},
"status": 200
}
]