一次线上http接口调用不通相关的解决过程

时间:2021-08-31 17:37:58

2016-05-25 08:58:34

昨天线上小白系统因为调用外部http接口,超时不释放,导致页面反应很慢,时间一长,报502错误。

上网查了下,502错误是因为服务对于客户的请求没有得到及时的反应,查询日志,发现很多调http接口异常,页面反应也很慢。

瞬间想到缩短http客户端调用http接口的超时时间,搜到这个博客http://blog.csdn.net/xinying0424/article/details/36006383  感谢

HttpClient 4:
连接超时: 连接超时
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);
// 或者
HttpConnectionParams.setConnectionTimeout(params,6000);
读取超时: httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,60000);
// 或者
HttpConnectionParams.setSoTimeout(params,60000);
HttpClient 3:
连接超时:
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
读取超时:
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000);

发现我本来代码里就写了这个,只是时间配置的实在太长

private static MultiThreadedHttpConnectionManager connectionManager = null;
private static int connectionTimeOut = 2000;t
private static int socketTimeOut = 10000;
private static int maxConnectionPerHost = 20;
private static int maxTotalConnections = 20;
private static HttpClient client; static {
connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.getParams().setConnectionTimeout(connectionTimeOut);
connectionManager.getParams().setSoTimeout(socketTimeOut);
connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);
connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
client = new HttpClient(connectionManager);
}

然后我把socketTimeOut调整为2000,本地想测试下,结果。。。

java.lang.IllegalArgumentException: host parameter is null  调用接口报错

查了下 http://wang371134086.iteye.com/blog/1688458  竟然调用http接口没写http://,加上这段解决

问题解决记录下,感谢一下博客。

http://blog.csdn.net/xinying0424/article/details/36006383
http://wang371134086.iteye.com/blog/1688458

http://blog.sina.com.cn/s/blog_a577563c01013tlz.html

http://jinnianshilongnian.iteye.com/blog/2089792

http://www.zhihu.com/question/21647204