Netflix OSS Eureka Spring boot 2.0 app on K8s returning name instead of ip address

4/28/2018

I am trying to deploy a spring boot netflix OSS discovery server on kubernetes. The server deploys fine and my apps register with the server and I even get an instance url back when I query the using the Eureka client.

@Autowired
private EurekaClient discoveryClient;

@GetMapping("/get_instance/")
public String serviceUrl() {

    return getUrlFromEureka();
}

private String getUrlFromEureka() {
    InstanceInfo instance = discoveryClient.getNextServerFromEureka("DEMO-SERVER", false);
    return instance.getHomePageUrl();
}

This returns a url like this http://demo-server-86c7cd568-xzzgt:8080/

This returns the name of the pod and the ports it is on so I try to query this to get response from the server but it times out. This is the method I use

@GetMapping("/get-from-demo-server")
public String getFromDemoServer(){
    RestTemplate restTemplate = new RestTemplate();
    return restTemplate.getForObject(getUrlFromEureka(), String.class);

}

I deploy the discovery server and the spring boot apps to the same k8's environment.

Here is my bootstrap.yml for eureka server.

Here is a screen shot of the eureka server running in k8's. screenshot

This is all running in my local minikube environment. Thanks for any help that I receive.

Here is the dependencies for my client

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.logstash.logback/logstash-logback-encoder -->
    <dependency>
        <groupId>net.logstash.logback</groupId>
        <artifactId>logstash-logback-encoder</artifactId>
        <version>5.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Here are the dependencies for the Eureka server

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
-- jjenksy
kubernetes
netflix-eureka
spring-boot

1 Answer

4/29/2018

The issue is by default the client prefers the hostname and not the ip. You can override that with a spring property. eureka.instance.prefer-ip-address= true. This worked perfect for my use case.

-- jjenksy
Source: StackOverflow