bhoo
bhoo

Reputation: 111

Spring cloud gateway auto routing with eureka

I work with Spring to create microservices. I am using Eureka for service discovery and Spring cloud gateway for routing. I would want to auto-route for the number of services I am having.

For example if one service 'eureka-client' registers to Eureka, and for routing with Spring Cloud Gateway, I've to create a route all by myself for each service like following.

  routes:
  - id: eureka-client
    uri: lb://eureka-client
    predicates:
    - Path=/eureka-client/**

With a few services that's acceptable but I might get hundreds of services in the end. And each has to write its own route in Spring Cloud Gateway. I have used spring.cloud.gateway.discovery.locator.enabled=true and doesn't solve the issue. Basically I am trying to eliminate the routes config in the yaml file.

Is there a way to provide auto routing from Spring Cloud Gateway to each service from Eureka?

Reference: https://cloud.spring.io/spring-cloud-gateway/reference/html/#discoveryclient-route-definition-locator

I am getting 404 as it was not able to get the proper routing Any help would be appreciated. Thanks.

Upvotes: 6

Views: 9362

Answers (4)

Tafadzwa Chimberengwa
Tafadzwa Chimberengwa

Reputation: 409

This can be achieved just by using the following properties If you are using application.properties:

spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id: true

Or the following if you are using application.yml

  cloud:
    gateway:
     discovery:
       locator:
         enabled: true
           lower-case-service-id: true

And you don't need to explicitly specify the routes.

Upvotes: 5

Asier Naiz
Asier Naiz

Reputation: 151

The auto routing configuration is described in:

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-discoveryclient-route-definition-locator

In case of Eureka Netflix client, the required dependency must be added in pom.xml, it must be enabled in the main app class (@EnableEurekaClient) and the required properties must be specified in applications.yml:

eureka:
    client:
        fetchRegistry: true
        registerWithEureka: false
        serviceUrl:
            defaultZone: http://localhost:port/eureka
    instance:
        preferIpAddress: true

In order to enable the gateway with services in lower case, add:

spring:
    application:
        name: gateway service
    cloud:
        gateway:
            discovery:
                locator:
                    enabled: 'true'
                    lower-case-service-id: 'true'

Upvotes: 1

Numan Karaaslan
Numan Karaaslan

Reputation: 1645

You must make the gateway aware of the eureka server with this

eureka.client.service-url.defaultZone=http://user:pass@localhost:8761/eureka

And then prevent the gateway from registering itself to eureka

eureka.client.register-with-eureka=false

Setting the second option to false is important as it causes 404 errors with loadbalancing using lb:servicename structure.

Upvotes: 0

Ali Kurnaz
Ali Kurnaz

Reputation: 57

Try to access with upper-case url like:

<localhost>:<port>/EUREKA-CLIENT/<path>

Alternatively, you can set this:

spring.cloud.gateway.discovery.locator.lower-case-service-id=true

Upvotes: -2

Related Questions