默认spring-boot 微服务中 用feign来做服务间调用,是不会携带token传递的。为了能让服务间调用的时候带上token,需要进行配置,增强resTemplate
 

1、先实现请求拦截器

/**
* feign配置token
*/
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor { @Override
public void apply(RequestTemplate requestTemplate) {
// 这里可以添加feign请求的全局参数
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null || attributes.getRequest() == null) {
return;
}
HttpServletRequest request = attributes.getRequest();
requestTemplate.header("token", request.getHeader("token"));
requestTemplate.header("feignClient", "ifaas-hotel-robot-platform"); }
}
 

2.在@FeignClient接口里添加configuration = {FeignConfig.class}

@FeignClient(name="被调用的服务名", configuration={FeignRequestInterceptor .class})
由于feign的熔断器hystrix的隔离策略的原因,feign调用线程和主线程隔离了,请求上下文不共用,导致feign拦截器中 RequestContextHolder.getRequestAttributes()为空
 
原因:
此属性指示HystrixCommand.run()执行哪种隔离策略,是以下两种选择之一:
  • THREAD —它在单独的线程上执行,并发请求受线程池中线程数的限制
  • SEMAPHORE —它在调用线程上执行,并发请求受信号量限制
 

3、解决feign中RequestContextHolder.getRequestAttributes()为null方案有两种:

3.1、修改隔离策略:默认是 采用THREAD ,修改成SEMAPHORE 即可,但是不推荐这种做法,因为并发请求收到限制。

 

3.2、自定义feign的并发策略 继承HystrixConcurrencyStrategy,然后重写wrapCallable方法。如下:

import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import java.util.concurrent.Callable; /**
* Created by YangGuanRong
* date: 2022/3/8
*/
@Slf4j
@Primary
@Component
public class CustomFeignHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy { public CustomFeignHystrixConcurrencyStrategy() {
try { HystrixPlugins.getInstance().registerConcurrencyStrategy(this); } catch (Exception e) {
log.error("Failed to register Sleuth Hystrix Concurrency Strategy", e);
}
} @Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
return new WrappedCallable<>(callable, requestAttributes);
} static class WrappedCallable<T> implements Callable<T> {
private final Callable<T> target;
private final RequestAttributes requestAttributes; public WrappedCallable(Callable<T> target, RequestAttributes requestAttributes) {
this.target = target;
this.requestAttributes = requestAttributes;
} /**
* feign opens the fuse (hystrix): feign.hystrix.enabled=ture, and uses the default signal isolation level,
* The HttpServletRequest object is independent of each other in the parent thread and the child thread and is not shared.
* So the HttpServletRequest data of the parent thread used in the child thread is null,
* naturally it is impossible to obtain the token information of the request header In a multithreaded environment, call before the request, set the context before the call
*
* feign启用了hystrix,并且feign.hystrix.enabled=ture。采用了线程隔离策略。
* HttpServletRequest 请求在对象在父线程和子线程中相互独立,且不共享
* 所以父线程的 HttpServletRequest 在子线程中为空,
* 所以通常 在多线程环境中,在请求调用之前设置上下文
* @return T
* @throws Exception Exception
*/
@Override
public T call() throws Exception {
try {
// Set true to share the parent thread's HttpServletRequest object setting
RequestContextHolder.setRequestAttributes(requestAttributes, true);
return target.call();
} finally {
RequestContextHolder.resetRequestAttributes();
}
}
}
}

