溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringCloud組件OpenFeign之攔截器怎么創(chuàng)建

發(fā)布時間:2023-04-26 11:38:29 來源:億速云 閱讀:108 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“SpringCloud組件OpenFeign之攔截器怎么創(chuàng)建”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“SpringCloud組件OpenFeign之攔截器怎么創(chuàng)建”文章能幫助大家解決問題。

    SpringCloud組件OpenFeign之攔截器

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

    我們來看一下源碼中關(guān)于這個接口的介紹。

    package feign;
     
    /**
     * 可以配置零個或多個請求攔截器,可以用于例如給所有請求添加請求頭信息.但是不能保證攔截器的應用順 
     * 序。一旦攔截器被應用,就會調(diào)用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 {
     
      /**
       * 可以被每個請求調(diào)用。使用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屬性中攔截器的配置順序有關(guān)。

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

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

    關(guān)于“SpringCloud組件OpenFeign之攔截器怎么創(chuàng)建”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI