简体   繁体   中英

Resttemplate with Spring boot and eureka

I create one project with spring-boot and a eureka client( I have 2 others project register in my eureka server) , so to access that other 2 projects I create in my Application a RestTemplate just like this:

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

So, now I when I try to access one of my project I only put in url the " http://my-project1/ " or " http://my-project2/ " and the eureka server resolve that url for me.

But now I need to call a rest from outside my scope (for sample a postal code service) so If i put :

return restTemplate.getForObject("https://viacep.com.br/ws/11380120/json", String.class);

don`t works, because the viacep.com are not in my scope, it's possible to fix this?

In this case you will have to create another RestTemplate which is not load balanced.

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

For calling any service which is not registered with your Eureka, use the non load balanced RestTemplate. As, with load balanced rest template, it will always try to resolve service from Eureka registry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM