Exception when try to access microservice using Eureka Service Discovery

85 views Asked by At

I was trying to access one microservice from another using Eureka Service Discovery. I started the server and able to register the clients into the server. Everything went well. But I couldn't able to access the microservice. I don't know why.

I've got "Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.web.reactive.function.client.WebClientRequestException] with root cause" exception when I was trying to access the microservice which was in Eureka server by using WebClient. Here is the code for this,

    @GetMapping("getStudentMessageByEureka")
    public String getMessageFromStudentApplicationThroughEureka()
    {
       String message =this.webClient.get()
               .uri("http://STUDENT-SERVICE/student/")
               .retrieve()
               .bodyToMono(String.class)
               .block();
       return message;
    }

The same happens for RestTemplate also "Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://student-service/student": student-service] with root cause" and here is the code for this,

@GetMapping("getStudentMessageByEureka2")
    public String getMessageFromStudentApplicationThroughEureka2()
    {
       String url = "http://student-service/student";
       String message = this.restTemplate.getForObject(url,String.class);
       return message;
    }

when I was using a static url("http://localhost:2000/student/") It works fine.Here is the code for that

    @GetMapping("getStudentMessage")
    public String getMessageFromStudentApplication(){
         String message = this.webClient.get()
                .uri("http://localhost:2000/student/")
                .retrieve()
                .bodyToMono(String.class)
                .block();
        return message;
    }

But I cant utilize the service discovery. Could somebody explain why this is happening? and what could be done to resolve this?

1

There are 1 answers

0
Ch Vamsi On

Check your code syntax.. Try this, and include varibles or any parameters you want to pass in api call, make changes accordingly in uri method.

@GetMapping("/getStudentMessageByEureka")
    public String getMessageFromStudentApplicationThroughEureka()
    {
        WebClient webClient = WebClient.create("http://localhost:8080");
        String message =webClient.get()
                .uri("STUDENT-SERVICE/student")
                .retrieve()
                .bodyToMono(String.class)
                .block();
        return message;
    }