国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

feign微服務之間傳遞請求頭數(shù)據(jù)

這篇具有很好參考價值的文章主要介紹了feign微服務之間傳遞請求頭數(shù)據(jù)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

直接在微服務遠程調(diào)用中獲取請求頭數(shù)據(jù)不能直接獲取到.為什么? 看源碼

默認情況下feign遠程調(diào)用的時候不會傳遞請求頭!

遠程調(diào)用源碼:

feign微服務之間傳遞請求頭數(shù)據(jù)

每一次遠程請求的時候都創(chuàng)建了一個新的Request Template對象,在該對象中不包含之前的請求頭數(shù)據(jù)

解決方案:

方案一:在feign接口上添加對應的形式參數(shù)即可

弊端:每一個接口想要獲取參數(shù)都需要在接口方法上添加對應的形式參數(shù).影響代碼效率

方案二:使用OpenFeign中的攔截器(RequestInterceptor)來攔截請求,添加請求頭。

方案一演示:

FeignClient接口:

@FeignClient(value = "service-cart",fallback = FeignClientFallback.class)//降級方法
public interface FeignClient { 
public Result xxx( 
                  @RequestParam(value = "xx" )Long xx ,
            ? ? ? @RequestParam(value = "xx" )String xx);
}

Controller遠程接口:

@RestController
@RequestMapping("/x/x/x")
public class Controller {

    @GetMapping("/x/x/x")
    public Result aaa(
            @RequestParam(value = "xx" )Long xx ,
            @RequestParam(value = "xx" )String xx
? ? ? ? ? ? ?) {
        return Result.ok() ;
    }
}

//@RequestParam 通過傳參的方法傳遞過去,并非從請求頭上拿
FeignClient.aaa(xx,xx); //遠程調(diào)用時候傳參

方案二演示:

思路:在OpenFeign遠程調(diào)用 定義一個攔截器類實現(xiàn)(RequestInterceptor)接口給RequestTemplate設(shè)置上請求頭

核心源碼

// feign.SynchronousMethodHandler#executeAndDecode
Request targetRequest(RequestTemplate template) {        
    // 在構(gòu)建目標請求對象的時候,遍歷所有的攔截器,
? ? //? ?然后調(diào)用了apply方法把RequestTemplate作為參數(shù)傳遞過去
    for (RequestInterceptor interceptor : requestInterceptors) {
        interceptor.apply(template);
    }
    return target.apply(template);
}

攔截器類如何獲取Controller請求頭數(shù)據(jù)呢?

思路1:通過Map實現(xiàn)

原理:feign的調(diào)用鏈使用的是同一個線程對象

思路2:通過ThreadLocal對象在一個線程中共享HttpServletRequest對象(使用JDK自帶的ThreadLocal在一個線程中共享HttpServletRequest對象,原理就是在底層維護了一個Map。)

思路3: 使用spring提供的對象RequestContextHolder進行實現(xiàn)!

原理:底層通過RequestContextListener監(jiān)聽器實現(xiàn)

思路1實現(xiàn):

獲取Controller中請求頭思路:

feign的調(diào)用鏈使用的是同一個線程對象

所以可以在Controller中定義一個Map集合,鍵就是當前線程對象,值就是HttpServletRequest對象

 //定義一個Map,作用是在tController和FeignClientInterceptor(攔截器)中共享HttpServletRequest
    public static final ConcurrentHashMap<Thread, HttpServletRequest> concurrentHashMap =new ConcurrentHashMap<>();

//給map中存request對象

@GetMapping("/xx")//required:傳遞過來的參數(shù)可以有可以無
    public String addCart(HttpServletRequest request, ) {

? ? ? ? concurrentHashMap.put(Thread.currentThread(),request);
}

攔截器類通過map獲取請求頭數(shù)據(jù),設(shè)置給RequestTemplate

@Component
@Slf4j
public class FeignClientInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        log.info("FeignClientInterceptor..........");
        HttpServletRequest httpServletRequest = CartController.concurrentHashMap.get(Thread.currentThread());
            //給requestTemplate 添加上對應的請求頭中的數(shù)據(jù):aa,bbb
        requestTemplate.header("userId",httpServletRequest.getHeader("aa"));
        requestTemplate.header("userTempId",httpServletRequest.getHeader("bbb"));

    }
}

思路2實現(xiàn):

在Controller中定義一個ThreadLocal對象

public static final  ThreadLocal<HttpServletRequest> threadLocal = new ThreadLocal<>();

然后在方法中調(diào)用set方法添加數(shù)據(jù)(request)

        threadLocal.set(request);

在攔截器中調(diào)用get方法就可以拿到HttpServletRequest對象,然后給requestTemplate 添加上對應的請求頭中的數(shù)據(jù):aa,bbb

 HttpServletRequest httpServletRequest = CartController.threadLocal.get();

 requestTemplate.header("userId",httpServletRequest.getHeader("aa"));
 requestTemplate.header("userTempId",httpServletRequest.getHeader("bbb"));

思路3實現(xiàn):使用spring提供的對象RequestContextHolder直接在攔截器類獲取request對象

//RequestContextHolder對象中獲取一個線程內(nèi)所共享的HttpServletRequest對象
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
//獲取到了request對象剩下的同上

思路3的在Controller使用模塊中通過RequestContextHolder隱式獲取請求頭數(shù)據(jù)

直接在使用模塊獲取ServletRequestAttributes對象,不需要在參數(shù)添加@RequestHeader(value = "xxx")
因為剛才已經(jīng)用到了request對象
// 獲取ServletRequestAttributes對象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest requestAttributesRequest = requestAttributes.getRequest();
        String userId = requestAttributesRequest.getHeader("userId");
        String userTempId = requestAttributesRequest.getHeader("userTempId") ;

代碼優(yōu)化:

1.將從RequestContextHolder對象中讀取xx和xx數(shù)據(jù)的代碼封裝到一個工具類(utilxx),并使用一個實體類返回相關(guān)數(shù)據(jù):

public class Utils {

    public static UserInfoVo userInfo() {

        // 獲取請求對象
        ServletRequestAttributes requestAttributes 
? ? ? ? = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();

        // 判斷requestAttributes是否為空
        if(requestAttributes != null) {

            // 獲取request對象
            HttpServletRequest request = requestAttributes.getRequest();

            // 從請求對象中獲取xx和xx數(shù)據(jù)
            String userid = request.getHeader("id");
            String username = request.getHeader("name");

            // 封裝數(shù)據(jù)到xxx對象中
            User user = new user() ;
            if(!StringUtils.isEmpty(userId)) {
                userAuthInfoVo.setUserId(Long.parseLong(userId));
            }
            userAuthInfoVo.setUserTempId(username);

            // 返回
            return user ;
        }

        return null ;
    }

}

2.FeignClientInterceptor抽取成公共組件

為了實現(xiàn)FeignClientInterceptor的復用,那么此時可以將FeignClientInterceptor抽取成一個公共的組件


@Slf4j
@Component
public class FeignClientInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        
        log.info("FeignClientInterceptor攔截器執(zhí)行了....");

        User user = User.getUser();
        if(user != null) {
            Long userId = user.getUserId();
            if(userId != null) {
                template.header("userId" , String.valueOf(userId)) ;
            }
            template.header("username" , userAuthInfo.getUsername()) ;
        }

    }

}

自定義注解:

因為抽取公共類后包路徑不一樣會導致掃描不到所以,可以使用自定義注解或@Import解決,這里使用自定義注解

@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
@Import(value = EnableFeignClientInterceptor.class)
public @interface EnableFeignClientInterceptor {
}

啟動類加上@EnableFeignClientIntercepto即可使用文章來源地址http://www.zghlxwxcb.cn/news/detail-455228.html

到了這里,關(guān)于feign微服務之間傳遞請求頭數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • Windows和Linux之間如何傳遞數(shù)據(jù)|兩臺Linux之間如何傳遞數(shù)據(jù)

    摘要:我們租用了一臺服務器,然后我們想要把我們寫的項目上傳到自己的Linux服務器中,那么我們應該怎么上傳呢?如果我們想要從服務器中下載一些資料,那么又該如何進行呢?看這篇文章將會告訴你答案。 把數(shù)據(jù)從本地電腦上傳到Linux服務器的方式有很多,這里介紹最

    2024年02月03日
    瀏覽(24)
  • 19.組件之間傳遞數(shù)據(jù)

    19.組件之間傳遞數(shù)據(jù)

    不同組件傳遞數(shù)據(jù)的時候,最好不要直接傳遞復雜數(shù)據(jù)類型(比如對象,數(shù)組) 前端需要處理的數(shù)據(jù)層級一般不會很多,需要在多處使用的數(shù)據(jù)一般會被放到數(shù)據(jù)庫中 目錄 1??組件的關(guān)系 2??父向子傳遞數(shù)據(jù)-props 3??子向父傳遞數(shù)據(jù)-自定義事件 4??父子組件同步數(shù)據(jù) v-model與

    2024年02月09日
    瀏覽(14)
  • SpringCloud微服務之間如何進行用戶信息傳遞(涉及:Gateway、OpenFeign組件)

    SpringCloud微服務之間如何進行用戶信息傳遞(涉及:Gateway、OpenFeign組件)

    在業(yè)務微服務中通過工具類獲取當前用戶信息 網(wǎng)關(guān)微服務(Gateway)往業(yè)務微服務傳遞用戶信息 業(yè)務微服務之間通過OpenFeign傳遞用戶信息 只要把上面兩處打通,然后業(yè)務微服務在通過攔截器獲取到用戶信息,之后再將用戶信息存在ThreadLocal中,這樣我們就可以實現(xiàn)在業(yè)務微服

    2024年02月13日
    瀏覽(22)
  • SpringCloud Alibaba(一)微服務簡介+Nacos的安裝部署與使用+Nacos集成springboot實現(xiàn)服務注冊+Feign實現(xiàn)服務之間的遠程調(diào)用+負載均衡+領(lǐng)域劃分

    SpringCloud Alibaba(一)微服務簡介+Nacos的安裝部署與使用+Nacos集成springboot實現(xiàn)服務注冊+Feign實現(xiàn)服務之間的遠程調(diào)用+負載均衡+領(lǐng)域劃分

    目錄 一.認識微服務 1.0.學習目標 1.1.單體架構(gòu) 單體架構(gòu)的優(yōu)缺點如下: 1.2.分布式架構(gòu) 分布式架構(gòu)的優(yōu)缺點: 1.3.微服務 微服務的架構(gòu)特征: 1.4.SpringCloud 1.5Nacos注冊中心 1.6.總結(jié) 二、Nacos基本使用安裝部署+服務注冊 (一)linux安裝包方式單節(jié)點安裝部署 1. jdk安裝配置 2. na

    2024年02月09日
    瀏覽(29)
  • Postman接口之間傳遞數(shù)據(jù) 實現(xiàn)接口關(guān)聯(lián)

    Postman接口之間傳遞數(shù)據(jù) 實現(xiàn)接口關(guān)聯(lián)

    在我們使用Postman進行接口測試的時候,經(jīng)常會遇到一個接口的返回結(jié)果是另一個接口所需要的請求參數(shù)。 例如 :我們再測試時,一般需要先去調(diào)用登錄接口,進行登錄,并返回 token 相關(guān)信息,隨后我們進行調(diào)用其它接口時可能會需要攜帶 token 來完成對應接口所實現(xiàn)的功能

    2023年04月21日
    瀏覽(23)
  • 小程序頁面之間數(shù)據(jù)傳遞的五種方法

    使用 wx.navigateTo() 時,在 url 中拼接,這種方法適用于數(shù)據(jù)量少的情況 跳轉(zhuǎn)前A頁面在 url 中拼接參數(shù),參數(shù)與路徑之間使用 ? 分隔,參數(shù)鍵與參數(shù)值用 = 相連,不同參數(shù)用 分隔; 跳轉(zhuǎn)到B頁面在生命周期函數(shù) onLoad 中接收 如果需要傳遞對象或數(shù)組,需先將對象或數(shù)據(jù)轉(zhuǎn)為JSON字符

    2024年02月10日
    瀏覽(31)
  • 互聯(lián)網(wǎng)大廠技術(shù)-HTTP請求-Springboot整合Feign更優(yōu)雅地實現(xiàn)Http服務調(diào)用

    互聯(lián)網(wǎng)大廠技術(shù)-HTTP請求-Springboot整合Feign更優(yōu)雅地實現(xiàn)Http服務調(diào)用

    目錄 一、SpringBoot快速整合Feign 1.添加Pom依賴 2.啟動類添加注解 3.引用Feign服務 二、為請求添加Header的3種方式 1.添加固定header 2.通過接口簽名添加header 3.動態(tài)添加header 三、為請求添加超時配置 1.默認超時時間 3.超時異常 4.全局超時配置 5.為單個服務設(shè)置超時配置 四、為請求配

    2024年02月04日
    瀏覽(21)
  • 前端HTML網(wǎng)頁之間傳遞數(shù)據(jù)多種辦法,附代碼案例

    前端HTML網(wǎng)頁之間傳遞數(shù)據(jù)多種辦法,附代碼案例

    ? ?目前常用的有三種辦法 session傳遞,cookie傳遞,url傳遞 url會暴露參數(shù),其余的兩個是保存在服務端和瀏覽器中,不會暴露在地址欄里面 使用url: ? 下面依次介紹 案例說明:? 在HTML1中,我們使用 form 標簽將數(shù)據(jù)提交到HTML2頁面,并設(shè)置 method 為 post , action 為HTML2的文件路

    2024年02月09日
    瀏覽(30)
  • 在 React 中,props(屬性)用于在組件之間傳遞數(shù)據(jù)

    在 React 中,props(屬性)用于在組件之間傳遞數(shù)據(jù)。它是父組件向子組件傳遞信息的一種方式,通過 props,父組件可以向子組件傳遞數(shù)據(jù)、回調(diào)函數(shù)、配置項等。 注意: props 是只讀的,它的值由父組件傳遞給子組件時確定,并且在子組件中不能直接修改。如果子組件需要改

    2024年02月15日
    瀏覽(29)
  • 微服務之間Feign調(diào)用無法解析IPage報錯問題:Cannot construct instance of `com.baomidou.mybatisplus.core.metadata.IPage

    微服務之間Feign調(diào)用無法解析IPage報錯問題:Cannot construct instance of `com.baomidou.mybatisplus.core.metadata.IPage

    最新在做一個對外提供基礎(chǔ)信息的需求,我在A服務中寫了一個分頁接口,本以為很簡單的我在B服務用 Feign 調(diào)用一下就可以了。 可想并沒有這么簡單,報錯了: 從源碼中我們可以看到:這里是分頁,而 com.baomidou.mybatisplus.core.metadata.IPage是一個接口(interface),源代碼如下: 因

    2024年04月27日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包