amursana0302 2021-12-02 11:46 采纳率: 0%
浏览 11
已结题

resttemplate+ribbon频繁调用导致的错误

使用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设置错了吗?求指教。

  • 写回答

1条回答 默认 最新

  • 有问必答小助手 2021-12-03 21:25
    关注

    你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


    本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


    因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。

    评论

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 创建了问题 12月2日

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误