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
相关推荐
方圆想当图灵7 分钟前
缓存之美:万文详解 Caffeine 实现原理(下)
java·redis·缓存
栗豆包22 分钟前
w175基于springboot的图书管理系统的设计与实现
java·spring boot·后端·spring·tomcat
等一场春雨1 小时前
Java设计模式 十四 行为型模式 (Behavioral Patterns)
java·开发语言·设计模式
微微%2 小时前
SpringCloud微服务Gateway网关简单集成Sentinel
spring cloud·微服务·gateway
酱学编程2 小时前
java中的单元测试的使用以及原理
java·单元测试·log4j
我的运维人生2 小时前
Java并发编程深度解析:从理论到实践
java·开发语言·python·运维开发·技术共享
一只爱吃“兔子”的“胡萝卜”2 小时前
2.Spring-AOP
java·后端·spring
HappyAcmen2 小时前
Java中List集合的面试试题及答案解析
java·面试·list
Ase5gqe2 小时前
Windows 配置 Tomcat环境
java·windows·tomcat
大乔乔布斯3 小时前
JRE、JVM 和 JDK 的区别
java·开发语言·jvm