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
相关推荐
儿时可乖了16 分钟前
使用 Java 操作 SQLite 数据库
java·数据库·sqlite
ruleslol17 分钟前
java基础概念37:正则表达式2-爬虫
java
xmh-sxh-131434 分钟前
jdk各个版本介绍
java
天天扭码1 小时前
五天SpringCloud计划——DAY2之单体架构和微服务架构的选择和转换原则
java·spring cloud·微服务·架构
程序猿进阶1 小时前
堆外内存泄露排查经历
java·jvm·后端·面试·性能优化·oom·内存泄露
FIN技术铺1 小时前
Spring Boot框架Starter组件整理
java·spring boot·后端
小曲程序1 小时前
vue3 封装request请求
java·前端·typescript·vue
凡人的AI工具箱1 小时前
15分钟学 Go 第 60 天 :综合项目展示 - 构建微服务电商平台(完整示例25000字)
开发语言·后端·微服务·架构·golang
陈王卜1 小时前
django+boostrap实现发布博客权限控制
java·前端·django