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

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

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

Feign傳遞請(qǐng)求頭信息

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

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

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

實(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請(qǐng)求攔截器(設(shè)置請(qǐng)求頭,傳遞登錄信息)
?*
?* @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è)置了請(qǐng)求頭,如果想傳遞請(qǐng)求參數(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());
? ? }
? }
}

注冊(cè)配置

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配置注冊(cè)(全局)
?*
?* @author simon
?* @create 2018-08-20 11:44
?**/
@Configuration
public class FeignSupportConfig {
? /**
? ?* feign請(qǐng)求攔截器
? ?*
? ?* @return
? ?*/
? @Bean
? public RequestInterceptor requestInterceptor(){
? ? return new FeignBasicAuthRequestInterceptor();
? }
}

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

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

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

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)用時(shí)不能調(diào)用含有HttpServletResponse參數(shù)(比如常用的數(shù)據(jù)導(dǎo)出),以下就不能遠(yuǎn)程調(diào)用,目前沒找到解決辦法

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

 

相關(guān)文章

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

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

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

    淺談Java的WeakHashMap源碼

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

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

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

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

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

    Java8中AbstractExecutorService與FutureTask源碼詳解

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

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

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

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

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

    Mybatis多個(gè)字段模糊匹配同一個(gè)值的案例

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

    java運(yùn)行windows的cmd命令簡(jiǎn)單代碼

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

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

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

最新評(píng)論

景洪市| 新郑市| 朝阳市| 新泰市| 柘城县| 沂南县| 明水县| 芒康县| 绵阳市| 巴林左旗| 铅山县| 厦门市| 彰化市| 武胜县| 聂拉木县| 新昌县| 安顺市| 涞源县| 金溪县| 松滋市| 师宗县| 黄骅市| 清远市| 桂林市| 杭锦旗| 聊城市| 临沭县| 垦利县| 汶上县| 呼和浩特市| 广河县| 叶城县| 谢通门县| 客服| 城口县| 新化县| 兴山县| 嘉祥县| 大洼县| 林芝县| 垫江县|