背景
- 系統(tǒng): SpringBoot開發(fā)的Web應(yīng)用厕九;
- ORM: JPA(Hibernate)
- 接口功能簡(jiǎn)述: 根據(jù)實(shí)體類ID到數(shù)據(jù)庫(kù)中查詢實(shí)體信息欲虚,然后使用RestTemplate調(diào)用外部系統(tǒng)接口獲取數(shù)據(jù)。
問題現(xiàn)象
- 瀏覽器頁(yè)面有時(shí)報(bào)504 GateWay Timeout錯(cuò)誤澈魄,刷新多次后理疙,則總是timeout
- 數(shù)據(jù)庫(kù)連接池報(bào)連接耗盡異常
- 調(diào)用外部系統(tǒng)時(shí)有時(shí)報(bào)502 Bad GateWay錯(cuò)誤
分析過程
為便于描述將本系統(tǒng)稱為A齿梁,外部系統(tǒng)稱為B。
這三個(gè)問題環(huán)環(huán)相扣叠纷,導(dǎo)火索是第3個(gè)問題刻帚,然后導(dǎo)致第2個(gè)問題,最后導(dǎo)致出現(xiàn)第3個(gè)問題涩嚣;
原因簡(jiǎn)述: 第3個(gè)問題是由于Nginx負(fù)載下沒有掛系統(tǒng)B崇众,導(dǎo)致本系統(tǒng)在請(qǐng)求外部系統(tǒng)時(shí)報(bào)502錯(cuò)誤掂僵,而A沒有正確處理異常,導(dǎo)致http請(qǐng)求無(wú)法正常關(guān)閉顷歌,而springboot默認(rèn)打開openInView, 導(dǎo)致調(diào)用A的請(qǐng)求關(guān)閉時(shí)才會(huì)關(guān)閉數(shù)據(jù)庫(kù)連接看峻。
這里主要分析第1個(gè)問題:為什么請(qǐng)求A的連接出現(xiàn)504 Timeout.
AbstractConnPool
通過日志看到A在調(diào)用B時(shí)出現(xiàn)阻塞,直到timeout衙吩,打印出線程堆棧查看:
可以看到線程阻塞在AbstractConnPool類getPoolEntryBlocking方法中互妓。
private E getPoolEntryBlocking(
final T route, final Object state,
final long timeout, final TimeUnit timeUnit,
final Future<E> future) throws IOException, InterruptedException, TimeoutException {
Date deadline = null;
if (timeout > 0) {
deadline = new Date (System.currentTimeMillis() + timeUnit.toMillis(timeout));
}
this.lock.lock();
try {
//根據(jù)route獲取route對(duì)應(yīng)的連接池
final RouteSpecificPool<T, C, E> pool = getPool(route);
E entry;
for (;;) {
Asserts.check(!this.isShutDown, "Connection pool shut down");
for (;;) {
//獲取可用的連接
entry = pool.getFree(state);
if (entry == null) {
break;
}
// 判斷連接是否過期,如過期則關(guān)閉并從可用連接集合中刪除
if (entry.isExpired(System.currentTimeMillis())) {
entry.close();
}
if (entry.isClosed()) {
this.available.remove(entry);
pool.free(entry, false);
} else {
break;
}
}
// 如果從連接池中獲取到可用連接,更新可用連接和待釋放連接集合
if (entry != null) {
this.available.remove(entry);
this.leased.add(entry);
onReuse(entry);
return entry;
}
// 如果沒有可用連接坤塞,則創(chuàng)建新連接
final int maxPerRoute = getMax(route);
// 創(chuàng)建新連接之前冯勉,檢查是否超過每個(gè)route連接池大小,如果超過摹芙,則刪除可用連接集合相應(yīng)數(shù)量的連接(從總的可用連接集合和每個(gè)route的可用連接集合中刪除)
final int excess = Math.max(0, pool.getAllocatedCount() + 1 - maxPerRoute);
if (excess > 0) {
for (int i = 0; i < excess; i++) {
final E lastUsed = pool.getLastUsed();
if (lastUsed == null) {
break;
}
lastUsed.close();
this.available.remove(lastUsed);
pool.remove(lastUsed);
}
}
if (pool.getAllocatedCount() < maxPerRoute) {
//比較總的可用連接數(shù)量與總的可用連接集合大小灼狰,釋放多余的連接資源
final int totalUsed = this.leased.size();
final int freeCapacity = Math.max(this.maxTotal - totalUsed, 0);
if (freeCapacity > 0) {
final int totalAvailable = this.available.size();
if (totalAvailable > freeCapacity - 1) {
if (!this.available.isEmpty()) {
final E lastUsed = this.available.removeLast();
lastUsed.close();
final RouteSpecificPool<T, C, E> otherpool = getPool(lastUsed.getRoute());
otherpool.remove(lastUsed);
}
}
// 真正創(chuàng)建連接的地方
final C conn = this.connFactory.create(route);
entry = pool.add(conn);
this.leased.add(entry);
return entry;
}
}
//如果已經(jīng)超過了每個(gè)route的連接池大小,則加入隊(duì)列等待有可用連接時(shí)被喚醒或直到某個(gè)終止時(shí)間
boolean success = false;
try {
if (future.isCancelled()) {
throw new InterruptedException("Operation interrupted");
}
pool.queue(future);
this.pending.add(future);
if (deadline != null) {
success = this.condition.awaitUntil(deadline);
} else {
this.condition.await();
success = true;
}
if (future.isCancelled()) {
throw new InterruptedException("Operation interrupted");
}
} finally {
//如果到了終止時(shí)間或有被喚醒時(shí)浮禾,加出隊(duì)列交胚,加入下次循環(huán)
pool.unqueue(future);
this.pending.remove(future);
}
// 處理異常喚醒和超時(shí)情況
if (!success && (deadline != null && deadline.getTime() <= System.currentTimeMillis())) {
break;
}
}
throw new TimeoutException("Timeout waiting for connection");
} finally {
this.lock.unlock();
}
}
從上面代碼中可以看出,getPoolEntryBlocking方法用于獲取連接盈电,主要有三步:
- 檢查可用連接集合中是否有可重復(fù)使用的連接蝴簇,如果有則獲取連接,返回
- 創(chuàng)建新連接匆帚,注意同時(shí)需要檢查可用連接集合(分為每個(gè)route的和全局的)是否有多余的連接資源熬词,如果有,則需要釋放吸重。
- 加入隊(duì)列等待
從線程堆椈ナ埃可以看出,第1個(gè)問題是由于走到了第3步嚎幸。開始時(shí)是有時(shí)會(huì)報(bào)504異常颜矿,刷新多次后會(huì)一直報(bào)504異常,經(jīng)過跟蹤調(diào)試發(fā)現(xiàn)前幾次會(huì)成功獲取到連接嫉晶,而連接池滿后骑疆,后面的請(qǐng)求會(huì)阻塞。正常情況下當(dāng)前面的連接釋放到連接池后车遂,后面的請(qǐng)求會(huì)得到連接資源繼續(xù)執(zhí)行封断,可現(xiàn)實(shí)是后面的連接一直處于等待狀態(tài)斯辰,猜想可能是由于連接一直未釋放導(dǎo)致舶担。
我們來(lái)看一下連接在什么時(shí)候會(huì)釋放。
RestTemplate
由于在調(diào)外部系統(tǒng)B時(shí)彬呻,使用的是RestTemplate的getForObject方法衣陶,從此入手跟蹤調(diào)試看一看柄瑰。
@Override
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}
@Override
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}
@Override
public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor);
}
getForObject都調(diào)用了execute方法(其實(shí)RestTemplate的其它http請(qǐng)求方法調(diào)用的也是execute方法)
@Override
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException {
URI expanded = getUriTemplateHandler().expand(url, uriVariables);
return doExecute(expanded, method, requestCallback, responseExtractor);
}
@Override
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Map<String, ?> uriVariables) throws RestClientException {
URI expanded = getUriTemplateHandler().expand(url, uriVariables);
return doExecute(expanded, method, requestCallback, responseExtractor);
}
@Override
public <T> T execute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {
return doExecute(url, method, requestCallback, responseExtractor);
}
所有execute方法都調(diào)用了同一個(gè)doExecute方法
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
ClientHttpResponse response = null;
try {
ClientHttpRequest request = createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
handleResponse(url, method, response);
if (responseExtractor != null) {
return responseExtractor.extractData(response);
}
else {
return null;
}
}
catch (IOException ex) {
String resource = url.toString();
String query = url.getRawQuery();
resource = (query != null ? resource.substring(0, resource.indexOf('?')) : resource);
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + resource + "\": " + ex.getMessage(), ex);
}
finally {
if (response != null) {
response.close();
}
}
}
doExecute方法創(chuàng)建了請(qǐng)求,然后執(zhí)行剪况,處理異常教沾,最后關(guān)閉∫攵希可以看到關(guān)閉操作放在finally中授翻,任何情況都會(huì)執(zhí)行到,除非返回的response為null孙咪。
InterceptingClientHttpRequest
進(jìn)入到request.execute()方法中堪唐,對(duì)應(yīng)抽象類org.springframework.http.client.AbstractClientHttpRequest的execute方法
@Override
public final ClientHttpResponse execute() throws IOException {
assertNotExecuted();
ClientHttpResponse result = executeInternal(this.headers);
this.executed = true;
return result;
}
executeInternal方法是一個(gè)抽象方法,由子類實(shí)現(xiàn)(restTemplate內(nèi)部的http調(diào)用實(shí)現(xiàn)方式有多種)翎蹈。進(jìn)入executeInternal方法淮菠,到達(dá)抽象類org.springframework.http.client.AbstractBufferingClientHttpRequest中
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
byte[] bytes = this.bufferedOutput.toByteArray();
if (headers.getContentLength() < 0) {
headers.setContentLength(bytes.length);
}
ClientHttpResponse result = executeInternal(headers, bytes);
this.bufferedOutput = null;
return result;
}
此抽象類在AbstractClientHttpRequest基礎(chǔ)之上添加了緩沖功能,可以保存要發(fā)送給服務(wù)器的數(shù)據(jù),然后一塊發(fā)送荤堪『狭辏看這一句:
ClientHttpResponse result = executeInternal(headers, bytes);
也是一個(gè)executeInternal方法,不過參數(shù)不同澄阳,它也是一個(gè)抽象方法拥知。進(jìn)入方法,到達(dá)org.springframework.http.client.InterceptingClientHttpRequest類
protected final ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
InterceptingRequestExecution requestExecution = new InterceptingRequestExecution();
return requestExecution.execute(this, bufferedOutput);
}
實(shí)例化了一個(gè)帶攔截器的請(qǐng)求執(zhí)行對(duì)象InterceptingRequestExecution碎赢,進(jìn)入看一看举庶。
public ClientHttpResponse execute(HttpRequest request, final byte[] body) throws IOException {
// 如果有攔截器,則執(zhí)行攔截器并返回結(jié)果
if (this.iterator.hasNext()) {
ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
return nextInterceptor.intercept(request, body, this);
}
else {
// 如果沒有攔截器揩抡,則通過requestFactory創(chuàng)建request對(duì)象并執(zhí)行
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), request.getMethod());
for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
List<String> values = entry.getValue();
for (String value : values) {
delegate.getHeaders().add(entry.getKey(), value);
}
}
if (body.length > 0) {
if (delegate instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate;
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
@Override
public void writeTo(final OutputStream outputStream) throws IOException {
StreamUtils.copy(body, outputStream);
}
});
}
else {
StreamUtils.copy(body, delegate.getBody());
}
}
return delegate.execute();
}
}
看一下RestTemplate的配置:
RestTemplateBuilder builder = new RestTemplateBuilder();
return builder
.setConnectTimeout(customConfig.getRest().getConnectTimeOut())
.setReadTimeout(customConfig.getRest().getReadTimeout())
.interceptors(restTemplateLogInterceptor)
.errorHandler(new ThrowErrorHandler())
.build();
}
可以看到配置了連接超時(shí)户侥,讀超時(shí),攔截器峦嗤,和錯(cuò)誤處理器蕊唐。
看一下攔截器的實(shí)現(xiàn):
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
// 打印訪問前日志
ClientHttpResponse execute = clientHttpRequestExecution.execute(httpRequest, bytes);
if (如果返回碼不是200) {
// 拋出自定義運(yùn)行時(shí)異常
}
// 打印訪問后日志
return execute;
}
可以看到當(dāng)返回碼不是200時(shí),拋出異常烁设。還記得RestTemplate中的doExecute方法吧,此處如果拋出異常替梨,雖然會(huì)執(zhí)行doExecute方法中的finally代碼,但由于返回的response為null(其實(shí)是有response的)装黑,沒有關(guān)閉response副瀑,所以這里不能拋出異常,如果確實(shí)想拋出異常恋谭,可以在錯(cuò)誤處理器errorHandler中拋出糠睡,這樣確保response能正常返回和關(guān)閉。
RestTemplate源碼部分解析
何時(shí)如何決定使用哪一個(gè)底層http框架
知道了原因疚颊,我們?cè)賮?lái)看一下RestTemplate在什么時(shí)候決定使用什么http框架狈孔。其實(shí)在通過RestTemplateBuilder實(shí)例化RestTemplate對(duì)象時(shí)就決定了信认。
看一下RestTemplateBuilder的build方法
public RestTemplate build() {
return build(RestTemplate.class);
}
public <T extends RestTemplate> T build(Class<T> restTemplateClass) {
return configure(BeanUtils.instantiate(restTemplateClass));
}
可以看到在實(shí)例化RestTemplate對(duì)象之后,進(jìn)行配置均抽。
public <T extends RestTemplate> T configure(T restTemplate) {
// 配置requestFactory
configureRequestFactory(restTemplate);
// 配置消息轉(zhuǎn)換器
if (!CollectionUtils.isEmpty(this.messageConverters)) {
restTemplate.setMessageConverters(
new ArrayList<HttpMessageConverter<?>>(this.messageConverters));
}
//配置uri模板處理器
if (this.uriTemplateHandler != null) {
restTemplate.setUriTemplateHandler(this.uriTemplateHandler);
}
//配置錯(cuò)誤處理器
if (this.errorHandler != null) {
restTemplate.setErrorHandler(this.errorHandler);
}
// 設(shè)置根路徑(一般為'/')
if (this.rootUri != null) {
RootUriTemplateHandler.addTo(restTemplate, this.rootUri);
}
// 配置登錄驗(yàn)證
if (this.basicAuthorization != null) {
restTemplate.getInterceptors().add(this.basicAuthorization);
}
//配置自定義restTemplate器
if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) {
for (RestTemplateCustomizer customizer : this.restTemplateCustomizers) {
customizer.customize(restTemplate);
}
}
//配置攔截器
restTemplate.getInterceptors().addAll(this.interceptors);
return restTemplate;
}
看一下方法的第一行嫁赏,配置requestFactory。
private void configureRequestFactory(RestTemplate restTemplate) {
ClientHttpRequestFactory requestFactory = null;
if (this.requestFactory != null) {
requestFactory = this.requestFactory;
}
else if (this.detectRequestFactory) {
requestFactory = detectRequestFactory();
}
if (requestFactory != null) {
ClientHttpRequestFactory unwrappedRequestFactory = unwrapRequestFactoryIfNecessary(
requestFactory);
for (RequestFactoryCustomizer customizer : this.requestFactoryCustomizers) {
customizer.customize(unwrappedRequestFactory);
}
restTemplate.setRequestFactory(requestFactory);
}
}
可以指定requestFactory油挥,也可以自動(dòng)探測(cè)潦蝇。看一下detectRequestFactory方法深寥。
private ClientHttpRequestFactory detectRequestFactory() {
for (Map.Entry<String, String> candidate : REQUEST_FACTORY_CANDIDATES
.entrySet()) {
ClassLoader classLoader = getClass().getClassLoader();
if (ClassUtils.isPresent(candidate.getKey(), classLoader)) {
Class<?> factoryClass = ClassUtils.resolveClassName(candidate.getValue(),
classLoader);
ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory) BeanUtils
.instantiate(factoryClass);
initializeIfNecessary(requestFactory);
return requestFactory;
}
}
return new SimpleClientHttpRequestFactory();
}
循環(huán)REQUEST_FACTORY_CANDIDATES集合护蝶,檢查classpath類路徑中是否存在相應(yīng)的jar包,如果存在翩迈,則創(chuàng)建相應(yīng)框架的封裝類對(duì)象持灰。如果都不存在,則返回使用JDK方式實(shí)現(xiàn)的RequestFactory對(duì)象负饲。
看一下REQUEST_FACTORY_CANDIDATES集合
private static final Map<String, String> REQUEST_FACTORY_CANDIDATES;
static {
Map<String, String> candidates = new LinkedHashMap<String, String>();
candidates.put("org.apache.http.client.HttpClient",
"org.springframework.http.client.HttpComponentsClientHttpRequestFactory");
candidates.put("okhttp3.OkHttpClient",
"org.springframework.http.client.OkHttp3ClientHttpRequestFactory");
candidates.put("com.squareup.okhttp.OkHttpClient",
"org.springframework.http.client.OkHttpClientHttpRequestFactory");
candidates.put("io.netty.channel.EventLoopGroup",
"org.springframework.http.client.Netty4ClientHttpRequestFactory");
REQUEST_FACTORY_CANDIDATES = Collections.unmodifiableMap(candidates);
}
可以看到共有四種Http調(diào)用實(shí)現(xiàn)方式堤魁,在配置RestTemplate時(shí)可指定,并在類路徑中提供相應(yīng)的實(shí)現(xiàn)jar包返十。
Request攔截器的設(shè)計(jì)
再看一下InterceptingRequestExecution類的execute方法妥泉。
public ClientHttpResponse execute(HttpRequest request, final byte[] body) throws IOException {
// 如果有攔截器,則執(zhí)行攔截器并返回結(jié)果
if (this.iterator.hasNext()) {
ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
return nextInterceptor.intercept(request, body, this);
}
else {
// 如果沒有攔截器洞坑,則通過requestFactory創(chuàng)建request對(duì)象并執(zhí)行
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), request.getMethod());
for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
List<String> values = entry.getValue();
for (String value : values) {
delegate.getHeaders().add(entry.getKey(), value);
}
}
if (body.length > 0) {
if (delegate instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate;
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
@Override
public void writeTo(final OutputStream outputStream) throws IOException {
StreamUtils.copy(body, outputStream);
}
});
}
else {
StreamUtils.copy(body, delegate.getBody());
}
}
return delegate.execute();
}
}
大家可能會(huì)有疑問盲链,傳入的對(duì)象已經(jīng)是request對(duì)象了,為什么在沒有攔截器時(shí)還要再創(chuàng)建一遍request對(duì)象呢迟杂?
其實(shí)傳入的request對(duì)象在有攔截器的時(shí)候是InterceptingClientHttpRequest對(duì)象刽沾,沒有攔截器時(shí),則直接是包裝了各個(gè)http調(diào)用實(shí)現(xiàn)框的Request排拷。如HttpComponentsClientHttpRequest侧漓、OkHttp3ClientHttpRequest等。當(dāng)有攔截器時(shí)监氢,會(huì)執(zhí)行攔截器布蔗,攔截器可以有多個(gè),而這里this.iterator.hasNext()不是一個(gè)循環(huán)浪腐,為什么呢纵揍?秘密在于攔截器的intercept方法。
ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException;
此方法包含request,body,execution议街。exection類型為ClientHttpRequestExecution接口泽谨,上面的InterceptingRequestExecution便實(shí)現(xiàn)了此接口,這樣在調(diào)用攔截器時(shí),傳入exection對(duì)象本身隔盛,然后再調(diào)一次execute方法,再判斷是否仍有攔截器拾稳,如果有吮炕,再執(zhí)行下一個(gè)攔截器,將所有攔截器執(zhí)行完后访得,再生成真正的request對(duì)象龙亲,執(zhí)行http調(diào)用。
那如果沒有攔截器呢悍抑?
上面已經(jīng)知道RestTemplate在實(shí)例化時(shí)會(huì)實(shí)例化RequestFactory鳄炉,當(dāng)發(fā)起http請(qǐng)求時(shí),會(huì)執(zhí)行restTemplate的doExecute方法搜骡,此方法中會(huì)創(chuàng)建Request拂盯,而createRequest方法中,首先會(huì)獲取RequestFactory
// org.springframework.http.client.support.HttpAccessor
protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
ClientHttpRequest request = getRequestFactory().createRequest(url, method);
if (logger.isDebugEnabled()) {
logger.debug("Created " + method.name() + " request for \"" + url + "\"");
}
return request;
}
// org.springframework.http.client.support.InterceptingHttpAccessor
public ClientHttpRequestFactory getRequestFactory() {
ClientHttpRequestFactory delegate = super.getRequestFactory();
if (!CollectionUtils.isEmpty(getInterceptors())) {
return new InterceptingClientHttpRequestFactory(delegate, getInterceptors());
}
else {
return delegate;
}
}
看一下RestTemplate與這兩個(gè)類的關(guān)系就知道調(diào)用關(guān)系了记靡。
而在獲取到RequestFactory之后谈竿,判斷有沒有攔截器,如果有摸吠,則創(chuàng)建InterceptingClientHttpRequestFactory對(duì)象空凸,而此RequestFactory在createRequest時(shí),會(huì)創(chuàng)建InterceptingClientHttpRequest對(duì)象,這樣就可以先執(zhí)行攔截器寸痢,最后執(zhí)行創(chuàng)建真正的Request對(duì)象執(zhí)行http調(diào)用呀洲。
獲取http連接邏輯流程圖
以HttpComponents為底層Http調(diào)用實(shí)現(xiàn)的邏輯流程圖。
流程圖說明:
- RestTemplate可以根據(jù)配置來(lái)實(shí)例化對(duì)應(yīng)的RequestFactory啼止,包括apache httpComponents道逗、OkHttp3、Netty等實(shí)現(xiàn)献烦。
- RestTemplate與HttpComponents銜接的類是HttpClient,此類是apache httpComponents提供給用戶使用憔辫,執(zhí)行http調(diào)用。HttpClient是創(chuàng)建RequestFactory對(duì)象時(shí)通過HttpClientBuilder實(shí)例化的仿荆,在實(shí)例化HttpClient對(duì)象時(shí)贰您,實(shí)例化了HttpClientConnectionManager和多個(gè)ClientExecChain,HttpRequestExecutor拢操、HttpProcessor以及一些策略锦亦。
- 當(dāng)發(fā)起請(qǐng)求時(shí),由requestFactory實(shí)例化httpRequest,然后依次執(zhí)行ClientexecChain令境,常用的有四種:
- RedirectExec: 請(qǐng)求跳轉(zhuǎn);根據(jù)上次響應(yīng)結(jié)果和跳轉(zhuǎn)策略決定下次跳轉(zhuǎn)的地址杠园,默認(rèn)最大執(zhí)行50次跳轉(zhuǎn);
- RetryExec:決定出現(xiàn)I/O錯(cuò)誤的請(qǐng)求是否再次執(zhí)行
- ProtocolExec: 填充必要的http請(qǐng)求header舔庶,處理http響應(yīng)header抛蚁,更新會(huì)話狀態(tài)
- MainClientExec:請(qǐng)求執(zhí)行鏈中最后一個(gè)節(jié)點(diǎn)陈醒;從連接池CPool中獲取連接,執(zhí)行請(qǐng)求調(diào)用瞧甩,并返回請(qǐng)求結(jié)果钉跷;
- PoolingHttpClientConnectionManager用于管理連接池,包括連接池初始化肚逸,獲取連接爷辙,獲取連接,打開連接朦促,釋放連接膝晾,關(guān)閉連接池等操作。
- CPool代表連接池务冕,但連接并不保存在CPool中血当;CPool中維護(hù)著三個(gè)連接狀態(tài)集合:leased(租用的,即待釋放的)/available(可用的)/pending(等待的)禀忆,用于記錄所有連接的狀態(tài)歹颓;并且維護(hù)著每個(gè)Route對(duì)應(yīng)的連接池RouteSpecificPool;
- RouteSpecificPool是連接真正存放的地方油湖,內(nèi)部同樣也維護(hù)著三個(gè)連接狀態(tài)集合巍扛,但只記錄屬于本route的連接。
HttpComponents將連接按照route劃分連接池乏德,有利于資源隔離撤奸,使每個(gè)route請(qǐng)求相互不影響;
總結(jié)
- 在使用框架時(shí)喊括,特別是在增強(qiáng)其功能胧瓜,自定義行為時(shí),要考慮到自定義行為對(duì)框架原有流程邏輯的影響郑什,并且最好要熟悉框架相應(yīng)功能的設(shè)計(jì)意圖府喳。
- 在與外部事物交互,包括網(wǎng)絡(luò)蘑拯,磁盤钝满,數(shù)據(jù)庫(kù)等,做到異常情況的處理申窘。