Keith Bennett
Keith Bennett

Reputation: 947

Spring Cloud Gateway and DiscoveryClient Routes

I am trying to migrate a gateway that's working using spring-cloud-starter-netflix-zuul to Spring Cloud Gateway, and I'm running into issues with request routing.

I ran across the following documentation regarding configuring predicates and filters for DiscoveryClient routes:

Configuring Predicates and Filters For DiscoveryClient Routes

Here's a snippet from my Netflix Zuul configuration:

zuul:
  routes:
    account-service: /accounts/**

And here is a snippet from the Spring Cloud Gateway configuration where I'm attempting to configure an equivalent route:

spring:
  cloud:
    gateway:
      routes:
        - id: account-service-route
          uri: lb://account-service
          predicates:
          - Path=/accounts/**

I am using Spring Cloud Eureka as my Discovery Server (in a separate microservice), and I do not currently have any configuration as outlined in Configuring Predicates and Filters For DiscoveryClient Routes

If I issue a request to /accounts/***, I receive a 404 reponse. If I change the Spring Cloud Gateway configuration to the following and issue the same request to /account-service/***, I receive a 403 Forbidden response:

spring:
  cloud:
    gateway:
      routes:
        - id: account-service-route
          uri: lb://account-service
          predicates:
          - Path=/account-service/**

I am suspecting that this has something to do with the default behavior of Spring Cloud Gateway with respect to how DiscoveryClient Routes are configured, but I don't see enough in the logs to point me in the right direction.

So, my question is this: When using Spring Cloud Gateway with Spring Cloud Eureka, is it required to make the configuration entries as outlined in Configuring Predicates and Filters For DiscoveryClient Routes?

If so, can someone provide more explanation/clarity around what needs to be configured referring back to my route examples? Sorry if I'm missing something, but it's not obvious to me from what I read what exactly is needed with this configuration. For example, are the spring.cloud.gateway.discovery.locator.predicates and spring.cloud.gateway.discovery.locator.filters configured in addition to or in place of the spring.cloud.gateway.routes predicates and filters?

If not, what other configuration(s) might I be missing?

I am on Spring Cloud Finchley.SR3/Spring Boot 2.0.8.RELEASE.

Upvotes: 8

Views: 9279

Answers (2)

Volo Myhal
Volo Myhal

Reputation: 144

The setting

  cloud:
    gateway:
      discovery:
        locator:
          enabled: true  # default false

will automatically create the route for EVERY service registered on Eureka, so you can access the EVERY service by http://<gateway>/<service-name>/path, and it will redirect to lb://<service-name>/path utilizing Load Balancer. It will also remove the route from Gateway when service is de-registered at Eureka.

This setting is quite easy and fit to many situations. Just remember that routes will appear/disappear in ~30 seconds by default, and you may want to decrease that interval, to 5 secs for example with

eureka:
  client:
    registry-fetch-interval-seconds: 5

Upvotes: 2

Hank
Hank

Reputation: 1338

Spring Cloud Gateway just enable request routing by default, and you have to enable discovery support explicitly. Try this:

spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true  # default false
      routes:
        - id: account-service-route
          uri: lb://account-service #
          predicates:
            - Path=/account/**

Now you should be able access your service via http://<gateway>/account-service/account/**

Upvotes: 9

Related Questions