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

Spring Cloud API Gateway结合Eureka Registry自定义路由失效问题

Spring Cloud Gateway自定义路由失效问题

环境

  • Spring Boot 3.1.5
  • Java 17
  • 涉及组件:Eureka注册中心、Gateway网关、Seguranca微服务

问题

配置Gateway自定义路由,希望将匹配/api/**的请求转发到Seguranca微服务,但该路由无法生效。添加服务发现定位器配置后,自动生成的路由(如/seguranca/**)可以正常工作,但自定义路由依旧失效。

相关配置

Gateway初始自定义路由配置

server:
    port: 8080

spring:
    application:
        name: gateway
    cloud:
        gateway:
            routes:
            - id: seguranca
              uri: lb://seguranca
              predicates:
                  - Path=/api/**

eureka:
    client:
        serviceUrl:
            defaultzone: http://localhost:8761/eureka

添加发现定位器后的Gateway配置

添加以下配置后自动路由生效,但自定义路由仍失效:

spring:
    cloud:
        gateway:
            discovery:
              locator:
                  enabled: true
                  lower-case-service-id: true
            routes:
            - id: seguranca
              uri: lb://seguranca
              predicates:
                  - Path=/api/**

Seguranca微服务配置

spring:
    application:
        name: seguranca

Seguranca微服务控制器代码

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class AuthenticationController {

    private final AuthenticationManager authenticationManager;
    private final TokenService tokenService;

    @PostMapping("/authenticate")
    public ResponseEntity authenticate(@RequestBody @Valid LoginRecord loginRecord) {

        var authenticationToken = new UsernamePasswordAuthenticationToken(loginRecord.username(), loginRecord.password());
        var authentication = authenticationManager.authenticate(authenticationToken);

        var tokenJWT = tokenService.createToken((UserDetailsImpl) authentication.getPrincipal());
        return ResponseEntity.ok(new DataTokenJWT(tokenJWT));
    }

}

Eureka注册中心配置

eureka:
    client:
        fetch-registry: false
        register-with-eureka: false
    instance:
        hostname: localhost
server:
    port: 8761
spring:
    application:
        name: service-registry

排查与解决建议

  1. 调整路由优先级
    启用discovery.locator后,自动路由的优先级可能高于自定义路由。给自定义路由设置更低的order值(数值越小优先级越高):

    routes:
    - id: seguranca
      uri: lb://seguranca
      predicates:
          - Path=/api/**
      order: 0
    
  2. 检查路径匹配与请求地址
    确认请求地址是否严格符合/api/**规则,比如测试请求http://localhost:8080/api/authenticate。同时检查Gateway是否有路径重写、全局过滤器等影响路径匹配的配置。

  3. 验证Eureka服务注册状态
    登录Eureka控制台(http://localhost:8761),确认Seguranca微服务已成功注册,Gateway能通过lb://seguranca解析到可用实例。

  4. 查看Gateway日志
    开启DEBUG级别的日志,追踪路由匹配过程:

    logging:
        level:
            org.springframework.cloud.gateway: DEBUG
            org.springframework.cloud.loadbalancer: DEBUG
    

    从日志中可以看到请求是否命中自定义路由,以及是否存在服务解析、转发错误。

  5. 修正Eureka配置的大小写问题
    Eureka配置中defaultZone是驼峰命名,原配置中的defaultzone可能导致服务发现异常,改为:

    eureka:
        client:
            serviceUrl:
                defaultZone: http://localhost:8761/eureka
    

内容的提问来源于stack exchange,提问作者rodrigo costa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 00:00:34