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

使用Feign傳遞請求頭信息(Finchley版本)

 更新時間:2022年03月07日 10:39:15   作者:AaronSimon  
這篇文章主要介紹了使用Feign傳遞請求頭信息(Finchley版本),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Feign傳遞請求頭信息

在我之前的文章服務(wù)網(wǎng)關(guān)Spring Cloud Zuul中,將用戶的登錄id放在了請求頭中傳遞給內(nèi)部服務(wù)。

但是當(dāng)內(nèi)部服務(wù)之間存在feign調(diào)用時,那么請求頭信息會在feign請求的時候傳遞嗎?不會,請求的頭信息和請求參數(shù)都不會進(jìn)行傳遞。

但是我們可以通過通過實現(xiàn)RequestInterceptor接口,完成對所有的Feign請求,傳遞請求頭和請求參數(shù)。

實現(xiàn)RequestInterceptor接口

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
?* Feign請求攔截器(設(shè)置請求頭,傳遞登錄信息)
?*
?* @author simon
?* @create 2018-09-07 9:51
?**/
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
? @Override
? public void apply(RequestTemplate requestTemplate) {
? ? ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
? ? ? ? ? ? .getRequestAttributes();
? ? HttpServletRequest request = attributes.getRequest();
? ? Enumeration<String> headerNames = request.getHeaderNames();
? ? if (headerNames != null) {
? ? ? while (headerNames.hasMoreElements()) {
? ? ? ? String name = headerNames.nextElement();
? ? ? ? String values = request.getHeader(name);
? ? ? ? requestTemplate.header(name, values);
? ? ? }
? ? }
? }
}

這里只設(shè)置了請求頭,如果想傳遞請求參數(shù),可以參考如下代碼:

public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
? @Override
? public void apply(RequestTemplate requestTemplate) {
? ? ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
? ? ? ? ? ? .getRequestAttributes();
? ? HttpServletRequest request = attributes.getRequest();
? ? Enumeration<String> headerNames = request.getHeaderNames();
? ? if (headerNames != null) {
? ? ? while (headerNames.hasMoreElements()) {
? ? ? ? String name = headerNames.nextElement();
? ? ? ? String values = request.getHeader(name);
? ? ? ? requestTemplate.header(name, values);
? ? ? }
? ? }
? ? Enumeration<String> bodyNames = request.getParameterNames();
? ? ? StringBuffer body =new StringBuffer();
? ? ? if (bodyNames != null) {
? ? ? ? ? while (bodyNames.hasMoreElements()) {
? ? ? ? ? ? String name = bodyNames.nextElement();
? ? ? ? ? ? String values = request.getParameter(name);
? ? ? ? ? ? body.append(name).append("=").append(values).append("&");
? ? ? ? ? }
? ? ? }
? ? ?if(body.length()!=0) {
? ? ? ? body.deleteCharAt(body.length()-1);
? ? ? ? template.body(body.toString());
? ? ? ? logger.info("feign interceptor body:{}",body.toString());
? ? }
? }
}

注冊配置

package com.southgis.ibase.personalConfigure.config;
import com.southgis.ibase.utils.FeignBasicAuthRequestInterceptor;
import com.southgis.ibase.utils.FeignSpringFormEncoder;
import feign.RequestInterceptor;
import feign.codec.Encoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
?* Feign配置注冊(全局)
?*
?* @author simon
?* @create 2018-08-20 11:44
?**/
@Configuration
public class FeignSupportConfig {
? /**
? ?* feign請求攔截器
? ?*
? ?* @return
? ?*/
? @Bean
? public RequestInterceptor requestInterceptor(){
? ? return new FeignBasicAuthRequestInterceptor();
? }
}

這個文件放在項目的掃描目錄下,所有的feign調(diào)用都會使用此配置。如果只有某個feign調(diào)用則可以這樣設(shè)置(但配置類不能在掃描目錄下):

@FeignClient(name = "organ",path = "/organ/OrganInfo",configuration = FeignSupportConfig.class)

Feign調(diào)用微服務(wù)傳遞header請求頭

package com.chitic.module.core.config;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
 
