Spring Cloud Alibaba微服务之间怎么让Open Feign携带header调用?

FeignBasicAuthRequestInterceptor.java

java 复制代码
package com.codex.terry.configuration;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * 文件名称: FeignBasicAuthRequestInterceptor.java
 * 编写人: yh.zeng
 * 编写时间: 2024/7/8 14:04
 * 文件描述: 将请求的Header添加到OpenFeign发起的请求中
 */
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor
{
    private HttpServletRequest getHttpServletRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
        return attributes.getRequest();
    }

    @Override
    public void apply(RequestTemplate template) {
        HttpServletRequest request = getHttpServletRequest();

        // 遍历所有header,添加到Feign的RequestTemplate中
        Enumeration<String> headers = request.getHeaderNames();
        while(headers.hasMoreElements()){
            String headerName = headers.nextElement();
            String headerValue = request.getHeader(headerName);
            template.header(headerName, headerValue);
        }

    }
}

1、方式一:

FeignConfig.java

java 复制代码
package com.codex.terry.configuration;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 文件名称: FeignConfig.java
 * 编写人: yh.zeng
 * 编写时间: 2024/7/8 14:09
 * 文件描述: 将请求的Header添加到OpenFeign发起的请求中
 */
@Configuration
public class FeignConfig
{

    @Bean
    public RequestInterceptor requestInterceptor(){
        return new FeignBasicAuthRequestInterceptor();
    }

}

HelloWorldFeignClient.java

java 复制代码
package com.codex.terry.feignclient;

import com.codex.terry.configuration.FeignClientLogConfiguration;
import com.codex.terry.configuration.FeignConfig;
import com.codex.terry.feignclient.fallback.HelloWorldFeignClientFallback;
import com.codex.terry.feignclient.fallbackfactory.HelloWorldFeignClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 文件名称: HelloWorldFeignClient.java
 * 编写人: yh.zeng
 * 编写时间: 2024/6/25 16:05
 * 文件描述: todo
 */
//@FeignClient(name="helloworld", configuration = FeignClientLogConfiguration.class)
//@FeignClient(name="helloworld", fallback = HelloWorldFeignClientFallback.class)
@FeignClient(name="helloworld",configuration = FeignConfig.class ,fallbackFactory = HelloWorldFeignClientFallbackFactory.class)
public interface HelloWorldFeignClient
{
    @RequestMapping("/sayHello")
    String sayHello();

    @RequestMapping(value="/say/{msg}")
    String say(@PathVariable("msg") String msg);
}

2、方式二:

XML 复制代码
feign:
  client:
    config:
      helloworld:
        loggerLevel: full
      default:
        requestInterceptors:
          - com.codex.terry.configuration.FeignBasicAuthRequestInterceptor
相关推荐
孟诸4 分钟前
计算机专业毕设-校园新闻网站
java·vue·毕业设计·springboot·课程设计
计算机学姐15 分钟前
基于SpringBoot+Vue的篮球馆会员信息管理系统
java·vue.js·spring boot·后端·mysql·spring·mybatis
kakwooi15 分钟前
JavaEE---Spring IOC(2)
java·spring·java-ee
程序员大金27 分钟前
基于SpringBoot+Vue+MySQL的智能物流管理系统
java·javascript·vue.js·spring boot·后端·mysql·mybatis
茜茜西西CeCe43 分钟前
移动技术开发:登录注册界面
java·gitee·gradle·android studio·安卓·移动技术开发·原生安卓开发
linux_lzj_cainiao43 分钟前
准备招银社招记录
java
不是编程家1 小时前
C++ 第三讲:内存管理
java·开发语言·c++
尸僵打怪兽1 小时前
软考(中级-软件设计师)(0919)
java·c语言·数据库·计算机网络·软考·多媒体·软件设计师
Liii4031 小时前
【ARM】Cache深度解读
java·arm开发·spring
litGrey2 小时前
Maven国内镜像(四种)
java·数据库·maven