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
相关推荐
雷神乐乐6 分钟前
File.separator与File.separatorChar的区别
java·路径分隔符
小刘|11 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
逊嘘30 分钟前
【Java语言】抽象类与接口
java·开发语言·jvm
morris13137 分钟前
【SpringBoot】Xss的常见攻击方式与防御手段
java·spring boot·xss·csp
七星静香1 小时前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员1 小时前
java导出word文件(手绘)
java·开发语言·word
ZHOUPUYU1 小时前
IntelliJ IDEA超详细下载安装教程(附安装包)
java·ide·intellij-idea
stewie61 小时前
在IDEA中使用Git
java·git
Elaine2023911 小时前
06 网络编程基础
java·网络
Gemini19951 小时前
分布式和微服务的区别
分布式·微服务·架构