@Configuration
public class FeignConfig {
    @Bean
    public RequestInterceptor headerInterceptor() {
        return template -> {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (null != attributes) {
                HttpServletRequest request = attributes.getRequest();
                Enumeration<String> headerNames = request.getHeaderNames();
                if (headerNames != null) {
                    while (headerNames.hasMoreElements()) {
                        String name = headerNames.nextElement();
                        String values = request.getHeader(name);
                        template.header(name, values);
                    }
                }
            }
        };
    }
}

需注意,feign調(diào)用時不能調(diào)用含有HttpServletResponse參數(shù)(比如常用的數(shù)據(jù)導(dǎo)出),以下就不能遠(yuǎn)程調(diào)用,目前沒找到解決辦法

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

 

相關(guān)文章

  • 最全LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化的方法

    最全LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化的方法

    大家在開發(fā)過程中必不可少的和日期打交道,對接別的系統(tǒng)時,時間日期格式不一致,每次都要轉(zhuǎn)化,本文為大家準(zhǔn)備了最全的LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化方法,需要的可以參考一下
    2023-06-06
  • 淺談Java的WeakHashMap源碼

    淺談Java的WeakHashMap源碼

    這篇文章主要介紹了淺談Java的WeakHashMap源碼,WeakHashMap,從名字可以看出它是某種?Map,它的特殊之處在于?WeakHashMap?里的entry可能會被GC自動刪除,即使程序員沒有調(diào)用remove()或者clear()方法,需要的朋友可以參考下
    2023-09-09
  • Spring?Boot虛擬線程Webflux在JWT驗證和MySQL查詢性能比較

    Spring?Boot虛擬線程Webflux在JWT驗證和MySQL查詢性能比較

    這篇文章主要為大家介紹了Spring Boot虛擬線程與Webflux在JWT驗證和MySQL查詢上的性能比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • SpringMVC框架整合Junit進(jìn)行單元測試(案例詳解)

    SpringMVC框架整合Junit進(jìn)行單元測試(案例詳解)

    本文詳細(xì)介紹在SpringMVC任何使用Junit框架。首先介紹了如何引入依賴,接著介紹了編寫一個測試基類,并且對其中涉及的各個注解做了一個詳細(xì)說明,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • Java8中AbstractExecutorService與FutureTask源碼詳解

    Java8中AbstractExecutorService與FutureTask源碼詳解

    這篇文章主要給大家介紹了關(guān)于Java8中AbstractExecutorService與FutureTask的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-01-01
  • 解決idea中javaweb的mysql8.0.15配置問題

    解決idea中javaweb的mysql8.0.15配置問題

    這篇文章主要介紹了idea中javaweb的mysql8.0.15配置問題 ,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • Java內(nèi)存區(qū)域管理詳解

    Java內(nèi)存區(qū)域管理詳解

    這篇文章主要介紹了Java內(nèi)存區(qū)域管理詳解,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Mybatis多個字段模糊匹配同一個值的案例

    Mybatis多個字段模糊匹配同一個值的案例

    這篇文章主要介紹了Mybatis多個字段模糊匹配同一個值的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • java運行windows的cmd命令簡單代碼

    java運行windows的cmd命令簡單代碼

    這篇文章主要介紹了java運行windows的cmd命令簡單代碼,有需要的朋友可以參考一下
    2013-12-12
  • jmeter實現(xiàn)接口關(guān)聯(lián)的兩種方式(正則表達(dá)式提取器和json提取器)

    jmeter實現(xiàn)接口關(guān)聯(lián)的兩種方式(正則表達(dá)式提取器和json提取器)

    Jmeter用于接口測試時,后一個接口經(jīng)常需要用到前一次接口返回的結(jié)果,本文主要介紹了jmeter實現(xiàn)接口關(guān)聯(lián)的兩種方式,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評論

屏边| 瑞昌市| 句容市| 喀喇沁旗| 卢氏县| 宁强县| 舟曲县| 长垣县| 奉新县| 绍兴市| 新昌县| 连山| 绵竹市| 施秉县| 上饶县| 新邵县| 翼城县| 宜兰市| 彭山县| 宜兰市| 龙川县| 杭锦旗| 山东省| 叙永县| 沿河| 丽水市| 敖汉旗| 疏勒县| 光泽县| 涡阳县| 平江县| 邹城市| 静宁县| 疏附县| 眉山市| 南康市| 翁源县| 宁城县| 惠来县| 义马市| 独山县|