1.创建RestTemplateConfig
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean(name = "loadBalanced")
@LoadBalanced
RestTemplate loadBalanced() {
return new RestTemplate();
}
@Bean(name = "restTemplate")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
开启了负载均衡的可以使用在多模块开发进行请求
@Autowired
@Qualifier("loadBalanced")
private RestTemplate loadBalanced;
JSONObject forObject = loadBalanced.getForObject(Constants.HTTP + ServiceNameConstants.SYSTEM_SERVICE + "/com/getSysUserCom?comName=" + carOwner.getComName()+"&userId="+userId, JSONObject.class);
另外一个是请求外部连接
@Autowired
@Qualifier("restTemplate")
private RestTemplate restTemplate;
JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
|