Spring cloud gateway auto routing with eureka
Asked Answered
V

4

6

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.

Vanhomrigh answered 5/11, 2019 at 15:50 Comment(0)
S
5

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.

Stadler answered 13/8, 2020 at 2:30 Comment(0)
S
1

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'
Stertorous answered 4/9, 2021 at 21:18 Comment(0)
S
0

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.

Socialistic answered 28/11, 2020 at 17:22 Comment(1)
It doesn't change anything if the gateway is itself an eureka client. I tested the both cases and didn't lead to the expected resultThrave
L
-2

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
Lounge answered 12/11, 2019 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.