# spring-cloud-gateway-demo **Repository Path**: c-java-father/spring-cloud-gateway-demo ## Basic Information - **Project Name**: spring-cloud-gateway-demo - **Description**: 高可用分布式项目 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-08-05 - **Last Updated**: 2025-09-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 全world最先进的分布式高可用系统 ``` eureka-server: -Dspring.profiles.active=node1 -Dserver.port=6066 user-service: -Dserver.port=6004 --add-opens java.base/java.time=ALL-UNNAMED api-gateway: -Dserver.port=6001 -Dspring.profiles.active=6001 mqtt-service: -Dserver.port=6007 --add-opens java.base/java.time=ALL-UNNAMED mqtt-server: -Dserver.port=6008 --add-opens java.base/java.time=ALL-UNNAMED search-service: -Dserver.port=6014 --add-opens java.base/java.time=ALL-UNNAMED ``` 生成新 Token: bash POST http://localhost:6001/api/users/auth/login Content-Type: application/json { "username": "admin", "password": "admin123" } 使用新 Token 测试: bash GET http://localhost:6001/api/users/hello Authorization: Bearer <新生成的token> http://localhost:6002/users/hello http://localhost:6066 三、负载均衡实现方案 方案1:Nginx 负载均衡(推荐) nginx upstream gateway_cluster { # 加权轮询配置 server 192.168.1.11:6001 weight=100; server 192.168.1.12:6005 weight=100; server 192.168.1.13:6006 weight=50; keepalive 32; least_conn; # 可选:最少连接策略 } server { listen 80; server_name api.yourdomain.com; location / { proxy_pass http://gateway_cluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_http_version 1.1; proxy_set_header Connection ""; # 健康检查 proxy_next_upstream error timeout http_500; } } 方案2:云厂商LB(以AWS ALB为例) terraform resource "aws_lb_target_group" "gateway" { port = 6001 protocol = "HTTP" vpc_id = aws_vpc.main.id health_check { path = "/actuator/health" } } resource "aws_lb_listener_rule" "gateway" { listener_arn = aws_lb_listener.front_end.arn action { type = "forward" target_group_arn = aws_lb_target_group.gateway.arn } condition { host_header { values = ["api.yourdomain.com"] } } } 方案3:Kubernetes Ingress yaml apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: gateway-ingress annotations: nginx.ingress.kubernetes.io/upstream-hash-by: "$remote_addr" # 会话保持 spec: rules: - host: api.yourdomain.com http: paths: - path: / pathType: Prefix backend: service: name: api-gateway port: number: 6001 四、关键配置说明 1. 健康检查配置 yaml management: endpoint: health: show-details: always probes: enabled: true health: readiness: enabled: true liveness: enabled: true 2. 负载均衡策略对比 策略类型 配置方式 适用场景 轮询 Nginx默认 常规流量分发 加权轮询 weight=x 异构服务器 最少连接 least_conn 长连接服务 IP Hash ip_hash 会话保持 区域感知 Eureka metadata 多活部署 3. 启动参数示例 bash # 节点1 java -jar api-gateway.jar --server.port=6001 --spring.profiles.active=6001 # 节点2 java -jar api-gateway.jar --server.port=6005 --spring.profiles.active=6005 # 节点3 java -jar api-gateway.jar --server.port=6006 --spring.profiles.active=6006 五、验证与监控 1. 验证负载均衡 bash # 连续访问测试 for i in {1..10}; do curl http://api.yourdomain.com/route-test; done 2. 监控关键指标 Nginx监控: bash nginx -T | grep stub_status curl http://localhost/nginx_status Spring Boot Actuator: text GET /actuator/metrics/http.server.requests 3. 日志关联分析 bash # 查看各节点日志中的请求分布 grep "X-Backend-Server" gateway.log 六、生产环境建议 会话保持: nginx upstream { ip_hash; # 或使用cookie保持 } 灰度发布: nginx # 通过Header分流 map $http_x_traffic_type $backend { "v2" gateway_v2; default gateway_v1; } 熔断配置: yaml spring: cloud: gateway: routes: - id: fallback uri: lb://fallback-service predicates: - Path=/fallback/** filters: - name: CircuitBreaker args: name: myCircuitBreaker fallbackUri: forward:/default-fallback 通过以上方案,您可以实现三个网关节点的负载均衡,并根据业务需求选择合适的流量分配策略。 https://chat.deepseek.com/a/chat/s/6a5ef2c0-6211-4e4e-868e-926e675f42e6