arj
arj

Reputation: 983

Spring Cloud API Gateway routing not working

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?

Upvotes: 9

Views: 54290

Answers (18)

Abhishek kumar
Abhishek kumar

Reputation: 53

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.

Upvotes: 4

Baidyanath
Baidyanath

Reputation: 19

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

Upvotes: 0

sparsh610
sparsh610

Reputation: 1601

I was also facing same issue

I am using

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

in gateway.

springboot version : 3.2.3

Upvotes: 0

Yehdhih ANNA
Yehdhih ANNA

Reputation: 1300

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.

Upvotes: 4

Jolly Good
Jolly Good

Reputation: 550

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

Upvotes: 0

Abdelrahman Elayashy
Abdelrahman Elayashy

Reputation: 590

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

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

Upvotes: 4

nonder
nonder

Reputation: 155

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.

Upvotes: 2

Ankit pathak
Ankit pathak

Reputation: 11

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

Upvotes: 1

aminator
aminator

Reputation: 481

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

Upvotes: 0

user15618336
user15618336

Reputation: 31

Add in your application.properties:

spring.cloud.discovery.enabled=true

Upvotes: 3

Raj Hirani
Raj Hirani

Reputation: 192

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.

Upvotes: 2

Ravindra
Ravindra

Reputation: 71

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.

Upvotes: 0

Cyril Sojan
Cyril Sojan

Reputation: 179

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/

Upvotes: 2

shrawan tiwari
shrawan tiwari

Reputation: 29

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

Upvotes: 1

gourav kumar
gourav kumar

Reputation: 11

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.

Upvotes: 1

Shubham Chinchure
Shubham Chinchure

Reputation: 89

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

Upvotes: 8

ilham kh
ilham kh

Reputation: 41

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

Upvotes: 1

arj
arj

Reputation: 983

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

Upvotes: 4

Related Questions