Search code examples
spring-bootspring-cloudnetflix-eureka

Spring boot/Cloud Eureka DiscoveryClient - No such host is known


I want to try out microservices using 2.5.4 spring boot and getting problems in discovery

Steps:

Created a RestTemplate with @LoadBalanced, and trying to call the service using "application name in the url"

I have the register and fetch registry true in properties (thought this should get available services?)

Error: No such host is known

I am trying

@Autowired
private DiscoveryClient discoveryClient;

and do

 discoveryClient.getInstances("myappservice-name").forEach((ServiceInstance s) -> {
            System.out.println(ToStringBuilder.reflectionToString(s));
        });

But all examples tell to use an endpoint? or commandLineRunner. Both I'm looking for auto loading

https://spring.io/guides/gs/service-registration-and-discovery/

https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-with-spring-cloud-and-netflix-s-eureka

Not ok to call the below for each app

@RequestMapping("/service-instances/{applicationName}")     public
 List<ServiceInstance> serviceInstancesByApplicationName(

How can I register automatically?

EDIT: More detailed question

(1) SERVICE App

bootstrap - spring.application.name=pluralsight-toll-service

application props

server.port=0
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

eureka.instance.instance-id=${spring.application.name}:${random.int}
eureka.instance.hostname=localhost
management.endpoints.web.exposure.include=*

App

@SpringBootApplication
@EnableEurekaClient
public class PluralsightEurekaTollrateServiceApplication {

I see app registered in eureka server

(2) Client bootstrap

spring.application.name=pluralsight-tollrate-billboard
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

application.props

server.port=8081
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
management.endpoints.web.exposure.include=*

App

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PluralsightEurekaTollrateBillboardApplication {

Controller

   @LoadBalanced
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
    
    @Autowired
    RestTemplate restTemplate;
    
    @RequestMapping("/dashboard")
    public String GetTollRate(@RequestParam int stationId, Model m) {
        
        TollRate tr = restTemplate.getForObject("http://pluralsight-toll-service/tollrate/" + stationId, TollRate.class);
    

Error: pluralsight-toll-service host is unknown

How can call service from client using the name


Solution

  • Fixed the issue

    Its all down to client project dependencies

        <!-- <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
            <version>3.0.3</version>
        </dependency> -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    

    The names are too close spring-cloud-netflix-eureka-client is wrong spring-cloud-starter-netflix-eureka-client is the right one

    Because of the wrong one, I had to add eureka-client of com.netflix.eureka. Cleaned all and it works (Also I was missing spring-cloud , as I was creating from eclipse. In future will use initializ only )