5

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?

Morteza
  • 642
  • 7
  • 17
arj
  • 887
  • 1
  • 15
  • 37
  • 2
    Add : eureka.instance.prefer-ip-address=true to application.properties file to all your microservices & api-gateway – Vipul Gupta Jan 27 '21 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! – Superman Jan 27 '22 at 20:01

13 Answers13

7

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

troy
  • 2,145
  • 2
  • 23
  • 31
3

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

arj
  • 887
  • 1
  • 15
  • 37
3

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

@Bean
public HttpClient httpClient() {
    return HttpClient.create().resolver(DefaultAddressResolverGroup.INSTANCE);
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
1

Add in your application.properties:

spring.cloud.discovery.enabled=true
Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
1

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/
Cyril Sojan
  • 149
  • 7
1

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.

Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
Raj Hirani
  • 182
  • 1
  • 5
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**
1

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.

nonder
  • 115
  • 7
0

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

ilham kh
  • 21
  • 1
0

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.

0

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

Ravindra
  • 51
  • 5
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

aminator
  • 396
  • 7
  • 18