一、简介

在微服务架构中,我们会有这样的需求,A服务调用B服务,B服务调用C服务,ABC服务都需要用到当前用户上下文信息(userId、orgId等),那么如何实现呢?

方案一: 拦截器加上ThreadLocal实现,但是如果在这次请求中创建了一个新的线程就拿不到了,也就是无法跨线程传递数据。

方案二: 使用拦截器加上 HystrixRequestContext 这个 request level 的 context实现,即保存到HystrixRequestContext中的数据在整个请求中都能访问。

二、使用

2.1代码示例

首先需要在pom文件引入依赖hystrix

<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.12</version>
</dependency>

保存上下文信息的对象ServiceContextHolder

package cn.sp.context;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault; /**
* Created by 2YSP on 2019/7/28.
*/
public class ServiceContextHolder { private static final HystrixRequestVariableDefault<ServiceContext> context = new HystrixRequestVariableDefault<>(); public static ServiceContext getServiceContext() {
initServiceContext();
return context.get();
} public static void setServiceContext(ServiceContext serviceContext) {
initServiceContext();
context.set(serviceContext);
} private static void initServiceContext() {
if (!HystrixRequestContext.isCurrentThreadInitialized()) {
HystrixRequestContext.initializeContext();
}
} public static void destroy() {
if (HystrixRequestContext.isCurrentThreadInitialized()) {
HystrixRequestContext.getContextForCurrentThread().shutdown();
}
}
}

ServiceContextInterceptor的作用是将请求头中的userId保存到上下文对象中。

@Slf4j
public class ServiceContextInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
initServiceContext(request, request.getRequestURL().toString());
return true;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
ServiceContextHolder.destroy();
} private void initServiceContext(HttpServletRequest request, String url) {
ServiceContext serviceContext = new ServiceContext();
String userId = request.getHeader("userId");
serviceContext.setUserId(Long.valueOf(userId));
ServiceContextHolder.setServiceContext(serviceContext);
}
}

添加拦截器配置

@Configuration
@EnableWebMvc
@Import(value = {RestResponseBodyAdvice.class})
public class MvcConfig implements WebMvcConfigurer { @Bean
public ServiceContextInterceptor getServiceContextInterceptor() {
return new ServiceContextInterceptor();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getServiceContextInterceptor()).addPathPatterns("/request-context/**");
} }

用于测试的RequestContextTestController

@RestController
@RequestMapping("request-context")
@Slf4j
public class RequestContextTestController { @RequestMapping(value = "test", method = RequestMethod.GET)
public String test() {
System.out.println("请求的用户id:" + ServiceContextHolder.getServiceContext().getUserId() + ""); HystrixContextRunnable runnable =
new HystrixContextRunnable(() -> {
//从新的线程中获取当前用户id
ServiceContext context = ServiceContextHolder.getServiceContext();
System.out.println("新线程的用户id:" + context.getUserId());
context.setUserId(110L);
}); new Thread(runnable).start(); try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ServiceContextHolder.getServiceContext().getUserId() + "";
}
}

注意: 只有使用HystrixContextRunnable或HystrixContextCallable创建线程才能在线程间传递数据,JDK自带的是无效的。

2.2测试

使用postman发送请求

请求示例

请求头中的userId是22,返回结果却变成110,说明在新线程中改变了ServiceContextHolder中保存的userId。

控制台日志如下:

请求的用户id:22
2019-08-31 14:25:29.787 [http-nio-80-exec-1] WARN c.n.c.sources.URLConfigurationSource - No URLs will be polled as dynamic configuration sources.
2019-08-31 14:25:29.787 [http-nio-80-exec-1] INFO c.n.c.sources.URLConfigurationSource - To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-08-31 14:25:29.798 [http-nio-80-exec-1] INFO c.n.config.DynamicPropertyFactory - DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@a6f6807
新线程的用户id:22

说明新线程也能获取到ServiceContextHolder中的数据,这种又是怎么实现的呢?下面介绍原理。

三、原理

上下文信息其实是保存在HystrixRequestVariableDefault类型的变量中,所以先看看这个类的源码。

HystrixRequestVariableDefault是HystrixRequestVariable接口的实现类,HystrixRequestVariable接口表示request level的属性,仅提供了get()来获取属性。

public interface HystrixRequestVariable<T> extends HystrixRequestVariableLifecycle<T> {

    public T get();

}

HystrixRequestVariableDefault和ThreadLocal一样,提供了 T get() 和 set(T value) 两个工具方法。

