You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Cloud Gateway无法找到Kubernetes远程Pod服务求助

问题:Spring Cloud Gateway 转发请求返回503 SERVICE_UNAVAILABLE

我基于Spring Cloud Gateway和Spring微服务搭建了测试项目,使用依赖implementation 'org.springframework.cloud:spring-cloud-kubernetes-client-discovery'。已在Gateway的配置文件中配置路由,两个服务都部署到了Kubernetes,但请求无法转发,日志返回503 SERVICE_UNAVAILABLE错误,怀疑是微服务名称未解析。

附日志信息:

2024-04-02 13:21:55.817 TRACE 1 --- [     parallel-1] o.s.c.g.filter.RouteToRequestUrlFilter   : RouteToRequestUrlFilter start
2024-04-02 13:21:55.886 TRACE 1 --- [     parallel-1] o.s.c.g.filter.GatewayMetricsFilter      : spring.cloud.gateway.requests tags: [tag(httpMethod=GET),tag(httpStatusCode=503),tag(outcome=SERVER_ERROR),tag(routeId=microservice_1),tag(routeUri=lb://microservice_1),tag(status=SERVICE_UNAVAILABLE)]
2024-04-02 13:22:42.084 TRACE 1 --- [     parallel-1] o.s.c.g.f.WeightCalculatorWebFilter      : Weights attr: {}
2024-04-02 13:22:42.089 TRACE 1 --- [     parallel-1] o.s.c.g.h.p.PathRoutePredicateFactory    : Pattern "/api/microservice_1/dashboard/users" matches against value "/api/microservice_1/dashboard/users"
2024-04-02 13:22:42.095 DEBUG 1 --- [     parallel-1] o.s.c.g.h.RoutePredicateHandlerMapping   : Route matched: microservice_1
2024-04-02 13:22:42.096 DEBUG 1 --- [     parallel-1] o.s.c.g.h.RoutePredicateHandlerMapping   : Mapping [Exchange: GET http://11.111.63.1:30057/api/microservice_1/dashboard/users?startDate=2024-03-02T14:21:45.540Z&endDate=2024-04-02T13:22:40.533Z] to Route{id='microservice_1', uri=lb://microservice_1, order=0, predicate=Paths: [/api/microservice_1/dashboard/users], match trailing slash: true, gatewayFilters=[[[RemoveRequestHeader name = 'Cookie'], order = 1], [[SpringCloudCircuitBreakerResilience4JFilterFactory name = 'microservice_1', fallback = [null]], order = 2], [[RewritePath /api/dashboard/users = '/dashboard/users'], order = 3]], metadata={}}
2024-04-02 13:22:42.096 DEBUG 1 --- [     parallel-1] o.s.c.g.h.RoutePredicateHandlerMapping   : [9b4aa35e-33] Mapped to org.springframework.cloud.gateway.handler.FilteringWebHandler@20a20ac5
2024-04-02 13:22:42.097 DEBUG 1 --- [     parallel-1] o.s.c.g.handler.FilteringWebHandler      : Sorted gatewayFilterFactories: [[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RemoveCachedBodyFilter@39c1fe0b}, order = -2147483648], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@b016b4e}, order = -2147482648], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@3e5d4f6b}, order = -1], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardPathFilter@367b22e5}, order = 0], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.GatewayMetricsFilter@10bd9df0}, order = 0], [[RemoveRequestHeader name = 'Cookie'], order = 1], [[SpringCloudCircuitBreakerResilience4JFilterFactory name = 'microservice_1', fallback = [null]], order = 2], [[RewritePath /api/dashboard/users = '/dashboard/users'], order = 3], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@5ae95707}, order = 10000], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.config.GatewayNoLoadBalancerClientAutoConfiguration$NoLoadBalancerClientFilter@3ae0b770}, order = 10150], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@fca387}, order = 2147483646], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@1ac45389}, order = 2147483647], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@62891fc8}, order = 2147483647]]

可能的原因及排查方向

  • Kubernetes服务发现权限不足:Spring Cloud Kubernetes Client需要通过K8s API获取服务实例信息,必须确保Gateway所在Pod的ServiceAccount拥有list、watch Pods和Services的权限。检查是否配置了对应的Role、RoleBinding(或ClusterRoleBinding),赋予ServiceAccount相关权限。

  • 后端服务的Kubernetes Service配置不匹配:

    • 确认后端微服务对应的K8s Service名称和Gateway路由中lb://microservice_1的microservice_1完全一致(K8s Service名称仅支持小写字母、数字和连字符,注意大小写)。
    • 检查Service的selector是否正确匹配后端Pod的标签,确保Pod被正确关联到Service,否则Service无法找到可用实例。
  • Gateway未启用Kubernetes服务发现:检查Gateway的配置文件,确认是否添加了spring.cloud.kubernetes.discovery.enabled=true配置,确保服务发现功能正常开启。

  • 后端Pod未处于就绪状态:查看后端微服务的Pod状态,确认Pod处于Running状态且就绪探针(Readiness Probe)返回成功。若Pod未就绪,K8s Service不会将其纳入可用实例列表,Gateway自然无法找到可用节点转发请求。

  • 负载均衡依赖缺失:日志中出现NoLoadBalancerClientFilter,说明Gateway可能缺少负载均衡客户端依赖。需引入spring-cloud-starter-loadbalancer依赖,才能正确处理lb://开头的URI,实现服务实例的负载转发。

  • 路由路径重写配置错误:日志中路径重写规则为RewritePath /api/dashboard/users = '/dashboard/users',但实际请求路径是/api/microservice_1/dashboard/users,重写规则可能无法匹配正确路径,导致转发后路径无效(虽然这不是503的直接原因,但会影响请求最终是否能被后端服务处理)。

内容的提问来源于stack exchange,提问作者Peter Penzov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:34:55