在Kong中为后端服务集群指定多个地址标准方法是使用Upstream上游和Target目标实体。这类似于Nginx的upstream模块可以实现负载均衡、健康检查等功能。配置步骤1. 创建Upstream上游服务首先创建一个Upstream它代表你的后端服务集群curl-XPOST http://localhost:8001/upstreams\-HContent-Type: application/json\-d{ name: hzero-backend-cluster, algorithm: round-robin, slots: 10000 }参数说明name: Upstream的名称后面创建Service时会用到algorithm: 负载均衡算法可选round-robin、consistent-hashing、least-connections、latency等slots: 哈希环的大小用于一致性哈希算法2. 添加Targets后端节点为Upstream添加多个后端节点地址支持IP或域名# 添加第一个节点curl-XPOST http://localhost:8001/upstreams/hzero-backend-cluster/targets\-HContent-Type: application/json\-d{ target: 192.168.1.10:8080, weight: 100 }# 添加第二个节点curl-XPOST http://localhost:8001/upstreams/hzero-backend-cluster/targets\-HContent-Type: application/json\-d{ target: 192.168.1.11:8080, weight: 100 }# 添加第三个节点curl-XPOST http://localhost:8001/upstreams/hzero-backend-cluster/targets\-HContent-Type: application/json\-d{ target: 192.168.1.12:8080, weight: 50 }参数说明target: 后端节点地址格式为IP:端口或域名:端口weight: 权重值越高处理的请求越多3. 创建Service指向Upstream创建Service时将host设置为Upstream的名称curl-XPOST http://localhost:8001/services\-HContent-Type: application/json\-d{ name: hzero-service, host: hzero-backend-cluster, port: 8080, protocol: http, path: /, retries: 5, connect_timeout: 60000, write_timeout: 60000, read_timeout: 60000 }4. 创建Route绑定Servicecurl-XPOST http://localhost:8001/services/hzero-service/routes\-HContent-Type: application/json\-d{ name: hzero-route, paths: [/], strip_path: false, preserve_host: true, protocols: [http, https] }验证配置查看Upstream详情curlhttp://localhost:8001/upstreams/hzero-backend-cluster查看Targets列表curlhttp://localhost:8001/upstreams/hzero-backend-cluster/targets查看健康节点自动过滤不健康的curlhttp://localhost:8001/upstreams/hzero-backend-cluster/health高级配置1. 配置健康检查在创建Upstream时可以添加健康检查配置curl-XPOST http://localhost:8001/upstreams\-HContent-Type: application/json\-d{ name: hzero-backend-cluster, healthchecks: { active: { type: http, timeout: 1, concurrency: 10, http_path: /health, healthy: { interval: 5, successes: 1 }, unhealthy: { interval: 5, http_failures: 3 } }, passive: { type: http, healthy: { successes: 1 }, unhealthy: { http_failures: 3 } } } }2. 其他负载均衡算法一致性哈希适合需要会话保持的场景curl-XPOST http://localhost:8001/upstreams\-HContent-Type: application/json\-d{ name: hzero-backend-cluster, algorithm: consistent-hashing, hash_on: ip }最少连接curl-XPOST http://localhost:8001/upstreams\-HContent-Type: application/json\-d{ name: hzero-backend-cluster, algorithm: least-connections }管理命令动态添加新节点curl-XPOST http://localhost:8001/upstreams/hzero-backend-cluster/targets\-HContent-Type: application/json\-d{ target: 192.168.1.13:8080, weight: 100 }删除节点标记为不可用curl-XDELETE http://localhost:8001/upstreams/hzero-backend-cluster/targets/192.168.1.10:8080更新节点权重# 先删除旧节点curl-XDELETE http://localhost:8001/upstreams/hzero-backend-cluster/targets/192.168.1.10:8080# 再添加新节点同地址但新权重curl-XPOST http://localhost:8001/upstreams/hzero-backend-cluster/targets\-HContent-Type: application/json\-d{ target: 192.168.1.10:8080, weight: 200 }两种负载均衡方式的对比方式优点缺点适用场景DNS-based配置简单自动发现功能有限仅支持round-robin简单场景节点变化不频繁Upstream Targets支持多种算法、健康检查、动态调整需要手动管理节点生产环境需要精细控制推荐使用Upstream方式它提供了更强大的功能包括健康检查、多种负载均衡算法、动态调整权重等。