spring-bootspring-cloud-gatewayservice-discoveryspring-cloud-eureka

Spring Boot API Gateway UnknownHostException


I am unable to call a micro service through the ApiGateway. It's giving the error below:

java.net.UnknownHostException: Failed to resolve 'ORDER-SERVICE' [A(1), AAAA(28)] after 2 queries 
    at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1151) ~[netty-resolver-dns-4.1.111.Final.jar:4.1.111.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 POST "/api/order/create" [ExceptionHandlingWebHandler]

I have seen several questions asked about similar issues. I tried all the solutions. But still, it is not resolved. I am using:

Spring Boot: 3.3.1, Spring Cloud version: 2023.0.3, Java: 17

ApiGateway properties

spring.application.name=api-gateway
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true

eureka.instance.hostname=localhost
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
spring.cloud.gateway.routes[0].id=ORDER-SERVICE
spring.cloud.gateway.routes[0].uri=http://ORDER-SERVICE/
spring.cloud.gateway.routes[0].predicates[0]=Path=/api/order/**

spring.cloud.discovery.enabled=true

Order service

spring.application.name=order-service
eureka.instance.hostname=localhost
eureka.instance.prefer-ip-address=true
server.port=8082

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.discovery.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id= true
spring.cloud.gateway.discovery.locator.enabled= true

Dependency for the api gateway

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

The Discovery server has 2 running instances:

Screenshot of instances currently registered with Eureka

It would be very helpful if you could find a solution to this.


Solution

  • Here you replace from spring.cloud.gateway.routes[0].uri=http://ORDER-SERVICE/ to spring.cloud.gateway.routes[0].uri=lb://ORDER-SERVICE

    reference

    http://ip-address {cannot be a service name}/
    

    Hope that helps.