Spring Cloud API Gateway routing not working
Asked Answered
I

18

9

I have designed a micro service prototype using below technologies

  1. Eureka Server
  2. a service
  3. Spring Cloud API Gateway

Above mentioned service are registered in the Eureka Server

enter image description here

API Gateway routing Configuration

server.port=8080
eureka.client.serviceUrl.defaultZone = http://localhost:8083/eureka
spring.application.name=ApiGateway
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

spring.cloud.gateway.routes[0].id=service1
spring.cloud.gateway.routes[0].uri=lb://MICROSERVICE1
spring.cloud.gateway.routes[0].predicates[0]=Path=/service1/**

The service Configuration

server.port=8081
server.address=127.0.0.1
eureka.client.serviceUrl.defaultZone = http://localhost:8083/eureka
spring.application.name=MicroService1
error.whitelabel.enabled= false

Controller

@RestController
@RequestMapping("/service1")
public class HomeController {
    @GetMapping("/message")
    public String hello() {
        return "response from micro service1";
    }

}

When I send a request to the gateway it's showing the below error

2020-12-16 22:26:09.770 ERROR 16700 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [d3334561-1]  500 Server Error for HTTP GET "/service1/message"

java.net.UnknownHostException: failed to resolve 'LAPTOP-KU56B6A8' after 3 queries 
    at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1013) ~[netty-resolver-dns-4.1.55.Final.jar:4.1.55.Final]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP GET "/service1/message" [ExceptionHandlingWebHandler]

How can we solve the above issue?

Increasing answered 17/12, 2020 at 1:48 Comment(2)
Add : eureka.instance.prefer-ip-address=true to application.properties file to all your microservices & api-gatewayNib
In your Service Configuration, you have set the application name as MicroService1 but have set the id as service1 in API Gateway Routing Configuration. Also, your uri is set as MICROSERVICE1. All 3 different, why? Put the id and uri to MicroService1 as the application name is being registered with it only. See if it helps!Medium
I
4

i have modified the API Gate Way routing Configuration like below

spring.cloud.gateway.routes[0].id=service1
spring.cloud.gateway.routes[0].uri=http://localhost:8081/service1/
spring.cloud.gateway.routes[0].predicates[0]=Path=/service1/**

Now is working fine

Increasing answered 17/12, 2020 at 4:46 Comment(2)
You simply removed the loadbalancingSport
your totally killed the purpose of microservices, by removing load balancing.Ainslee
A
8

Add eureka.instance.hostname=localhost in both the microservices instances this will work and not give an error

Aiglet answered 18/1, 2021 at 6:58 Comment(2)
Can you explain why this works? And what was wrong before?Scevo
ran into the same issue, this works like a charm, not sure why though lol maybe API gateway doesn't recognize the localhost until you explicitly specify it ?Frizzly
I
4

i have modified the API Gate Way routing Configuration like below

spring.cloud.gateway.routes[0].id=service1
spring.cloud.gateway.routes[0].uri=http://localhost:8081/service1/
spring.cloud.gateway.routes[0].predicates[0]=Path=/service1/**

Now is working fine

Increasing answered 17/12, 2020 at 4:46 Comment(2)
You simply removed the loadbalancingSport
your totally killed the purpose of microservices, by removing load balancing.Ainslee
R
4

Add this bean in your API gateway and you are good to go.

@Bean
public HttpClient httpClient() {
    return HttpClient.create().resolver(DefaultAddressResolverGroup.INSTANCE);
}
Rigdon answered 5/4, 2022 at 15:48 Comment(1)
the above bean should be added to the API gate way and all will be fine.Harts
R
4

Check your pom.xml file, depending on the version of the dependencies with which the project got initialized there is a slight chance that you have :

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
</dependency>

instead of :

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

notice that in the first it is using spring-cloud-starter-gateway-mvc instead of the bare spring-cloud-starter-gateway

correct it and hopefully it might fix it for you.

Redbird answered 27/2, 2024 at 11:45 Comment(0)
A
2

Add in your application.properties:

spring.cloud.discovery.enabled=true
Adjunction answered 13/4, 2021 at 4:52 Comment(0)
F
2

Add below to both gateway and individual microservice fix the issue

eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8010/eureka/
Fazeli answered 6/5, 2021 at 11:51 Comment(0)
R
2

You can add the following in application.yml file

spring:
  cloud:
    gateway:
      routes:
        - id: test-service
          uri: lb://MICROSERVICE1
          predicates:
            - Path=/microservice1/**
          filters:
            - RewritePath=/microservice1/(?<segment>.*), /$\{segment}

with this it should works.

Like let say if your microservice1 is url is localhost:8081/service1/message then you can define the base path of your microservice1 in api-gateway by setting up the path as i did in above configuration.

Reactive answered 27/7, 2021 at 12:53 Comment(0)
D
2

This is the only solution works among all of the answers above.

@Bean
public HttpClient httpClient() {
    return HttpClient.create().resolver(DefaultAddressResolverGroup.INSTANCE);
}

This is the default behavior, so no effect.

spring.cloud.discovery.enabled=true

This has nothing to do with the discovery client. It is related with the discovery server.

eureka.instance.hostname=localhost

So if you don't know, just don't mess it up with wrong directions.

Debus answered 21/7, 2022 at 7:22 Comment(0)
B
2

Please check your dependency like the below

org.springframework.cloud

spring-cloud-starter-gateway

it should not contain like below

org.springframework.cloud

spring-cloud-starter-gateway-mvc

Please remove -mvc from dependency.

Blackburn answered 18/4, 2024 at 21:9 Comment(1)
The wost part of your answer is that it is 100% right. I don't know why the initializr add -mvc.Normi
D
1

hello jebji if you still have this problem add spring.cloud.discovery.enabled=true in application.properties

Dogbane answered 25/12, 2020 at 14:41 Comment(1)
I have the same issueTrussell
C
1

Only Add the following property into your API gateway:

spring.cloud.discovery.enabled=true

Make sure you already added DevTool maven dependency into your API gateway project but if not then restart it.

Chasten answered 26/1, 2021 at 3:56 Comment(0)
C
1

add flowing property in application.property file of all eruka client microservice and api gateway , i face same issue and resolve doing same activity

spring.cloud.discovery.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id= true
spring.cloud.gateway.discovery.locator.enabled= true
eureka.instance.hostname=localhost
Churrigueresque answered 27/2, 2021 at 11:7 Comment(0)
M
1

I have modified my .yaml file with this configuration. Issue resolved for me.

**server:
  port: 9999
spring:
  application:
    name: gateway-ws
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
      - id: userService
        uri: http://user-service/
        predicates:
        - Path=/user/**
      - id: contactService
        uri: http://contact-service/
        predicates:
        - Path=/contact/**
        
        
eureka:
  client:
    service-url:
       defaultZone: http://localhost:8085/eureka**
Minutely answered 15/5, 2022 at 1:19 Comment(0)
A
0

after adding all the above properties then also if you are facing issue then try the below one,

don't use lb://albums_service , but use lb://albums-service .Because URI don‘t support underline.

Avid answered 26/5, 2021 at 4:12 Comment(0)
C
0

The error message is "failed to resolve 'LAPTOP-KU56B6A8'".

This is an DNS issue.

  • You can set eureka.instance.prefer-ip-address=true in the service.

So it will register with its ip at Eureka and the DNS issue can be avoided.

This is actually the same issue as this QUESTION.

With this ANSWER

Christmann answered 28/1, 2022 at 22:12 Comment(0)
K
0

For anyone else who's coming here and not using Eureka; In my case I was using Docker and I've specified docker service name instead of localhost in the routes config! (and vice versa). Also putting this here because I am making this mistake twice and if I need to remind myself again :) (Or maybe we can define this for different environments I guess.) For example:

uri: http://commit-tracker:8081

to

 uri: http://localhost:8081
Krol answered 6/10, 2023 at 17:6 Comment(0)
P
0

I was also facing same issue

I am using

spring.cloud.gateway.discovery.locator.enabled=true

in gateway.

springboot version : 3.2.3
Polycarp answered 3/3, 2024 at 4:21 Comment(0)
A
0

Please use spring-boot-starter-webflux and spring-cloud-starter-gateway dependency. It worked for me

Aymara answered 6/4, 2024 at 17:12 Comment(1)
With only spring-cloud-starter-gateway is enoughNormi

© 2022 - 2025 — McMap. All rights reserved.