最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud組件OpenFeign之攔截器解讀

 更新時間:2023年04月26日 10:29:06   作者:luffylv  
這篇文章主要介紹了SpringCloud組件OpenFeign之攔截器用法。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringCloud組件OpenFeign之攔截器

OpenFeign組件中有這么一個接口——RequestInterceptor 。

我們來看一下源碼中關于這個接口的介紹。

package feign;
?
/**
?* 可以配置零個或多個請求攔截器,可以用于例如給所有請求添加請求頭信息.但是不能保證攔截器的應用順?
?* 序。一旦攔截器被應用,就會調用Target類中的apply(RequestTemplate)方法去創(chuàng)建不可變的http請?
?* 求,該請求通過Client類中的execute(Request, feign.Request.Options)發(fā)送。
?*
?* 攔截器是在設置rest模板參數(shù)后才被應用的,因此不能再攔截器中添加參數(shù),比如不能再 ? ?
?* apply(RequestTemplate)方法中給/path/{foo}/bar中的foo設置參數(shù)。
?* 這個類類似于RequestInterceptor.intercept()方法,可以實現(xiàn)讀取、刪除或以其他方式改變請求模板?
?* 的任何部分。
?*/
public interface RequestInterceptor {
?
? /**
? ?* 可以被每個請求調用。使用RequestTemplate提供的這個方法可以添加數(shù)據(jù)。
? ?*/
? void apply(RequestTemplate template);
}

通過對該類及方法的注釋可以了解到RequestInterceptor接口的apply方法可以對請求進行攔截,可以在該方法中添加請求頭信息。

實踐一下。

一、創(chuàng)建一個攔截器在請求頭中添加traceId信息

場景如下,使用攔截器在請求頭中添加traceId屬性,服務端可以獲取到該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接口請求攔截器
?**/
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {
?
? ? /**
? ? ?* @description: 將traceId設置到請求頭
? ? ?*/
? ? @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接口請求攔截器
?**/
public class FeignRequestInterceptor implements RequestInterceptor {
?
? ? /**
? ? ?* @description: 將traceId設置到請求頭
? ? ?*/
? ? @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)建兩個攔截器

也可以同時創(chuàng)建多個攔截器實現(xiàn)攔截器鏈的功能。

此時再創(chuàng)建一個攔截器FeignRequestInterceptor2,用于在請求頭中設置屬性名為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接口請求攔截器
?**/
@Configuration
public class FeignRequestInterceptor2 implements RequestInterceptor {
?
? ? /**
? ? ?* @description: 將test設置到請求頭
? ? ?*/
? ? @Override
? ? public void apply(RequestTemplate template) {
? ? ? ? String traceId = TraceIdUtil.getTraceId();
? ? ? ? if (StringUtils.isNotEmpty(traceId)) {
? ? ? ? ? ? template.header("test", "lalala");
? ? ? ? }
? ? }
}

方式二:同上,注意這里設置的@FeignClient注解的configuration屬性值是兩個攔截器的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)建并配置攔截器時有兩點需要特別注意。

1.在使用方式一去創(chuàng)建攔截器時

會攔截所有請求。用方式二時若@FeignClient注解的configuration屬性未設置攔截器,那么并不會攔截該接口下所有方法的請求。攔截器只會攔截所有configuration屬性值設置了攔截器的接口下所有方法的請求。因此使用方式二更靈活。

2.攔截器執(zhí)行順序

若使用方式一去創(chuàng)建多個攔截器時,正如前面注釋所講,不能保證攔截器的執(zhí)行順序。

但是使用方式二則可以控制攔截器的執(zhí)行順序,攔截器的執(zhí)行順序和@FeignClient注解中configuration屬性中攔截器的配置順序有關。

若配置為 {FeignRequestInterceptor.class,FeignRequestInterceptor2.class}),則會先執(zhí)行FeignRequestInterceptor中的攔截,再執(zhí)行FeignRequestInterceptor2中的攔截。

