使用resttemplate+ribbon取其他微服务的数时,如果调用resttemplate过于频繁,报如下错误,
I/O error on GET request for "http://SCENE/get_features": SCENE; nested exception is java.net.UnknownHostException: SCENE
相关代码如下:
@Configuration
public class RestConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@RestController
public class outward_contact{
@Autowired
private RestTemplate restTemplate;
public outward_contact(){
Thread t=new Thread(new Request_Thread());
/* try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
t.start();
}
@RequestMapping("/hi")
public Vector<Feature> Get_Features() {
Vector<Feature> features = null;
//features=restTemplate.getForObject("http://localhost:9001/get_features", Vector.class);
if(restTemplate!=null){
System.out.println("restTemplate is not null");
features=restTemplate.getForObject("http://SCENE/get_features", Vector.class);
//features=restTemplate.getForEntity("http://SCENE/get_features", Vector.class).getBody();
System.out.println("features: "+features);
}
return features;
}
class Request_Thread implements Runnable{
@Override
public void run() {
while(true){
Get_Features();
try {
TimeUnit.MILLISECONDS.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
如果TimeUnit.MILLISECONDS.sleep(300);的时间改为2000或以上,程序就不会报错,能够正常取数并计算。
是ribbon或resttemplate设置错了吗?求指教。
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。