开发者社区> 问答> 正文

http接口调用超时,怎么解决呢?

omcat8、springmvc、httpclient4.5
有A、B、C三个应用
C负责操作数据库,提供接口,返回JSON
B负责处理业务,提供接口,返回JSON,并调用C的接口,保存/读取数据
A负责前台,调用B的接口
正常使用没什么问题,但碰到并发的时候,出现僵死状态,感觉接口处理不过来,然后超时。
大并发时,页面一直转,就是加载不出来,然后后台sql也一直在执行,要等待所有的操作执行完了,C的接口才能正常加载。而此时B和A就会访问超时了。
请问有什么破解方案没?
tomcat 使用nio模式启动了。并且有两个C,通过nginx来负载。

httpclient调用代码大致如下:

public static String post(String url, Map<String, Object> param)
            throws IOException {
        String responseBody = null;
        RequestConfig config = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).setConnectionRequestTimeout(20000).setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> nameValuePairs = new LinkedList<>();
        for (String key : param.keySet()) {
            Object obj = param.get(key);
            if (obj == null) {
                obj = new String("");
            }
            nameValuePairs.add(new BasicNameValuePair(key, obj.toString()));
        }
        CloseableHttpResponse httpResponse = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            httpResponse = httpClient.execute(httpPost);
            StatusLine line = httpResponse.getStatusLine();
            if(line.getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                responseBody = EntityUtils.toString(httpEntity);
                EntityUtils.consume(httpEntity);
            }
        } catch (UnsupportedEncodingException e) {
            throw e;
        } catch (ClientProtocolException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } finally {
            if (httpResponse != null) {
                httpResponse.close();
            }
            httpClient.close();
        }
        return responseBody;
    }

请问在服务器,或者http调用上面,有什么可优化方案?

展开
收起
小旋风柴进 2016-03-04 11:41:44 18310 0
1 条回答
写回答
取消 提交回答
  • 问题看来是c的性能不足,阻塞了,如果io的性能提升到了瓶颈,建议把c的读写分离,写入采用后台队列方式,并处理好数据同步的问题

    2019-07-17 18:52:02
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里巴巴HTTP 2.0实践及无线通信协议的演进之路 立即下载
CDN助力企业网站进入HTTPS时代 立即下载
低代码开发师(初级)实战教程 立即下载