若配置為 {FeignRequestInterceptor2.class,FeignRequestInterceptor.class}),則會先執(zhí)行FeignRequestInterceptor2中的攔截,再執(zhí)行FeignRequestInterceptor中的攔截。有興趣的可以試一下。

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 教你構建第一個Java Applet程序

    教你構建第一個Java Applet程序

    本文的主要目的是創(chuàng)建一個簡單的Java applet,需要的朋友可以參考下
    2014-10-10
  • Java利用InputStream類實現(xiàn)文件讀取與處理

    Java利用InputStream類實現(xiàn)文件讀取與處理

    在Java開發(fā)中,輸入流(InputStream)是一個非常重要的概念,它涉及到文件讀寫、網(wǎng)絡傳輸?shù)榷鄠€方面,InputStream類是Java中輸入流的抽象基類,定義了讀取輸入流數(shù)據(jù)的方法,本文將以InputStream類為切入點,介紹Java中的輸入流概念及其應用,需要的朋友可以參考下
    2023-11-11
  • Spring中Bean注入源碼示例解析

    Spring中Bean注入源碼示例解析

    這篇文章主要為大家介紹了Spring中Bean注入源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • java駝峰轉換的方法

    java駝峰轉換的方法

    這篇文章主要為大家詳細介紹了java駝峰轉換的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Mybatis集成到Spring容器的詳細步驟

    Mybatis集成到Spring容器的詳細步驟

    在現(xiàn)在的JavaEE開發(fā)過程中,我們經(jīng)常會使用到Spring+SpringMVC+Mybatis這個組合,那么Mybatis是如何集成到Spring中的呢,下面通過實例代碼給大家詳細講解,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • SpringBoot+Redis實現(xiàn)消息的發(fā)布與訂閱的示例代碼

    SpringBoot+Redis實現(xiàn)消息的發(fā)布與訂閱的示例代碼

    本文主要介紹了SpringBoot+Redis實現(xiàn)消息的發(fā)布與訂閱,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • SpringBoot中使用configtree讀取樹形文件目錄中的配置詳解

    SpringBoot中使用configtree讀取樹形文件目錄中的配置詳解

    這篇文章主要介紹了SpringBoot中使用configtree讀取樹形文件目錄中的配置詳解,configtree通過spring.config.import?+?configtree:前綴的方式,加載以文件名為key、文件內容為value的配置屬性,需要的朋友可以參考下
    2023-12-12
  • mybatis-plus返回查詢總記錄數(shù)方式

    mybatis-plus返回查詢總記錄數(shù)方式

    這篇文章主要介紹了mybatis-plus返回查詢總記錄數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • java使用htmlparser提取網(wǎng)頁純文本例子

    java使用htmlparser提取網(wǎng)頁純文本例子

    這篇文章主要介紹了java使用htmlparser提取網(wǎng)頁純文本例子,需要的朋友可以參考下
    2014-04-04
  • SpringBoot詳細講解異步任務如何獲取HttpServletRequest

    SpringBoot詳細講解異步任務如何獲取HttpServletRequest

    在使用框架日常開發(fā)中需要在controller中進行一些異步操作減少請求時間,但是發(fā)現(xiàn)在使用@Anysc注解后會出現(xiàn)Request對象無法獲取的情況,本文就此情況給出完整的解決方案
    2022-04-04

最新評論

丰顺县| 巴林左旗| 台山市| 温宿县| 贡山| 深圳市| 亳州市| 离岛区| 武穴市| 报价| 静海县| 邻水| 奉节县| 唐山市| 加查县| 连江县| 都安| 池州市| 大名县| 南乐县| 方山县| 江川县| 抚顺市| 金门县| 上蔡县| 海门市| 钟山县| 肇东市| 米泉市| 华容县| 江安县| 三都| 阿勒泰市| 周口市| 临潭县| 洱源县| 新绛县| 渝中区| 西峡县| 嵊泗县| 财经|