httpclient请求接口超时问题

最近线上出现一个问题,外部请求过来后一直没有响应给调用方,看日志没有报错,可以复现。
想到的就可能是五个原因:

  1. 日志文件过大导致磁盘空间满了,导致正常的业务日志无法写入,但是重启后发现日志能正常写入,排除这个问题
  2. 系统对接很多外部数据源,可能哪个数据源响应延迟导致没有返回导致系统服务一直等待
  3. 数据库连接池无可用连接,请求一直在等待可用数据库连接导致外部数据请求一直没有返回
  4. 代码哪里有问题导致死锁,两个线程相互等待导致系统响应异常
  5. 服务器内存溢出导致请求无返回

排查思路由易到难:
针对可能的原因2,项目重启后看日志发现数据库有返回,但是仍然无响应,排除数据库连接问题。
想到如果是原因4或者5的话,日志里面应该有死锁堆栈信息或者内存溢出的异常信息,又这种问题不易排查解决,暂时搁置,看看原因2
针对外部请求的httputil工具类如下:

public class HttpUtil {
	
	 private static Logger logger= LoggerFactory.getLogger(HttpUtil.class);
	 
	public static String postJson(String url, String body) throws Exception {
		return post(url, body, "application/json");
	}

	public static String postForm(String url, String body) throws Exception {
		return post(url, body, "application/x-www-form-urlencoded");
	}

	public static String post(String url, String body, String contentType) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader("Content-Type", contentType);
		httpPost.setEntity(new StringEntity(body, Charset.forName("UTF-8")));

		CloseableHttpResponse response = httpClient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String responseContent = EntityUtils.toString(entity, "UTF-8");

		response.close();
		httpClient.close();
		return responseContent;
	}
}

乍一看不觉得有啥问题,很正常的http请求,突然想到那么针对http请求的超时时间,处理时间之类的配置是在哪里设置的呢?这个默认配置是什么呢?
跟踪代码
在这里插入图片描述
这边用了设计模式的建造者模式,这个方法体很长,就不全贴出来了,具体可自己debug查看,这里贴出关键内容
在这里插入图片描述

 public CloseableHttpClient build() {
        PublicSuffixMatcher publicSuffixMatcherCopy = this.publicSuffixMatcher;
        if (publicSuffixMatcherCopy == null) {
            publicSuffixMatcherCopy = PublicSuffixMatcherLoader.getDefault();
        }
        //中间逻辑=========
   		return new InternalHttpClient((ClientExecChain)execChain, (HttpClientConnectionManager)connManagerCopy, (HttpRoutePlanner)routePlannerCopy, cookieSpecRegistryCopy, (Lookup)authSchemeRegistryCopy, (CookieStore)defaultCookieStore, (CredentialsProvider)defaultCredentialsProvider, this.defaultRequestConfig != null ? this.defaultRequestConfig : RequestConfig.DEFAULT, closeablesCopy);
    }

方法最后返回http请求client的时候,会做一个判断有没有设置自定义的请求配置requestconfig,如果设置了用自定义的,如果没设置,用工具类默认的,那么默认的配置到底是什么呢?
在这里插入图片描述
在这里插入图片描述
问题到现在基本很明了了,这种client的默认配置如果请求一直没响应会一直等待,外部请求过来我们请求其他数据源外部数据,但因为其他数据源异常没有及时返回数据给我们导致整个服务卡死,知道原因后解决方法就比较简单了。修改后的http工具类如下:

public class HttpUtil {
	
	 private static Logger logger= LoggerFactory.getLogger(HttpUtil.class);
	 
	public static String postJson(String url, String body) throws Exception {
		return post(url, body, "application/json");
	}

	public static String postForm(String url, String body) throws Exception {
		return post(url, body, "application/x-www-form-urlencoded");
	}

	public static String post(String url, String body, String contentType) throws Exception {
		RequestConfig defaultRequestConfig = RequestConfig.custom()
				.setConnectTimeout(5000) //连接时间
				.setSocketTimeout(5000) //请求处理时间
				.setConnectionRequestTimeout(3000) //从连接池获取连接超时时间
				.build();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader("Content-Type", contentType);
		httpPost.setEntity(new StringEntity(body, Charset.forName("UTF-8")));

		CloseableHttpResponse response = httpClient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String responseContent = EntityUtils.toString(entity, "UTF-8");

		response.close();
		httpClient.close();
		return responseContent;
	}
}

注:关于超时时间的参数具体设置值需要根据自己的业务需求自定义修改,不必照搬
至此,问题解决,针对这个问题有如下反思:
1、每次用一个不熟悉的技术,需要熟悉这个技术相关api,不能单纯会用就好,需要考虑到后续的扩展,稳定性等因素
2、排查问题有优先级,先排除容易排除的因素,然后再针对性排查

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章