public class HystrixRequestVariableDefault<T> implements HystrixRequestVariable<T> {
static final Logger logger = LoggerFactory.getLogger(HystrixRequestVariableDefault.class); @SuppressWarnings("unchecked")
public T get() {
if (HystrixRequestContext.getContextForCurrentThread() == null) {
throw new IllegalStateException(HystrixRequestContext.class.getSimpleName() + ".initializeContext() must be called at the beginning of each request before RequestVariable functionality can be used.");
}
// 拿到当前线程的存储结构,以自己为key索引数据
ConcurrentHashMap<HystrixRequestVariableDefault<?>, LazyInitializer<?>> variableMap = HystrixRequestContext.getContextForCurrentThread().state; // short-circuit the synchronized path below if we already have the value in the ConcurrentHashMap
LazyInitializer<?> v = variableMap.get(this);
...
} public void set(T value) {
// 拿到当前线程的存储结构,以自己为key来存储实际的数据。
HystrixRequestContext.getContextForCurrentThread().state.put(this, new LazyInitializer<T>(this, value));
} }

set/get方法都调用了HystrixRequestContext的方法完成的,HystrixRequestContext的部分源码如下:

public class HystrixRequestContext implements Closeable {

   //每个线程的ThreadLocal将保存HystrixRequestVariableState
private static ThreadLocal<HystrixRequestContext> requestVariables = new ThreadLocal<HystrixRequestContext>(); // 当前线程是否初始化了HystrixRequestContext
public static boolean isCurrentThreadInitialized() {
HystrixRequestContext context = requestVariables.get();
return context != null && context.state != null;
} // 从当前线程获取HystrixRequestContext
public static HystrixRequestContext getContextForCurrentThread() {
HystrixRequestContext context = requestVariables.get();
if (context != null && context.state != null) { return context;
} else {
return null;
}
} public static void setContextOnCurrentThread(HystrixRequestContext state) {
requestVariables.set(state);
} // 在每个请求开始的时候调用此方法,创建一个HystrixRequestContext,并与当前线程关联
public static HystrixRequestContext initializeContext() {
HystrixRequestContext state = new HystrixRequestContext();
requestVariables.set(state);
return state;
} ConcurrentHashMap<HystrixRequestVariableDefault<?>, HystrixRequestVariableDefault.LazyInitializer<?>> state = new ConcurrentHashMap<HystrixRequestVariableDefault<?>, HystrixRequestVariableDefault.LazyInitializer<?>>(); }

可以看出实际数据是存储在state这个ConcurrentHashMap中的,每个线程关联一个HystrixRequestContext,每个HystrixRequestContext有个Map结构存储数据,key就是HystrixRequestVariableDefault。

如何实现request level context?

HystrixContextRunnable源码如下:

// HystrixContextRunnable是个Runnable,一个可用于执行的任务
public class HystrixContextRunnable implements Runnable { private final Callable<Void> actual;
private final HystrixRequestContext parentThreadState; public HystrixContextRunnable(Runnable actual) {
this(HystrixPlugins.getInstance().getConcurrencyStrategy(), actual);
} public HystrixContextRunnable(HystrixConcurrencyStrategy concurrencyStrategy, final Runnable actual) {
// 获取当前线程的HystrixRequestContext
this(concurrencyStrategy, HystrixRequestContext.getContextForCurrentThread(), actual);
} // 关键的构造器
public HystrixContextRunnable(final HystrixConcurrencyStrategy concurrencyStrategy, final HystrixRequestContext hystrixRequestContext, final Runnable actual) { // 将原始任务Runnable包装成Callable, 创建了一个新的callable
this.actual = concurrencyStrategy.wrapCallable(new Callable<Void>() {
@Override
public Void call() throws Exception {
actual.run();
return null;
}
});
// 存储当前线程的hystrixRequestContext
this.parentThreadState = hystrixRequestContext;
} @Override
public void run() {
// 运行实际的Runnable之前先保存当前线程已有的HystrixRequestContext
HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread();
try {
// 设置当前线程的HystrixRequestContext,来自上一级线程,因此两个线程是同一个HystrixRequestContext
HystrixRequestContext.setContextOnCurrentThread(parentThreadState);
try {
actual.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
} finally {
// 还原当前线程的HystrixRequestContext
HystrixRequestContext.setContextOnCurrentThread(existingState);
}
}
}

代码地址

HystrixRequestContext实现Request级别的上下文的更多相关文章

  1. Flask Markup 上下文,request

