feign服务中调用,传递token
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})
- 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的更多相关文章
- SpringCloud初体验:三、Feign 服务间调用(FeignClient)、负载均衡(Ribbon)、容错/降级处理(Hystrix)
FeignOpenFeign Feign是一种声明式.模板化的HTTP客户端. 看了解释过后,可以理解为他是一种 客户端 配置实现的策略,它实现 服务间调用(FeignClient).负载均衡(Rib ...
- Spring Cloud Feign 服务消费调用(三)
序言 Spring Cloud Netflix的微服务都是以HTTP接口的形式暴露的,所以可以用Apache的HttpClient或Spring的RestTemplate去调用 而Feign是一个使用 ...
- 在Delphi开发的服务中调用指定应用程序
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://fxh7622.blog.51cto.com/63841/529033 在很多时候 ...
- Spring Cloud OAuth2.0 微服务中配置 Jwt Token 签名/验证
关于 Jwt Token 的签名与安全性前面已经做了几篇介绍,在 IdentityServer4 中定义了 Jwt Token 与 Reference Token 两种验证方式(https://www ...
- 代码书写C++ 中调用传递与指针传递根本区别
从概念上讲.指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变.而引用是一个别名,它在逻辑上不是独立的,它的存在具有依 ...
- 【多线程】java多线程Completablefuture 详解【在spring cloud微服务之间调用,防止接口超时的应用】【未完成】
参考地址:https://www.jianshu.com/p/6f3ee90ab7d3 示例: public static void main(String[] args) throws Interr ...
- DELPHI编写服务程序总结(在系统服务和桌面程序之间共享内存,在服务中使用COM组件)
DELPHI编写服务程序总结 一.服务程序和桌面程序的区别 Windows 2000/XP/2003等支持一种叫做“系统服务程序”的进程,系统服务和桌面程序的区别是:系统服务不用登陆系统即可运行:系统 ...
- Spring Cloud中Feign如何统一设置验证token
代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...
- SpringCloud系列——Feign 服务调用
前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调 ...
随机推荐
- Vue之 css3 样式重置 代码
reset.css @charset "utf-8";html{background-color:#fff;color:#000;font-size:12px} body,ul,o ...
- Luogu_P1613跑路
跳转链接 题目大意 题目中要求的是从1号点到n号点所需要的最短时间, 一秒可以走 \(2^k\) 个距离 给定的有向图的边边权都是1. 问题分析 由于一秒可以走 \(2^k\) 个距离,因此题目转化为 ...
- [NOI Online #1 提高组]
A 首先从 \(t = 2\) 的特殊部分分出发. 不难发现这个操作是很不直观的,于是可以考虑对于每个操作 \((u, v)\) 在 \(u, v\) 之间连一条无向边. 显然连通块之间要分开考虑,对 ...
- JAVA多线程学习六-守护线程
java中的守护程序线程是一个服务提供程序线程,它为用户线程提供服务. 它的生命依赖于用户线程,即当所有用户线程都死掉时,JVM会自动终止该线程. 有许多java守护程序线程自动运行,例如 gc,fi ...
- 【VUE】vue中遍历数组和对象
一.遍历对象 对象数据 cities:{ "A":[{ "id": 56, "spell": "aba", " ...
- java创建一个子类对象是会调用父类的构造方法会不会创建父类
1.子类在创建实例后,类初始化方法会调用父类的初始化方法(除了Java.lang.Object类,因为java.lang.Object类没有父类),而这种调用会逐级追述,直到java.lang.Obj ...
- pandas中常用的操作一
pandas中常用的功能: 1.显示所有的列的信息,999表示显示最大的列为999 pd.options.display.max_columns=999 2.读取excel时设置使用到列的名称,和列的 ...
- iOS 屏幕录制实现
iOS 屏幕录制实现 目录 iOS 屏幕录制实现 录屏API版本变化 App内部录制屏幕 录音麦克风声音 App内部录屏直播 Bonjour APP广播端实现 广播端App(直播平台)的实现 iOS1 ...
- Solution -「ROI 2019」「LOJ #3192」课桌
\(\mathcal{Description}\) Link. 原题意足够简洁啦.( \(\mathcal{Solution}\) 乍一看比较棘手,但可以从座位的安排方式入手,有结论: ...
- Note -「单位根反演」学习笔记
\(\mathcal{Preface}\) 单位根反演,顾名思义就是用单位根变换一类式子的形式.有关单位根的基本概念可见我的这篇博客. \(\mathcal{Formula}\) 单位根反演的 ...