SpringCloud組件OpenFeign之?dāng)r截器解讀
SpringCloud組件OpenFeign之?dāng)r截器
OpenFeign組件中有這么一個(gè)接口——RequestInterceptor 。
我們來(lái)看一下源碼中關(guān)于這個(gè)接口的介紹。
package feign;
?
/**
?* 可以配置零個(gè)或多個(gè)請(qǐng)求攔截器,可以用于例如給所有請(qǐng)求添加請(qǐng)求頭信息.但是不能保證攔截器的應(yīng)用順?
?* 序。一旦攔截器被應(yīng)用,就會(huì)調(diào)用Target類(lèi)中的apply(RequestTemplate)方法去創(chuàng)建不可變的http請(qǐng)?
?* 求,該請(qǐng)求通過(guò)Client類(lèi)中的execute(Request, feign.Request.Options)發(fā)送。
?*
?* 攔截器是在設(shè)置rest模板參數(shù)后才被應(yīng)用的,因此不能再攔截器中添加參數(shù),比如不能再 ? ?
?* apply(RequestTemplate)方法中給/path/{foo}/bar中的foo設(shè)置參數(shù)。
?* 這個(gè)類(lèi)類(lèi)似于RequestInterceptor.intercept()方法,可以實(shí)現(xiàn)讀取、刪除或以其他方式改變請(qǐng)求模板?
?* 的任何部分。
?*/
public interface RequestInterceptor {
?
? /**
? ?* 可以被每個(gè)請(qǐng)求調(diào)用。使用RequestTemplate提供的這個(gè)方法可以添加數(shù)據(jù)。
? ?*/
? void apply(RequestTemplate template);
}通過(guò)對(duì)該類(lèi)及方法的注釋可以了解到RequestInterceptor接口的apply方法可以對(duì)請(qǐng)求進(jìn)行攔截,可以在該方法中添加請(qǐng)求頭信息。
實(shí)踐一下。
一、創(chuàng)建一個(gè)攔截器在請(qǐng)求頭中添加traceId信息
場(chǎng)景如下,使用攔截器在請(qǐng)求頭中添加traceId屬性,服務(wù)端可以獲取到該traceId,用于日志追蹤。
方式一:創(chuàng)建自定義攔截器+@Configuration
package com.example.rtbootconsumer.config.interceptor;
?
import com.example.rtbootconsumer.common.utils.TraceIdUtil;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
?
/**
?* @Description Feign接口請(qǐng)求攔截器
?**/
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {
?
? ? /**
? ? ?* @description: 將traceId設(shè)置到請(qǐng)求頭
? ? ?*/
? ? @Override
? ? public void apply(RequestTemplate template) {
? ? ? ? String traceId = TraceIdUtil.getTraceId();
? ? ? ? if (StringUtils.isNotEmpty(traceId)) {
? ? ? ? ? ? template.header("traceId", traceId);
? ? ? ? }
? ? }
}方式二:創(chuàng)建自定義攔截器+配置@FeignClient注解的configuration屬性
package com.example.rtbootconsumer.config.interceptor;
?
import com.example.rtbootconsumer.common.utils.TraceIdUtil;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
?
/**
?* @Description Feign接口請(qǐng)求攔截器
?**/
public class FeignRequestInterceptor implements RequestInterceptor {
?
? ? /**
? ? ?* @description: 將traceId設(shè)置到請(qǐng)求頭
? ? ?*/
? ? @Override
? ? public void apply(RequestTemplate template) {
? ? ? ? String traceId = TraceIdUtil.getTraceId();
? ? ? ? if (StringUtils.isNotEmpty(traceId)) {
? ? ? ? ? ? template.header("traceId", traceId);
? ? ? ? }
? ? }
}package com.example.rtbootconsumer.feignservice;
?
import com.example.rtbootconsumer.pojo.User;
import com.example.rtbootconsumer.vo.ResultBody;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
?
import java.util.List;
?
?
@FeignClient(name = "service-provider", path = "/testComm", url = "${addr.url}",configuration = FeignRequestInterceptor.class)
public interface UserFeignService {
?
? ? @PostMapping(value = "/getUser")
? ? public ResultBody<User> getUser(@RequestBody User user);
}二、創(chuàng)建兩個(gè)攔截器
也可以同時(shí)創(chuàng)建多個(gè)攔截器實(shí)現(xiàn)攔截器鏈的功能。
此時(shí)再創(chuàng)建一個(gè)攔截器FeignRequestInterceptor2,用于在請(qǐng)求頭中設(shè)置屬性名為test,值為lalala信息。
方式一:同上
package com.example.rtbootconsumer.config.interceptor;
?
import com.example.rtbootconsumer.common.utils.TraceIdUtil;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
?
/**
?* @Description Feign接口請(qǐng)求攔截器
?**/
@Configuration
public class FeignRequestInterceptor2 implements RequestInterceptor {
?
? ? /**
? ? ?* @description: 將test設(shè)置到請(qǐng)求頭
? ? ?*/
? ? @Override
? ? public void apply(RequestTemplate template) {
? ? ? ? String traceId = TraceIdUtil.getTraceId();
? ? ? ? if (StringUtils.isNotEmpty(traceId)) {
? ? ? ? ? ? template.header("test", "lalala");
? ? ? ? }
? ? }
}方式二:同上,注意這里設(shè)置的@FeignClient注解的configuration屬性值是兩個(gè)攔截器的class數(shù)組。
package com.example.rtbootconsumer.feignservice;
?
import com.example.rtbootconsumer.pojo.User;
import com.example.rtbootconsumer.vo.ResultBody;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
?
import java.util.List;
?
?
@FeignClient(name = "service-provider", path = "/testComm", url = "${addr.url}",configuration = {FeignRequestInterceptor.class,FeignRequestInterceptor2.class})
public interface UserFeignService {
?
? ? @PostMapping(value = "/getUser")
? ? public ResultBody<User> getUser(@RequestBody User user);
?
? ? @PostMapping(value = "/testList")
? ? public ResultBody<List<User>> testList(@RequestBody List<User> list);
}三、注意
在創(chuàng)建并配置攔截器時(shí)有兩點(diǎn)需要特別注意。
1.在使用方式一去創(chuàng)建攔截器時(shí)
會(huì)攔截所有請(qǐng)求。用方式二時(shí)若@FeignClient注解的configuration屬性未設(shè)置攔截器,那么并不會(huì)攔截該接口下所有方法的請(qǐng)求。攔截器只會(huì)攔截所有configuration屬性值設(shè)置了攔截器的接口下所有方法的請(qǐng)求。因此使用方式二更靈活。
2.攔截器執(zhí)行順序
若使用方式一去創(chuàng)建多個(gè)攔截器時(shí),正如前面注釋所講,不能保證攔截器的執(zhí)行順序。
但是使用方式二則可以控制攔截器的執(zhí)行順序,攔截器的執(zhí)行順序和@FeignClient注解中configuration屬性中攔截器的配置順序有關(guān)。
若配置為 {FeignRequestInterceptor.class,FeignRequestInterceptor2.class}),則會(huì)先執(zhí)行FeignRequestInterceptor中的攔截,再執(zhí)行FeignRequestInterceptor2中的攔截。
若配置為 {FeignRequestInterceptor2.class,FeignRequestInterceptor.class}),則會(huì)先執(zhí)行FeignRequestInterceptor2中的攔截,再執(zhí)行FeignRequestInterceptor中的攔截。有興趣的可以試一下。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java利用InputStream類(lèi)實(shí)現(xiàn)文件讀取與處理
在Java開(kāi)發(fā)中,輸入流(InputStream)是一個(gè)非常重要的概念,它涉及到文件讀寫(xiě)、網(wǎng)絡(luò)傳輸?shù)榷鄠€(gè)方面,InputStream類(lèi)是Java中輸入流的抽象基類(lèi),定義了讀取輸入流數(shù)據(jù)的方法,本文將以InputStream類(lèi)為切入點(diǎn),介紹Java中的輸入流概念及其應(yīng)用,需要的朋友可以參考下2023-11-11
SpringBoot+Redis實(shí)現(xiàn)消息的發(fā)布與訂閱的示例代碼
本文主要介紹了SpringBoot+Redis實(shí)現(xiàn)消息的發(fā)布與訂閱,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
SpringBoot中使用configtree讀取樹(shù)形文件目錄中的配置詳解
這篇文章主要介紹了SpringBoot中使用configtree讀取樹(shù)形文件目錄中的配置詳解,configtree通過(guò)spring.config.import?+?configtree:前綴的方式,加載以文件名為key、文件內(nèi)容為value的配置屬性,需要的朋友可以參考下2023-12-12
mybatis-plus返回查詢(xún)總記錄數(shù)方式
這篇文章主要介紹了mybatis-plus返回查詢(xún)總記錄數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
java使用htmlparser提取網(wǎng)頁(yè)純文本例子
這篇文章主要介紹了java使用htmlparser提取網(wǎng)頁(yè)純文本例子,需要的朋友可以參考下2014-04-04
SpringBoot詳細(xì)講解異步任務(wù)如何獲取HttpServletRequest
在使用框架日常開(kāi)發(fā)中需要在controller中進(jìn)行一些異步操作減少請(qǐng)求時(shí)間,但是發(fā)現(xiàn)在使用@Anysc注解后會(huì)出現(xiàn)Request對(duì)象無(wú)法獲取的情況,本文就此情況給出完整的解決方案2022-04-04