    在模板渲染中,使用Markup转换变量中的特殊字符 from flask import Markup Markup函数对字符串进行转移处理再传递给render_template()函数 在浏览器中显示 ...

  2. Flask 上下文管理-- (session,request,current_app的传递)--类似本地线程实现,以及多app应用

    Flask session,request,current_app的传递 请求上下文的作用 -- 封装请求相关得数据(request,session) 请求上下文 request session re ...

  3. Flask 上下文(Context)原理解析

    :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...

  4. 第三节:EF Core上下文DbContext相关配置和生命周期

    一. 配置相关 1. 数据库连接字符串的写法 (1).账号密码:Server=localhost;Database=EFDB01;User ID=sa;Password=123456; (2).win ...

  5. python2.7高级编程 笔记一(Python中的with语句与上下文管理器学习总结)

    0.关于上下文管理器上下文管理器是可以在with语句中使用,拥有__enter__和__exit__方法的对象. with manager as var: do_something(var) 相当于以 ...

  6. Request中的各种方法

    前言 Request中方法众多,对于Java Web程序员来说,种种方法都会在工作中常常用到.Request由于不是JDK的一部分,这些方法的用法也没有专门的API可以查,所以在工作中遇到Reques ...

  7. 15.SpringMVC和Spring上下文关系(为什么SpringMVC可以调用到Spring)

    springmvc上下文继承于spring, 也就是springmvc的上下文可访问spring上下文,在springmvc的上下文中可取得spring bean. spring上下文是spring启 ...

  8. spring和springMVC的上下文

    上下文可以替代注解, 但是注解更方便 package com.tgb.web.controller; import javax.annotation.Resource; import javax.se ...

  9. request 的介绍使用属性

    上下文:相当于一个容器,保存了 Flask 程序运行过程中的一些信息. Flask中有两种上下文,请求上下文和应用上下文 请求上下文(request context) 在 flask 中,可以直接在视 ...

随机推荐

  1. CTF SHOW WEB_AK赛

    CTF SHOW平台的WEB AK赛: 签到_观己 <?php ​ if(isset($_GET['file'])){ $file = $_GET['file']; if(preg_match( ...

  2. [SQL Server]多次为 '派生表' 指定了列 'id'

    问题: 原因: 因为派生表oo中出现了两个同样的'ID'属性,所以会报[多次为 'o' 指定了列 'ID']的错误. 只需要把第二个星号替换成所需要的列名并把重复字段重命名就好了 解决方案:

  3. 从go-libp2p开始

    这里是从一系列关于libp2p的go实现教程开始,go-libp2p 我们会讲述go的安装,go模块的设置,启动libp2p节点,并在它们之间发送消息. 安装go go-libp2p推荐使用包含 mo ...

  4. vue 中 this.$options.data() 重置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 关于Redis分布式锁这一篇应该是讲的最好的了,先收藏起来再看!

    前言 在Java并发编程中,我们通常使用到synchronized .Lock这两个线程锁,Java中的锁,只能保证对同一个JVM中的线程有效.而在分布式集群环境,这个时候我们就需要使用到分布式锁. ...

  6. 状压DP复习笔记

    前言 复习笔记第4篇.CSP RP++. 引用部分为总结性内容. 0--P1433 吃奶酪 题目链接 luogu 题意 房间里放着 \(n\) 块奶酪,要把它们都吃掉,问至少要跑多少距离?一开始在 \ ...

  7. 数位DP复习笔记

    前言 复习笔记第五篇.(由于某些原因(见下),放到了第六篇后面更新)CSP-S RP++. luogu 的难度评级完全不对,所以换了顺序,换了别的题目.有点乱,见谅.要骂就骂洛谷吧,原因在T2处 由于 ...

  8. MySQL的验证方式

    mysql8之后root用户的密码验证方式修改了,mysql8的加密方式为caching_sha2_passoword,而navicat连接所用的方式为native_password. 使用命令mys ...

  9. 跨站点脚本编制 - SpringBoot配置XSS过滤器(基于mica-xss)

    1. 简介   XSS,即跨站脚本编制,英文为Cross Site Scripting.为了和CSS区分,命名为XSS.   XSS是最普遍的Web应用安全漏洞.这类漏洞能够使得攻击者嵌入恶意脚本代码 ...

  10. Shell脚本命令常用技巧

    如果一个命令只有一次输出,但想持续观察输出变化,使用watch -d -n1 'df -h'可行,df -h输出一次硬盘使用情况,用上面指令可以持续观察.-d表示相邻输出如果有差异要高亮标记,-n1表 ...