關(guān)注我旦袋,可以獲取最新知識骤菠、經(jīng)典面試題以及微服務(wù)技術(shù)分享
??在微服務(wù)中,rest
服務(wù)互相調(diào)用是很普遍的疤孕,我們該如何優(yōu)雅地調(diào)用商乎,其實在Spring框架使用RestTemplate
類可以優(yōu)雅地進行rest
服務(wù)互相調(diào)用,它簡化了與http
服務(wù)的通信方式祭阀,統(tǒng)一了RESTful
的標準鹉戚,封裝了http
鏈接,操作使用簡便专控,還可以自定義RestTemplate所需的模式抹凳。其中:
RestTemplate
默認使用HttpMessageConverter
實例將HTTP
消息轉(zhuǎn)換成POJO
或者從POJO
轉(zhuǎn)換成HTTP
消息。默認情況下會注冊主mime
類型的轉(zhuǎn)換器伦腐,但也可以通過setMessageConverters
注冊自定義轉(zhuǎn)換器赢底。RestTemplate
使用了默認的DefaultResponseErrorHandler
,對40XBad Request
或50Xinternal
異常error
等錯誤信息捕捉柏蘑。-
RestTemplate
還可以使用攔截器interceptor
幸冻,進行對請求鏈接跟蹤,以及統(tǒng)一head的設(shè)置咳焚。
其中洽损,RestTemplate
還定義了很多的REST
資源交互的方法,其中的大多數(shù)都對應(yīng)于HTTP
的方法革半,如下:
方法 | 解析 |
---|---|
delete() | 在特定的URL上對資源執(zhí)行HTTP DELETE操作 |
exchange() | 在URL上執(zhí)行特定的HTTP方法碑定,返回包含對象的ResponseEntity |
execute() | 在URL上執(zhí)行特定的HTTP方法流码,返回一個從響應(yīng)體映射得到的對象 |
getForEntity() | 發(fā)送一個HTTP GET請求,返回的ResponseEntity包含了響應(yīng)體所映射成的對象 |
getForObject() | 發(fā)送一個HTTP GET請求延刘,返回的請求體將映射為一個對象 |
postForEntity() | POST 數(shù)據(jù)到一個URL旅掂,返回包含一個對象的ResponseEntity |
postForObject() | POST 數(shù)據(jù)到一個URL,返回根據(jù)響應(yīng)體匹配形成的對象 |
headForHeaders() | 發(fā)送HTTP HEAD請求访娶,返回包含特定資源URL的HTTP頭 |
optionsForAllow() | 發(fā)送HTTP OPTIONS請求,返回對特定URL的Allow頭信息 |
postForLocation() | POST 數(shù)據(jù)到一個URL觉阅,返回新創(chuàng)建資源的URL |
put() | PUT 資源到特定的URL |
1. RestTemplate源碼
1.1 默認調(diào)用鏈路
restTemplate
進行API調(diào)用時崖疤,默認調(diào)用鏈:
###########1.使用createRequest創(chuàng)建請求########
resttemplate->execute()->doExecute()
HttpAccessor->createRequest()
//獲取攔截器Interceptor,InterceptingClientHttpRequestFactory典勇,SimpleClientHttpRequestFactory
InterceptingHttpAccessor->getRequestFactory()
//獲取默認的SimpleBufferingClientHttpRequest
SimpleClientHttpRequestFactory->createRequest()
#######2.獲取響應(yīng)response進行處理###########
AbstractClientHttpRequest->execute()->executeInternal()
AbstractBufferingClientHttpRequest->executeInternal()
###########3.異常處理#####################
resttemplate->handleResponse()
##########4.響應(yīng)消息體封裝為java對象#######
HttpMessageConverterExtractor->extractData()
1.2 restTemplate->doExecute()
在默認調(diào)用鏈中劫哼,restTemplate
進行API調(diào)用都會調(diào)用 doExecute
方法,此方法主要可以進行如下步驟:
1)使用createRequest
創(chuàng)建請求割笙,獲取響應(yīng)
2)判斷響應(yīng)是否異常权烧,處理異常
3)將響應(yīng)消息體封裝為java對象
@Nullable
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback,
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "URI is required");
Assert.notNull(method, "HttpMethod is required");
ClientHttpResponse response = null;
try {
//使用createRequest創(chuàng)建請求
ClientHttpRequest request = createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
//獲取響應(yīng)response進行處理
response = request.execute();
//異常處理
handleResponse(url, method, response);
//響應(yīng)消息體封裝為java對象
return (responseExtractor != null ? responseExtractor.extractData(response) : 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();
}
}
}
1.3 InterceptingHttpAccessor->getRequestFactory()
在默認調(diào)用鏈中,InterceptingHttpAccessor的getRequestFactory()
方法中伤溉,如果沒有設(shè)置interceptor
攔截器般码,就返回默認的SimpleClientHttpRequestFactory
,反之乱顾,返回InterceptingClientHttpRequestFactory
的requestFactory
板祝,可以通過resttemplate.setInterceptors
設(shè)置自定義攔截器interceptor
。
//Return the request factory that this accessor uses for obtaining client request handles.
public ClientHttpRequestFactory getRequestFactory() {
//獲取攔截器interceptor(自定義的)
List<ClientHttpRequestInterceptor> interceptors = getInterceptors();
if (!CollectionUtils.isEmpty(interceptors)) {
ClientHttpRequestFactory factory = this.interceptingRequestFactory;
if (factory == null) {
factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors);
this.interceptingRequestFactory = factory;
}
return factory;
}
else {
return super.getRequestFactory();
}
}
然后再調(diào)用SimpleClientHttpRequestFactory的createRequest
創(chuàng)建連接:
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpURLConnection connection = openConnection(uri.toURL(), this.proxy);
prepareConnection(connection, httpMethod.name());
if (this.bufferRequestBody) {
return new SimpleBufferingClientHttpRequest(connection, this.outputStreaming);
}
else {
return new SimpleStreamingClientHttpRequest(connection, this.chunkSize, this.outputStreaming);
}
}
1.4 resttemplate->handleResponse()
在默認調(diào)用鏈中走净,resttemplate的handleResponse
券时,響應(yīng)處理,包括異常處理伏伯,而且異常處理可以通過調(diào)用setErrorHandler
方法設(shè)置自定義的ErrorHandler
橘洞,實現(xiàn)對請求響應(yīng)異常的判別和處理。自定義的ErrorHandler
需實現(xiàn)ResponseErrorHandler
接口说搅,同時Spring boot
也提供了默認實現(xiàn)DefaultResponseErrorHandler
炸枣,因此也可以通過繼承該類來實現(xiàn)自己的ErrorHandler
。
DefaultResponseErrorHandler
默認對40X Bad Request
或50X internal
異常error
等錯誤信息捕捉蜓堕。如果想捕捉服務(wù)本身拋出的異常信息抛虏,需要通過自行實現(xiàn)RestTemplate
的ErrorHandler
。
ResponseErrorHandler errorHandler = getErrorHandler();
//判斷響應(yīng)是否有異常
boolean hasError = errorHandler.hasError(response);
if (logger.isDebugEnabled()) {
try {
int code = response.getRawStatusCode();
HttpStatus status = HttpStatus.resolve(code);
logger.debug("Response " + (status != null ? status : code));
}catch (IOException ex) {
// ignore
}
}
//有異常進行異常處理
if (hasError) {
errorHandler.handleError(url, method, response);
}
}
1.5 HttpMessageConverterExtractor->extractData()
在默認調(diào)用鏈中套才, HttpMessageConverterExtractor
的extractData
中進行響應(yīng)消息體封裝為java
對象迂猴,就需要使用message
轉(zhuǎn)換器,可以通過追加的方式增加自定義的messageConverter
:先獲取現(xiàn)有的messageConverter
背伴,再將自定義的messageConverter
添加進去沸毁。
根據(jù)restTemplate
的setMessageConverters
的源碼可得峰髓,使用追加的方式可防止原有的messageConverter
丟失,源碼:
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
//檢驗
validateConverters(messageConverters);
// Take getMessageConverters() List as-is when passed in here
if (this.messageConverters != messageConverters) {
//先清除原有的messageConverter
this.messageConverters.clear();
//后加載重新定義的messageConverter
this.messageConverters.addAll(messageConverters);
}
}
HttpMessageConverterExtractor的extractData
源碼:
MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response);
if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) {
return null;
}
//獲取到response的ContentType類型
MediaType contentType = getContentType(responseWrapper);
try {
//依次循環(huán)messageConverter進行判斷是否符合轉(zhuǎn)換條件息尺,進行轉(zhuǎn)換java對象
for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
//會根據(jù)設(shè)置的返回類型responseType和contentType參數(shù)進行匹配携兵,選擇合適的MessageConverter
if (messageConverter instanceof GenericHttpMessageConverter) {
GenericHttpMessageConverter<?> genericMessageConverter =
(GenericHttpMessageConverter<?>) messageConverter;
if (genericMessageConverter.canRead(this.responseType, null, contentType)) {
if (logger.isDebugEnabled()) {
ResolvableType resolvableType = ResolvableType.forType(this.responseType);
logger.debug("Reading to [" + resolvableType + "]");
}
return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
}
}
if (this.responseClass != null) {
if (messageConverter.canRead(this.responseClass, contentType)) {
if (logger.isDebugEnabled()) {
String className = this.responseClass.getName();
logger.debug("Reading to [" + className + "] as \"" + contentType + "\"");
}
return (T) messageConverter.read((Class) this.responseClass, responseWrapper);
}
}
}
}
.....
}
1.6 contentType與messageConverter之間的關(guān)系
在HttpMessageConverterExtractor
的extractData
方法中看出,會根據(jù)contentType
與responseClass
選擇messageConverter
是否可讀搂誉、消息轉(zhuǎn)換徐紧。關(guān)系如下:
類名 | 支持的JavaType | 支持的MediaType |
---|---|---|
ByteArrayHttpMessageConverter | byte[] | application/octet-stream, */* |
StringHttpMessageConverter | String | text/plain, */* |
ResourceHttpMessageConverter | Resource | */* |
SourceHttpMessageConverter | Source | application/xml, text/xml, application/*+xml |
AllEncompassingFormHttpMessageConverter | Map<K, List<?>> | application/x-www-form-urlencoded, multipart/form-data |
MappingJackson2HttpMessageConverter | Object | application/json, application/*+json |
Jaxb2RootElementHttpMessageConverter | Object | application/xml, text/xml, application/*+xml |
JavaSerializationConverter | Serializable | x-java-serialization;charset=UTF-8 |
FastJsonHttpMessageConverter | Object | */* |
2. springboot集成RestTemplate
??根據(jù)上述源碼的分析學習,可以輕松炭懊,簡單地在項目進行對RestTemplate進行優(yōu)雅地使用并级,比如增加自定義的異常處理、MessageConverter
以及攔截器interceptor
侮腹。本文使用示例demo
嘲碧,詳情請查看接下來的內(nèi)容。
2.1. 導入依賴:(RestTemplate集成在Web Start中)
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
2.2. RestTemplat配置:
- 使用
ClientHttpRequestFactory
屬性配置RestTemplat參數(shù)父阻,比如ConnectTimeout
愈涩,ReadTimeout
; - 增加自定義的
interceptor
攔截器和異常處理; - 追加
message
轉(zhuǎn)換器; - 配置自定義的異常處理.
@Configuration
public class RestTemplateConfig {
@Value("${resttemplate.connection.timeout}")
private int restTemplateConnectionTimeout;
@Value("${resttemplate.read.timeout}")
private int restTemplateReadTimeout;
@Bean
//@LoadBalanced
public RestTemplate restTemplate( ClientHttpRequestFactory simleClientHttpRequestFactory) {
RestTemplate restTemplate = new RestTemplate();
//配置自定義的message轉(zhuǎn)換器
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
messageConverters.add(new CustomMappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
//配置自定義的interceptor攔截器
List<ClientHttpRequestInterceptor> interceptors=new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new HeadClientHttpRequestInterceptor());
interceptors.add(new TrackLogClientHttpRequestInterceptor());
restTemplate.setInterceptors(interceptors);
//配置自定義的異常處理
restTemplate.setErrorHandler(new CustomResponseErrorHandler());
restTemplate.setRequestFactory(simleClientHttpRequestFactory);
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory reqFactory= new SimpleClientHttpRequestFactory();
reqFactory.setConnectTimeout(restTemplateConnectionTimeout);
reqFactory.setReadTimeout(restTemplateReadTimeout);
return reqFactory;
}
}
2.3. 組件(自定義異常處理、interceptor攔截器加矛、message轉(zhuǎn)化器)
自定義interceptor
攔截器履婉,實現(xiàn)ClientHttpRequestInterceptor
接口
- 自定義
TrackLogClientHttpRequestInterceptor
,記錄resttemplate
的request
和response
信息荒椭,可進行追蹤分析谐鼎; -
自定義
HeadClientHttpRequestInterceptor
,設(shè)置請求頭的參數(shù)趣惠。API發(fā)送各種請求狸棍,很多請求都需要用到相似或者相同的Http Header。如果在每次請求之前都把Header
填入HttpEntity/RequestEntity
味悄,這樣的代碼會顯得十分冗余草戈,可以在攔截器統(tǒng)一設(shè)置。
TrackLogClientHttpRequestInterceptor:
/**
* @Auther: ccww
* @Date: 2019/10/25 22:48侍瑟,記錄resttemplate訪問信息
* @Description: 記錄resttemplate訪問信息
*/
@Slf4j
public class TrackLogClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
trackRequest(request,body);
ClientHttpResponse httpResponse = execution.execute(request, body);
trackResponse(httpResponse);
return httpResponse;
}
private void trackResponse(ClientHttpResponse httpResponse)throws IOException {
log.info("============================response begin==========================================");
log.info("Status code : {}", httpResponse.getStatusCode());
log.info("Status text : {}", httpResponse.getStatusText());
log.info("Headers : {}", httpResponse.getHeaders());
log.info("=======================response end=================================================");
}
private void trackRequest(HttpRequest request, byte[] body)throws UnsupportedEncodingException {
log.info("======= request begin ========");
log.info("uri : {}", request.getURI());
log.info("method : {}", request.getMethod());
log.info("headers : {}", request.getHeaders());
log.info("request body : {}", new String(body, "UTF-8"));
log.info("======= request end ========");
}
}
HeadClientHttpRequestInterceptor:
@Slf4j
public class HeadClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
log.info("#####head handle########");
HttpHeaders headers = httpRequest.getHeaders();
headers.add("Accept", "application/json");
headers.add("Accept-Encoding", "gzip");
headers.add("Content-Encoding", "UTF-8");
headers.add("Content-Type", "application/json; charset=UTF-8");
ClientHttpResponse response = clientHttpRequestExecution.execute(httpRequest, bytes);
HttpHeaders headersResponse = response.getHeaders();
headersResponse.add("Accept", "application/json");
return response;
}
}
自定義異常處理唐片,可繼承DefaultResponseErrorHandler
或者實現(xiàn)ResponseErrorHandler
接口:
- 實現(xiàn)自定義
ErrorHandler
的思路是根據(jù)響應(yīng)消息體進行相應(yīng)的異常處理策略,對于其他異常情況由父類DefaultResponseErrorHandler
來進行處理涨颜。 -
自定義
CustomResponseErrorHandler
進行30x異常處理
CustomResponseErrorHandler:
/**
* @Auther: Ccww
* @Date: 2019/10/28 17:00
* @Description: 30X的異常處理
*/
@Slf4j
public class CustomResponseErrorHandler extends DefaultResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = response.getStatusCode();
if(statusCode.is3xxRedirection()){
return true;
}
return super.hasError(response);
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = response.getStatusCode();
if(statusCode.is3xxRedirection()){
log.info("########30X錯誤费韭,需要重定向!##########");
return;
}
super.handleError(response);
}
}
自定義message轉(zhuǎn)化器
/**
* @Auther: Ccww
* @Date: 2019/10/29 21:15
* @Description: 將Content-Type:"text/html"轉(zhuǎn)換為Map類型格式
*/
public class CustomMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
public CustomMappingJackson2HttpMessageConverter() {
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.TEXT_PLAIN);
mediaTypes.add(MediaType.TEXT_HTML); //加入text/html類型的支持
setSupportedMediaTypes(mediaTypes);// tag6
}
}