配置路由
Apache APISIX 基于路由提供灵活的网关管理能力,其中定义了路由路径和目标上游。
路由 是通往上游目标的路由路径。在 Apache APISIX 中,路由负责根据定义的规则匹配客户端请求,加载并执行相应的插件,以及将请求转发到指定的上游服务。一个简单的路由可以使用路径匹配 URI 和相应的上游地址来设置。
上游 是一组具有相同工作的目标节点。它定义了一个虚拟主机抽象,根据配置的规则对给定的一组服务节点执行负载均衡。
本教程指导你如何创建路由并对其进行验证。你将:
前置条件
- 完成 安装 APISIX 以在 Docker 或 Kubernetes 中安装 APISIX。
- 如果使用这些工具,请安装 ADC 或 APISIX-MCP。
创建路由
在本节中,你将创建一个路由,将客户端请求转发到 httpbin,这是一个 HTTP 请求和响应服务。
- Admin API
- ADC
- APISIX-MCP
curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
"id": "getting-started-ip",
"uri": "/ip",
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
如果路由创建成功,你将收到 HTTP/1.1 201 Created 响应。
adc.yaml
services:
- name: httpbin Service
routes:
- uris:
- /ip
name: getting-started-ip
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到 APISIX:
adc sync -f adc.yaml
如果配置同步成功,你将收到类似的响应:
[11:25:49 AM] [ADC] › ✔ success Sync configuration
[11:25:49 AM] [ADC] › ★ star All is well, see you next time!
在你的 AI 客户端中输入以下提示:
Create a route with the ID getting-started-ip that matches requests to the URI /ip, and forwards them to an upstream using round-robin load balancing with a single node at httpbin.org on port 80.
你应该看到类似的响应:
Successfully created route "getting-started-ip" with the following configuration:
URI: /ip
Upstream: http://httpbin.org:80 (roundrobin load balancing)
Route ID: getting-started-ip
Status: Active (1)
Created at: 1744183830 (2025-04-09 07:30:30 UTC)
The route is now ready to accept requests at the /ip path, which will be forwarded to httpbin.org on port 80.
验证
- Admin API
- ADC
- APISIX-MCP
向路由发送请求:
curl "http://127.0.0.1:9080/ip"
你应该看到类似的响应:
{
"origin": "183.94.122.205"
}
向路由发送请求:
curl "http://127.0.0.1:9080/ip"
你应该看到类似的响应:
{
"origin": "183.94.122.205"
}
在你的 AI 客户端中输入以下提示:
Send a request to the route for verification.
你应该看到类似的响应:
Successfully verified the route "getting-started-ip":
Received HTTP 200 response from httpbin.org
Response shows origin IP address (192.168.107.1, 103.97.2.159)
The route is properly configured and forwarding requests to httpbin.org
The upstream service is responding correctly to requests
下一步
本教程创建了一个指向单个上游节点的路由。在下一个教程中,你将学习如何配置多上游节点的负载均衡。