feign服务中调用,传递token的更多相关文章

  1. SpringCloud初体验:三、Feign 服务间调用(FeignClient)、负载均衡(Ribbon)、容错/降级处理(Hystrix)

    FeignOpenFeign Feign是一种声明式.模板化的HTTP客户端. 看了解释过后,可以理解为他是一种 客户端 配置实现的策略,它实现 服务间调用(FeignClient).负载均衡(Rib ...

  2. Spring Cloud Feign 服务消费调用(三)

    序言 Spring Cloud Netflix的微服务都是以HTTP接口的形式暴露的,所以可以用Apache的HttpClient或Spring的RestTemplate去调用 而Feign是一个使用 ...

  3. 在Delphi开发的服务中调用指定应用程序

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://fxh7622.blog.51cto.com/63841/529033 在很多时候 ...

  4. Spring Cloud OAuth2.0 微服务中配置 Jwt Token 签名/验证

    关于 Jwt Token 的签名与安全性前面已经做了几篇介绍,在 IdentityServer4 中定义了 Jwt Token 与 Reference Token 两种验证方式(https://www ...

  5. 代码书写C++ 中调用传递与指针传递根本区别

    从概念上讲.指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变.而引用是一个别名,它在逻辑上不是独立的,它的存在具有依 ...

  6. 【多线程】java多线程Completablefuture 详解【在spring cloud微服务之间调用,防止接口超时的应用】【未完成】

    参考地址:https://www.jianshu.com/p/6f3ee90ab7d3 示例: public static void main(String[] args) throws Interr ...

  7. DELPHI编写服务程序总结(在系统服务和桌面程序之间共享内存,在服务中使用COM组件)

    DELPHI编写服务程序总结 一.服务程序和桌面程序的区别 Windows 2000/XP/2003等支持一种叫做“系统服务程序”的进程,系统服务和桌面程序的区别是:系统服务不用登陆系统即可运行:系统 ...

  8. Spring Cloud中Feign如何统一设置验证token

    代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...

  9. SpringCloud系列——Feign 服务调用

    前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调 ...

随机推荐

  1. 计算机网络再次整理————tcp例子前奏[三]

    前言 简单编写一下tcp例子. 正文 我们常说IOS有7层,实际上也只有4层,或者这样说简单的说是4层. 首先是数据链路层,首先这一层解决了什么问题呢?为什么要有这一层呢? 首先要抛开有操作系统的意识 ...

  2. 蓝桥杯试题 基础练习 2n皇后问题以及n皇后问题

    在学习2n皇后之前,我们应该认识一下n皇后问题: 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于 ...

  3. 寻找写代码感觉(十六)之 集成Validation做参数校验

    写在前面 今天是大年初五了... 不知不觉,又要上班了,美好的假期只剩一天了,有点不舍呢! 也不知道为什么,总感觉像在做梦一样,像没睡醒一样,并不是因为眼睛小,更多应该是自寻烦恼,想得多罢了. 参数校 ...

  4. ApacheCN Linux 译文集(二) 20211206 更新

    CentOS7 Linux 服务器秘籍 零.前言 一.安装 CentOS 二.配置系统 三.管理系统 四.用 YUM 管理包 五.管理文件系统 六.提供安全性 七.构建网络 八.使用文件传输协议 九. ...

  5. 技术管理进阶——Leader应该关注成长慢的同学吗?

    原创不易,求分享.求一键三连 两个故事 我该怎么办? ​在大学毕业的时候,恩师跟我说了一个故事: 有一个女同学跟他说,不知道毕业了该干撒,不知道该怎么办. 正处于「低谷期」的恩师突然一怔,想到貌似自己 ...

  6. 微信h5下拉隐藏网页,还有取消页面滑动

    需求: 网页下拉太丑了,如下 度娘了一下, 发现一篇相关文档 基本解决了问题 https://juejin.cn/post/6844903940190896135#heading-2 加入如下代码即可 ...

  7. UITableViewCell结构

  8. oracle锁表问题处理

    文章转载自:http://blog.itpub.net/31397003/viewspace-2142672/ "ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或 ...

  9. ACM-ICPC 2015辽宁省赛

    省赛之于ACM 就是让省内的队伍互相比较而已~~~(何况弱省(本渣校  四个二等四个三等(其实是六个三道题 两个两道题,是院长后来和主办方沟通了下- - (本弱很水,但还是要吐槽:好水的省赛啊!!

  10. 7、Linux基础--权限、查看用户信息

    笔记 1.晨考 1.Linux系统中的文件"身份证号"是什么 index node 号码 2.什么是硬链接,什么是软连接 硬链接是文件的入口,软连接是快捷方式. 3.硬链接中保存的 ...