Search code examples
springcloudspring-cloudgatewayspring-cloud-gateway

Redirect all non resolved endpoints to a specific registered service


I am trying to redirect all endpoints (which are not resolved by service-discovery) route to a specific url/lb.

I have a spring cloud gateway server configured to resolve every /api request to via Eureka.

spring:
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
          predicates:
            - name: Path
              args:
                pattern: "'/api/'+serviceId.toLowerCase()+'/**'"
          filters:
            - name: RewritePath
              args:
                regexp: "'/api/' + serviceId.toLowerCase() + '/(?<remaining>.*)'"
                replacement: "'/${remaining}'"

There is another frontend service too deployed and registered in Eureka. My goal is to route everything else to this frontend service. I tried adding the following route to do so:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
          predicates:
            - name: Path
              args:
                pattern: "'/api/'+serviceId.toLowerCase()+'/**'"
          filters:
            - name: RewritePath
              args:
                regexp: "'/api/' + serviceId.toLowerCase() + '/(?<remaining>.*)'"
                replacement: "'/${remaining}'"
      routes:
        - id: frontend
          order: -1
          uri: lb://frontend
          predicates:
            - name: Path
              args:
                pattern: "^((?!api).)*$"

But it didn't worked.

Any idea on how to route everything else that is not discovered in eureka to a specific service/url?

Thanks for the help


Solution

  • spring:
      cloud:
        gateway:
          discovery:
            locator:
              lowerCaseServiceId: true
              enabled: true
              predicates:
                - name: Path
                  args:
                    pattern: "'/api/'+serviceId.toLowerCase()+'/**'"
              filters:
                - name: RewritePath
                  args:
                    regexp: "'/api/' + serviceId.toLowerCase() + '/(?<remaining>.*)'"
                    replacement: "'/${remaining}'"
          routes:
            - id: frontend
              order: 10
              uri: lb://frontend
              predicates:
                - Path=/**