配置Bean
@Configuration
public class RestTemplateConfig{
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
服务调用
@RestController
public class RestTemplateController {
@Autowired
RestTemplate restTemplate;
/**
* 如果服务是注册到注册中心的,那么两种url访问方式都可以。
* 1:http://应用程序名称/hello
* 2:http://ip:port/hello
*
*/
@GetMapping("/hello")
public String hello() {
return "[RestTemplate]" + restTemplate.getForObject("http://应用程序名称/hello", String.class);
}
}