Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 428
Keith Bennett Avatar asked Mar 12 '19 20:03

Keith Bennett


1 Answers

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/**

like image 61
Hank Avatar answered Oct 11 '22 12:10

Hank