Search code examples
javaspringspring-bootnetflix-eurekagateway

Name resolution failure with Spring Gateway and Eureka


Good day,

At this moment I am working on a very simple gateway that (for now) only needs to redirect incoming HTTP POST and GET requests.

THE SETUP:

  • The Eureka Server: the location where my Spring Boot microservices are registered;
  • The Spring Gateway: maps all incoming HTTP POST and GET requests and routes them to the proper microservice;
  • The Spring Boot microservices: doing just some thingies as requested :)

Note: I'm kinda new to this gateway stuff, just you know :).

The microservice is registered fine with the Eureka server. Its webbased GUI shows me that the instance "MY-MICRO-SERVICE" is registered with the Eureka server. Other (Spring Boot) services can use that name ("MY-MICRO-SERVICE") without issues, so for them it works fine. Just this gateway can't handle the instance name; it seems it only accepts IP addresses (which I just want to prevent, as the microservice can change from servers and therefor their IP address). And the Eureka server is not configured to only allow/use IP addresses.


THE ISSUE:

All runs smooth when the Gateway has a route that holds an IP address of the microservice. But what I want is to let the Gateway resolve the service ID from the Eureka server. And if I do that, it throws me a java.net.UnknownHostException: MY-MICRO-SERVICE: Temporary failure in name resoultion.


THE QUESTION:

Now why can't I use the name of the Spring application "MY-MICRO-SERVICE" (being the registered Spring Boot microservice) in the Spring Gateway (while that construction works fine when used in other microservices)? Can't a Yaml config file handle such instance names, just only IP addresses?


THE DETAILS

The gateway is mostly configured via a yaml config file. There is only one simple Java class that kicks off the gateway application. The routing is all set in the yaml config file.

The Spring Gateway application class

@SpringBootApplication
@EnableEurekaClient
public class MyGatewayApplication {

  public static void main(String[] args) {
    SpringApplication.run(MyGatewayApplication.class, args);
  }
}

The Gateway Yaml configuration file (application.yml)

spring:
  application:
    name: my-gateway
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "*"
            allowedMethods:
              - GET
              - POST
      routes:
        - id: my_route
          uri: http://MY-MICRO-SERVICE
          predicates:
            - Path=/test/**
server:
  port: 8999

info:
  app:
    properties: dev

The Error

java.net.UnknownHostException: MY-MICRO-SERVICE: Temporary failure in name resolution
    at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
    at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929) ~[na:na]
    at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515) ~[na:na]
    at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298) ~[na:na]
    at java.base/java.net.InetAddress.getByName(InetAddress.java:1248) ~[na:na]
    at io.netty.util.internal.SocketUtils$8.run(SocketUtils.java:146) ~[netty-common-4.1.36.Final.jar:4.1.36.Final]
...

Solution

  • Issue has been fixed.

    I changed the "http" to "lb" protocol and that fixed my issue. To my understanding, "lb" stands for LoadBalancing. I have no loadbalancer active on my local machine, but anyway: this works.

       - POST
          routes:
            - id: my_route
              uri: lb://MY-MICRO-SERVICE
              predicates:
                - Path=/test/**