您好,登錄后才能下訂單哦!
這篇文章主要講解了“feign中RequestInterceptor的原理和作用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“feign中RequestInterceptor的原理和作用”吧!
本文主要研究一下feign的RequestInterceptor
feign-core-10.2.3-sources.jar!/feign/RequestInterceptor.java
public interface RequestInterceptor { /** * Called for every request. Add data using methods on the supplied {@link RequestTemplate}. */ void apply(RequestTemplate template); }
RequestInterceptor接口定義了apply方法,其參數(shù)為RequestTemplate;它有一個(gè)抽象類為BaseRequestInterceptor,還有幾個(gè)實(shí)現(xiàn)類分別為BasicAuthRequestInterceptor、FeignAcceptGzipEncodingInterceptor、FeignContentGzipEncodingInterceptor
feign-core-10.2.3-sources.jar!/feign/auth/BasicAuthRequestInterceptor.java
public class BasicAuthRequestInterceptor implements RequestInterceptor { private final String headerValue; /** * Creates an interceptor that authenticates all requests with the specified username and password * encoded using ISO-8859-1. * * @param username the username to use for authentication * @param password the password to use for authentication */ public BasicAuthRequestInterceptor(String username, String password) { this(username, password, ISO_8859_1); } /** * Creates an interceptor that authenticates all requests with the specified username and password * encoded using the specified charset. * * @param username the username to use for authentication * @param password the password to use for authentication * @param charset the charset to use when encoding the credentials */ public BasicAuthRequestInterceptor(String username, String password, Charset charset) { checkNotNull(username, "username"); checkNotNull(password, "password"); this.headerValue = "Basic " + base64Encode((username + ":" + password).getBytes(charset)); } /* * This uses a Sun internal method; if we ever encounter a case where this method is not * available, the appropriate response would be to pull the necessary portions of Guava's * BaseEncoding class into Util. */ private static String base64Encode(byte[] bytes) { return Base64.encode(bytes); } @Override public void apply(RequestTemplate template) { template.header("Authorization", headerValue); } }
BasicAuthRequestInterceptor實(shí)現(xiàn)了RequestInterceptor接口,其apply方法往RequestTemplate添加名為Authorization的header
spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/encoding/BaseRequestInterceptor.java
public abstract class BaseRequestInterceptor implements RequestInterceptor { /** * The encoding properties. */ private final FeignClientEncodingProperties properties; /** * Creates new instance of {@link BaseRequestInterceptor}. * @param properties the encoding properties */ protected BaseRequestInterceptor(FeignClientEncodingProperties properties) { Assert.notNull(properties, "Properties can not be null"); this.properties = properties; } /** * Adds the header if it wasn't yet specified. * @param requestTemplate the request * @param name the header name * @param values the header values */ protected void addHeader(RequestTemplate requestTemplate, String name, String... values) { if (!requestTemplate.headers().containsKey(name)) { requestTemplate.header(name, values); } } protected FeignClientEncodingProperties getProperties() { return this.properties; } }
BaseRequestInterceptor定義了addHeader方法,往requestTemplate添加非重名的header
spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/encoding/FeignAcceptGzipEncodingInterceptor.java
public class FeignAcceptGzipEncodingInterceptor extends BaseRequestInterceptor { /** * Creates new instance of {@link FeignAcceptGzipEncodingInterceptor}. * @param properties the encoding properties */ protected FeignAcceptGzipEncodingInterceptor( FeignClientEncodingProperties properties) { super(properties); } /** * {@inheritDoc} */ @Override public void apply(RequestTemplate template) { addHeader(template, HttpEncoding.ACCEPT_ENCODING_HEADER, HttpEncoding.GZIP_ENCODING, HttpEncoding.DEFLATE_ENCODING); } }
FeignAcceptGzipEncodingInterceptor繼承了BaseRequestInterceptor,它的apply方法往RequestTemplate添加了名為Accept-Encoding,值為gzip,deflate的header
spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptor.java
public class FeignContentGzipEncodingInterceptor extends BaseRequestInterceptor { /** * Creates new instance of {@link FeignContentGzipEncodingInterceptor}. * @param properties the encoding properties */ protected FeignContentGzipEncodingInterceptor( FeignClientEncodingProperties properties) { super(properties); } /** * {@inheritDoc} */ @Override public void apply(RequestTemplate template) { if (requiresCompression(template)) { addHeader(template, HttpEncoding.CONTENT_ENCODING_HEADER, HttpEncoding.GZIP_ENCODING, HttpEncoding.DEFLATE_ENCODING); } } /** * Returns whether the request requires GZIP compression. * @param template the request template * @return true if request requires compression, false otherwise */ private boolean requiresCompression(RequestTemplate template) { final Map<String, Collection<String>> headers = template.headers(); return matchesMimeType(headers.get(HttpEncoding.CONTENT_TYPE)) && contentLengthExceedThreshold(headers.get(HttpEncoding.CONTENT_LENGTH)); } /** * Returns whether the request content length exceed configured minimum size. * @param contentLength the content length header value * @return true if length is grater than minimum size, false otherwise */ private boolean contentLengthExceedThreshold(Collection<String> contentLength) { try { if (contentLength == null || contentLength.size() != 1) { return false; } final String strLen = contentLength.iterator().next(); final long length = Long.parseLong(strLen); return length > getProperties().getMinRequestSize(); } catch (NumberFormatException ex) { return false; } } /** * Returns whether the content mime types matches the configures mime types. * @param contentTypes the content types * @return true if any specified content type matches the request content types */ private boolean matchesMimeType(Collection<String> contentTypes) { if (contentTypes == null || contentTypes.size() == 0) { return false; } if (getProperties().getMimeTypes() == null || getProperties().getMimeTypes().length == 0) { // no specific mime types has been set - matching everything return true; } for (String mimeType : getProperties().getMimeTypes()) { if (contentTypes.contains(mimeType)) { return true; } } return false; } }
FeignContentGzipEncodingInterceptor繼承了BaseRequestInterceptor,其apply方法先判斷是否需要compression,即mimeType是否符合要求以及content大小是否超出閾值,需要compress的話則添加名為Content-Encoding,值為gzip,deflate的header
RequestInterceptor接口定義了apply方法,其參數(shù)為RequestTemplate;它有一個(gè)抽象類為BaseRequestInterceptor,還有幾個(gè)實(shí)現(xiàn)類分別為BasicAuthRequestInterceptor、FeignAcceptGzipEncodingInterceptor、FeignContentGzipEncodingInterceptor
BasicAuthRequestInterceptor實(shí)現(xiàn)了RequestInterceptor接口,其apply方法往RequestTemplate添加名為Authorization的header
BaseRequestInterceptor定義了addHeader方法,往requestTemplate添加非重名的header;FeignAcceptGzipEncodingInterceptor繼承了BaseRequestInterceptor,它的apply方法往RequestTemplate添加了名為Accept-Encoding,值為gzip,deflate的header;FeignContentGzipEncodingInterceptor繼承了BaseRequestInterceptor,其apply方法先判斷是否需要compression,即mimeType是否符合要求以及content大小是否超出閾值,需要compress的話則添加名為Content-Encoding,值為gzip,deflate的header
感謝各位的閱讀,以上就是“feign中RequestInterceptor的原理和作用”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)feign中RequestInterceptor的原理和作用這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。