Why I can not use virtual hostname to get my micro service?

197 Views Asked by At

I have a provider in my eureka, like this:

enter image description here

Then I use restTemplate to get the the message, like this:

  @Autowired
RestTemplate restTemplate;

@RequestMapping("getInfo")
public String getMessage(){
    return restTemplate.getForObject("http://microservice_provider/user/getUserInfo", String.class);
}

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

}

}

But it never does works. It tells me:

enter image description here

So, what can I do to use like this successfully?

2

There are 2 best solutions below

3
On

Create a RestTemplate bean and annotate you bean with @LoadBalanced.

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

That way, you can use the name of the microservice microservice_provider as part of the url.

String remotemsg = restTemplate.getForObject("http://microservice_provider/user/getInfo",String.class);
0
On

In the url "http://microservice_provider" the hostname microservice_provider is not a valid hostname since underscore _ isn't valid in hostnames. Change the name and it will work. There has been a recent change to fix the NPE with a clear error message.