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

Spring Cloud网关报错:getaddrinfo EAI_AGAIN及404问题求助

问题排查与解决方案

1. 修复Cloud Gateway的YAML配置缩进错误

YAML格式严格依赖缩进,你的Gateway配置文件存在大量缩进混乱,导致路由规则无法被正确加载。正确的缩进格式如下:

spring:
  application:
    name: GATEWAY-SERVICE
  cloud:
    gateway:
      routes:
        - id: order-service
          uri: lb://ORDER-SERVICE
          predicates:
            - Path=/order/**
          filters:
            - name: CircuitBreaker
              args:
                name: order-service
                fallbackuri: forward:/orderFallBack
        - id: res-service
          uri: lb://RESTAURANT-SERVICE
          predicates:
            - Path=/res/**
          filters:
            - name: CircuitBreaker
              args:
                name: res-service
                fallbackuri: forward:/restaurantFallBack
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    preferIpAddress: true       
server:
  port: 8988
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream
hystrix:
  command:
    fallbackcmd:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000

2. 修正业务服务的Eureka配置错误

你的RESTAURANT-SERVICE和ORDER-SERVICE中,eureka.client.serviceUrl.instance.preferIpAddress配置路径错误,正确的配置应该是eureka.instance.preferIpAddress,修改后的application.properties片段:

# RESTAURANT-SERVICE 修正后
eureka.instance.preferIpAddress=true
# ORDER-SERVICE 修正后
eureka.instance.preferIpAddress=true

原配置会导致服务实例的IP偏好设置不生效,Eureka注册的可能是主机名而非IP,Gateway解析时出现DNS错误。

3. 确保Gateway引入必要依赖

Spring Cloud Gateway需要依赖服务发现客户端和负载均衡器来解析lb://前缀的服务地址。检查你的Gateway项目pom.xml(或build.gradle)是否包含以下依赖:

<!-- Eureka客户端依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 负载均衡器依赖(Spring Cloud 2020.0+版本需要显式引入) -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

缺少loadbalancer依赖会导致Gateway无法将服务名转换为具体的实例地址,触发getaddrinfo EAI_AGAIN错误。

4. 验证服务注册状态

访问Eureka控制台http://localhost:8761,确认ORDER-SERVICE和RESTAURANT-SERVICE已出现在Instances currently registered with Eureka列表中。

  • 如果服务未注册:检查业务服务的Eureka配置(defaultZone是否正确、register-with-eureka是否为true),以及服务启动时是否有连接Eureka的报错日志。

5. 检查请求路径匹配

确保你发送的请求路径符合Gateway路由规则:

  • 访问ORDER-SERVICE的请求路径应为http://localhost:8988/order/**(例如http://localhost:8988/order/list)
  • 访问RESTAURANT-SERVICE的请求路径应为http://localhost:8988/res/**(例如http://localhost:8988/res/detail)
    路径不匹配会直接返回404错误。

内容的提问来源于stack exchange,提问作者Pragya Kumari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 18:55:18