/fallback

Fallback example with spring cloud gateway

Primary LanguageJava

Fallback Example

Using fallbackUrl with path variable

When I do a request in the first url:

curl -v http://localhost:8080/payments/123/details

The gateway receive a error 502 (because the route is directed to https://httpbin.org/status/502) The CircuitBreaker start to work and do the foward to fallbackUri (static): paymentFallbackPathResolver

- id: new-payment-route
  uri: https://httpbin.org
  predicates:
    - Path=/payments/{resource_id}/details
  filters:
    - RewritePath=/(?<segment>.*), /status/502
    - name: CircuitBreaker
      args:
        name: payment-circuit-breaker
        fallbackUri: forward:/paymentFallbackPathResolver

So a created a custom filter just to resolve the path variable:

- id: fallback-path-resolver-route
  uri: http://localhost:8080
  predicates:
    - Path=/paymentFallbackPathResolver
  filters:
    - name: Fallback
      args:
        fallbackUri: /fallback-payments/{resource_id}/details

And after my custom filter Fallback process the request, my real fallback receive it:

- id: fallback-payment-route
  uri: http://localhost:8080
  predicates:
    - Path=/fallback-payments/{resource_id}/details
  filters:
    - RewritePath=/(?<segment>.*), /legacy-billing/${segment}

It Works, but I needed to wrote a custom filter and create one route only to resolve path variables. With my PR: spring-cloud/spring-cloud-gateway#1641, the same solution can be done with this:

- id: new-payment-route
  uri: https://httpbin.org
  predicates:
    - Path=/payments/{resource_id}/details
  filters:
    - RewritePath=/(?<segment>.*), /status/502
    - name: CircuitBreaker
      args:
        name: payment-circuit-breaker
        fallbackUri: forward:/fallback-payments/{resource_id}/details
        
- id: fallback-payment-route
  uri: http://localhost:8080
  predicates:
    - Path=/fallback-payments/{resource_id}/details
  filters:
    - RewritePath=/(?<segment>.*), /legacy-billing/${segment}        

With less code, because the fallbackUri accepts path variable.