eureka unknownHostException in service discovery


prranay

I have two microservices,

  1. eureka-client-1 running on localhost:8081
  2. eureka-client-2 running on localhost:8082

Both are DiscoveryClient registered with 'eureka-server' running on localhost:8761.

In the code snippet below, I am trying to call eureka-client-2 from eureka-client-1. Instead of calling http://localhost:8082 , I want to call http://eureka-client-2, but this throws a java.net.UnknownHostException during Eureka service discovery.

After searching, I found that I need to use "Brixton" to do it.

Is there a way to do this with Camden.SR3?

Please suggest.

@Component
public class HystrixDemoService {   

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @HystrixCommand(fallbackMethod = "getFallbackCustomerName")
    public String getCustomerName() {
        RestTemplate restTemplate = new RestTemplate();
        URI uri = URI.create("http://eureka-client-2");     // fails here
        return restTemplate.getForObject(uri, String.class);
    }

    public String getFallbackCustomerName() {
        System.out.println("coming inside fallback method");
        return "Resillient Customer";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo-pranay-eureka-client1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-pranay-eureka-client1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

client 1's application.properties is similar to client 2 (just change the name, e.g. eureka-client-2)

spring.application.name=eureka-client-1
server.port=8081
eureka:
  client:
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    statusPageUrlPath: /info
    healthCheckUrlPath: /health

application.properties for eureka server

spring.application.name=eureka-service
server.port=8761
eureka:
  client:
    registerWithEureka: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    statusPageUrlPath: /info
    healthCheckUrlPath: /health
prranay

The changes below worked for me.

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

@EnableDiscoveryClient
@EnableCircuitBreaker
@RestController
class HystrixDemoApplication {
    @Autowired
    HystrixDemoService hystrixDemoService;

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RequestMapping("/")
    public String name() {
        String str = hystrixDemoService.getCustomerName();
        return "I'm A talking to "+str;
    }

}

Below is the line of code used to select the eureka-client-2 instance...

ServiceInstance实例= loadBalancer.choose(“ eureka-client-2”);

@Component
public class HystrixDemoService {

    @Autowired
    private LoadBalancerClient loadBalancer;

    @HystrixCommand(fallbackMethod = "getFallbackCustomerName")
    public String getCustomerName() {
        RestTemplate restTemplate = new RestTemplate();
        ServiceInstance instance = loadBalancer.choose("eureka-client-2");
        URI uri = instance.getUri();
        return restTemplate.getForObject(uri, String.class);
    }

    public String getFallbackCustomerName() {
        System.out.println("coming inside fallback method");
        return "Resillient Customer";
    }
}

Related


eureka unknownHostException in service discovery

prranay I have two microservices, eureka-client-1 running on localhost:8081 eureka-client-2 running on localhost:8082 Both are DiscoveryClient registered with 'eureka-server' running on localhost:8761. In the code snippet below, I am trying to call eureka-clie

eureka unknownHostException in service discovery

prranay I have two microservices, eureka-client-1 running on localhost:8081 eureka-client-2 running on localhost:8082 Both are DiscoveryClient registered with 'eureka-server' running on localhost:8761. In the code snippet below, I am trying to call eureka-clie

java.net.UnknownHostException during Eureka service discovery

Karamabet According to this blog https://spring.io/blog/2015/07/14/microservices-with-spring Able to run the application without any issues. In this order: java -jar microservice-demo-0.0.1-SNAPSHOT.jar注册1111 java -jar microservice-demo-0.0.1-SNAPSHOT.jar 帐 户

Unable to register to discovery server service (EUREKA)

Heena Mittal: So when I run my service instance. It does not register itself with the discovery server. And there aren't any errors, so I'm not able to pinpoint the problem here. application.propertiesMy service looks like this and the application.propertiesdi

Eureka service discovery without Spring-boot

Upul Doluweera: I have written a Spring Boot microservice and a REST client. The client is part of another module and makes RESTful calls to the microservice. The microservice is registered in the Eureka registry and I want my clients (not the Spring Boot proj

Eureka service discovery without Spring-boot

Upul Doluweera: I have written a Spring Boot microservice and a REST client. The client is part of another module and makes RESTful calls to the microservice. The microservice is registered in the Eureka registry and I want my clients (not the Spring Boot proj

Disadvantages of using Eureka for Kubernetes service discovery

David context I'm deploying a set of services containerized using Docker into AWS. Regardless of the deployment solution chosen (like raw EC2/ECS/Elastic Beanstalk/Fargate), we're going to face the problem of "service discovery". To name just a few service dis

Unable to register to discovery server service (EUREKA)

Heena Mittal: So when I run my service instance. It does not register itself with the discovery server. And there aren't any errors, so I'm not able to pinpoint the problem here. application.propertiesMy service looks like this and the application.propertiesdi

Custom Service Discovery Using Netflix Eureka

Mahesh Sind Currently, I'm working on a POC in an organization. Our goal is to implement service discovery. So I'm exploring Eureka. Eureka is great for unconditional discovery services. Our requirement is to implement conditional service discovery. Our curren

Eureka service discovery without Spring-boot

Upul Doluweera I have written a Spring Boot microservice and a REST client. The client is part of another module and makes RESTful calls to the microservice. The microservice is registered in the Eureka registry and I want my clients (not the Spring Boot proje

Disadvantages of using Eureka for Kubernetes service discovery

David context I'm deploying a set of services containerized using Docker into AWS. Regardless of the deployment solution chosen (like raw EC2/ECS/Elastic Beanstalk/Fargate), we're going to face the problem of "service discovery". To name just a few service dis

Custom Service Discovery Using Netflix Eureka

Mahesh Sind Currently, I'm working on a POC in an organization. Our goal is to implement service discovery. So I'm exploring Eureka. Eureka is great for unconditional discovery services. Our requirement is to implement conditional service discovery. Our curren

Unable to register to discovery server service (EUREKA)

Heena Mittal: So when I run my service instance. It does not register itself with the discovery server. And there aren't any errors, so I'm not able to pinpoint the problem here. application.propertiesMy service looks like this and the application.propertiesdi

Unable to register to discovery server service (EUREKA)

Heena Mittal: So when I run my service instance. It does not register itself with the discovery server. And there aren't any errors, so I'm not able to pinpoint the problem here. application.propertiesMy service looks like this and the application.propertiesdi

Eureka service discovery without Spring-boot

Upul Doluweera: I have written a Spring Boot microservice and a REST client. The client is part of another module and makes RESTful calls to the microservice. The microservice is registered in the Eureka registry and I want my clients (not the Spring Boot proj

Custom Service Discovery Using Netflix Eureka

Mahesh Sind Currently, I'm working on a POC in an organization. Our goal is to implement service discovery. So I'm exploring Eureka. Eureka is great for unconditional discovery services. Our requirement is to implement conditional service discovery. Our curren

Custom Service Discovery Using Netflix Eureka

Mahesh Sind Currently, I'm working on a POC in an organization. Our goal is to implement service discovery. So I'm exploring Eureka. Eureka is great for unconditional discovery services. Our requirement is to implement conditional service discovery. Our curren

Disadvantages of using Eureka for Kubernetes service discovery

David context I'm deploying a set of services containerized using Docker into AWS. Regardless of the deployment solution chosen (like raw EC2/ECS/Elastic Beanstalk/Fargate), we're going to face the problem of "service discovery". To name just a few service dis