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?

2
  • 2
    Add : eureka.instance.prefer-ip-address=true to application.properties file to all your microservices & api-gateway Commented Jan 27, 2021 at 13:44
  • 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! Commented Jan 27, 2022 at 20:01

18 Answers 18

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain why this works? And what was wrong before?
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 ?
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

2 Comments

You simply removed the loadbalancing
your totally killed the purpose of microservices, by removing load balancing.
4

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

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

1 Comment

the above bean should be added to the API gate way and all will be fine.
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.

Comments

4

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.

2 Comments

The wost part of your answer is that it is 100% right. I don't know why the initializr add -mvc.
Note that spring-cloud-starter-gateway is the reactive gateway, and spring-cloud-starter-gateway-mvc is the newer non-reactive gateway. They are closely related but two totally separate frameworks.
3

Add in your application.properties:

spring.cloud.discovery.enabled=true

Comments

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/

Comments

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.

Comments

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.

Comments

1

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

1 Comment

I have the same issue
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.

Comments

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

Comments

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

Comments

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.

Comments

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

Comments

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

Comments

0

I was also facing same issue

I am using

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

in gateway.

springboot version : 3.2.3

Comments

0

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

1 Comment

With only spring-cloud-starter-gateway is enough

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.