亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用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)行傳遞。

但是我們可以通過(guò)通過(guò)實(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)用,目前沒(méi)找到解決辦法

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

 

相關(guān)文章

  • Java多線程實(shí)現(xiàn)同時(shí)輸出

    Java多線程實(shí)現(xiàn)同時(shí)輸出

    這篇文章主要介紹了Java多線程實(shí)現(xiàn)同時(shí)打印的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • java使用poi生成excel的步驟

    java使用poi生成excel的步驟

    2010以上格式使用XSSFWorkBook對(duì)象, 2003格式使用HSSFWorkBook對(duì)象, 其他對(duì)象操作基本一樣,本文重點(diǎn)給大家介紹java使用poi生成excel的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2022-04-04
  • java實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼生成

    java實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼生成

    這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Java實(shí)現(xiàn)5種負(fù)載均衡算法(小結(jié))

    Java實(shí)現(xiàn)5種負(fù)載均衡算法(小結(jié))

    負(fù)載均衡是將客戶端請(qǐng)求訪問(wèn),通過(guò)提前約定好的規(guī)則轉(zhuǎn)發(fā)給各個(gè)server,本文主要介紹了Java實(shí)現(xiàn)5種負(fù)載均衡算法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-06-06
  • 如何使用Java給您的圖片瘦身之Thumbnailator技術(shù)

    如何使用Java給您的圖片瘦身之Thumbnailator技術(shù)

    在java日常開(kāi)發(fā)中經(jīng)常遇到對(duì)圖片資源的操作需求,如壓縮、縮放、旋轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于如何使用Java給您的圖片瘦身之Thumbnailator技術(shù)的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • 徹底搞懂java并發(fā)ThreadPoolExecutor使用

    徹底搞懂java并發(fā)ThreadPoolExecutor使用

    這篇文章主要為大家介紹了徹底搞懂java并發(fā)ThreadPoolExecutor使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 都9102年了,你還用for循環(huán)操作集合嗎

    都9102年了,你還用for循環(huán)操作集合嗎

    這篇文章主要給大家介紹了關(guān)于java中for循環(huán)操作集合使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 詳解Java的JDBC API的存儲(chǔ)過(guò)程與SQL轉(zhuǎn)義語(yǔ)法的使用

    詳解Java的JDBC API的存儲(chǔ)過(guò)程與SQL轉(zhuǎn)義語(yǔ)法的使用

    這篇文章主要介紹了詳解Java的JDBC API的存儲(chǔ)過(guò)程與SQL轉(zhuǎn)義語(yǔ)法的使用,JDBC是Java用于連接使用各種數(shù)據(jù)庫(kù)的API,需要的朋友可以參考下
    2015-12-12
  • Spark Streaming編程初級(jí)實(shí)踐詳解

    Spark Streaming編程初級(jí)實(shí)踐詳解

    這篇文章主要為大家介紹了Spark Streaming編程初級(jí)實(shí)踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • idea搭建可運(yùn)行Servlet的Web項(xiàng)目

    idea搭建可運(yùn)行Servlet的Web項(xiàng)目

    在網(wǎng)上看到一篇很詳細(xì)的 intelliJ IDEA 創(chuàng)建web項(xiàng)目并簡(jiǎn)單部署servlet的圖文教程,今天自己也配置一下,留個(gè)筆記,感興趣的可以了解一下
    2021-06-06

最新評(píng)論