多线程调用如何传递请求上下文?简述ThreadLocal和TaskDecorator

Spring线程池—TaskDecorator线程的装饰(跨线程传递ThreadLocal的方案) - 简书 (jianshu.com)icon-default.png?t=M85Bhttps://www.jianshu.com/p/4ec1f0b993cd

背景

微服务应用大多对外提供RESTful API,需要有相应的token才能访问,我们在聚合服务中使用Feign Client调用这些API,顺序执行往往会浪费大量的IO等待时间,为了提高查询速度,我们会使用异步调用,Java 8引入了CompletableFuture,结合Executor框架大大简化了异步编程的复杂性。

问题描述

我们的服务使用Spring Security OAuth2授权,并通过JWT传递token,对于用户侧的请求一律使用用户token,后端服务间的调用使用系统token。但有时为了简化开发(如用户信息传递),服务间调用仍然传递用户token。这就带来一个问题,在异步请求中子线程会丢失相应的认证信息,错误如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.oauth2ClientContext': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:365)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)
    at com.sun.proxy.$Proxy170.getAccessToken(Unknown Source)
    at org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.getToken(OAuth2FeignRequestInterceptor.java:126)
    at org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.extract(OAuth2FeignRequestInterceptor.java:115)
    at org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.apply(OAuth2FeignRequestInterceptor.java:104)
    at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:169)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:99)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
...
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:42)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:353)
    ... 76 common frames omitted

问题暴露出来了,该怎么解决呢?在这之前我们先了解两个概念,线程的ThreadLocal和TaskDecorator。

ThreadLocal<T>的原理

首先,在Thread类中定义了一个threadLocals,它是ThreadLocal.ThreadLocalMap对象的引用,默认值是null。ThreadLocal.ThreadLocalMap对象表示了一个以开放地址形式的散列表。当我们在线程的run方法中第一次调用ThreadLocal对象的get方法时,会为当前线程创建一个ThreadLocalMap对象。也就是每个线程都各自有一张独立的散列表,以ThreadLocal对象作为散列表的key,set方法中的值作为value(第一次调用get方法时,以initialValue方法的返回值作为value)。显然我们可以定义多个ThreadLocal对象,而我们一般将ThreadLocal对象定义为static类型或者外部类中。上面所表达的意思就是,相同的key在不同的散列表中的值必然是独立的,每个线程都是在各自的散列表中执行操作。

ThreadLocal存储

TaskDecorator简介

先看官方api文档说明

public interface TaskDecorator
A callback interface for a decorator to be applied to any Runnable about to be executed.
Note that such a decorator is not necessarily being applied to the user-supplied Runnable/Callable but rather to the actual execution callback (which may be a wrapper around the user-supplied task).
The primary use case is to set some execution context around the task's invocation, or to provide some monitoring/statistics for task execution.

意思就是说这是一个执行回调方法的装饰器,主要应用于传递上下文,或者提供任务的监控/统计信息。看上去正好可以应用于我们这种场景。

解决方案

上文中的错误信息涉及到RequestAttributes
和SecurityContext,他们都是通过ThreadLocal来保存线程数据,在同步方法中没有问题,使用线程池异步调用时,我们可以通过配合线程池的TaskDecorator装饰器拷贝上下文传递。

注意 线程池中的线程是可复用的,使用ThreadLocal需要注意内存泄露问题,所以线程执行完成后需要在finally方法中移除上下文对象。

代码如下

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskDecorator;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.annotation.Nonnull;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
    @Bean("ttlExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置线程池核心容量
        executor.setCorePoolSize(20);
        // 设置线程池最大容量
        executor.setMaxPoolSize(100);
        // 设置任务队列长度
        executor.setQueueCapacity(200);
        // 设置线程超时时间
        executor.setKeepAliveSeconds(60);
        // 设置线程名称前缀
        executor.setThreadNamePrefix("ttl-executor-");
        // 设置任务丢弃后的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 设置任务的装饰
        executor.setTaskDecorator(new ContextCopyingDecorator());
        executor.initialize();
        return executor;
    }

    static class ContextCopyingDecorator implements TaskDecorator {
        @Nonnull
        @Override
        public Runnable decorate(@Nonnull Runnable runnable) {
            RequestAttributes context = RequestContextHolder.currentRequestAttributes();
            SecurityContext securityContext = SecurityContextHolder.getContext();
            return () -> {
                try {
                    RequestContextHolder.setRequestAttributes(context);
                    SecurityContextHolder.setContext(securityContext);
                    runnable.run();
                } finally {
                    SecurityContextHolder.clearContext();
                    RequestContextHolder.resetRequestAttributes();
                }
            };
        }
    }
}

扩展知识

Spring安全策略可见性分为三个层级:

  1. MODE_THREADLOCAL 仅当前线程(默认)
  2. MODE_INHERITABLETHREADLOCAL 子线程可见
  3. MODE_GLOBAL 全局可见

可通过启动项参数进行设置

-Dspring.security.strategy=MODE_INHERITABLETHREADLOCAL

参考资料

ThreadPoolTaskExecutor在执行线程时,存在一个TaskDecorator配置,可以装饰线程类。

1. 源码分析

源码:org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#initializeExecutor

@Override protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {

    BlockingQueue < Runnable > queue = createQueue(this.queueCapacity);
    
    ThreadPoolExecutor executor;
    //配置了线程装饰器
    if (this.taskDecorator != null) {
        executor = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler) {@Override public void execute(Runnable command) {
                //装饰线程类
                Runnable decorated = taskDecorator.decorate(command);
                if (decorated != command) {
                    decoratedTaskMap.put(decorated, command);
                }
                //执行原有逻辑
                super.execute(decorated);
            }
        };
    } else {
        executor = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler);

    }

    if (this.allowCoreThreadTimeOut) {
        executor.allowCoreThreadTimeOut(true);
    }

    this.threadPoolExecutor = executor;
    return executor;
}

2. 实际使用

声明spring线程池时,需要指定TaskDecorator,在装饰代码中获取到ThreadLocal的值。

@Configuration
@EnableAsync
public class ThreadPoolConfig {

    @Bean("asyncServiceExecutor")
    public ThreadPoolTaskExecutor asyncRabbitTimeoutServiceExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        //核心线程数
        threadPoolTaskExecutor.setCorePoolSize(5);
        //核心线程若处于闲置状态的话,超过一定的时间(KeepAliveTime),就会销毁掉。
        threadPoolTaskExecutor.setAllowCoreThreadTimeOut(true);
        //最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(10);
        //配置队列大小
        threadPoolTaskExecutor.setQueueCapacity(300);
        //加入装饰器
        threadPoolTaskExecutor.setTaskDecorator(new ContextCopyingDecorator());
        //配置线程池前缀
        threadPoolTaskExecutor.setThreadNamePrefix("test-log-");
        //拒绝策略:只要线程池未关闭,该策略直接在调用者线程中串行运行被丢弃的任务,显然这样不会真的丢弃任务,但是可能会造成调用者性能急剧下降
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }

    @Slf4j
    static class ContextCopyingDecorator implements TaskDecorator {
        @Nonnull
        @Override
        public Runnable decorate(@Nonnull Runnable runnable) {
            //主流程
            String res = TestMappingController.local.get();
            log.info("装饰前:" + res);
            //子线程逻辑
            return () -> {
                try {
                    //将变量重新放入到run线程中。
                    TestMappingController.local.set(res);
                    log.info("打印日志-开始");
                    runnable.run();
                } finally {
                    log.info("打印日志-结束");
                }
            };
        }
    }
}

业务逻辑:

@PostMapping("test2") 
public void test() {
    //主线程执行
    local.set("主线程设置的ThreadLocal");
    //子线程执行
    executor.execute(() - >{
        String res = local.get();
        log.info("线程:" + res);
    });
}

执行结果:可以看到,在ContextCopyingDecorator中是主线程在调用,所以可以获取到主线程的ThreadLocal信息。

2020-11-17 18:54:25,027 INFO [1747069] [http-nio-8087-exec-5] [] (ThreadPoolConfig.java:53): 装饰前:主线程设置的ThreadLocal
2020-11-17 18:54:25,028 INFO [1747070] [test-log-4] [] (ThreadPoolConfig.java:59): 打印日志-开始
2020-11-17 18:54:25,028 INFO [1747070] [test-log-4] [] (TestMappingController.java:46): 线程:主线程设置的ThreadLocal
2020-11-17 18:54:25,028 INFO [1747070] [test-log-4] [] (ThreadPoolConfig.java:62): 打印日志-结束

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多线程环境中,我们可以使用ThreadLocal类来实现线程局部变量。ThreadLocal为每个线程提供了一个独立的存储空间,每个线程都可以独立地修改自己的副本,互不干扰。 要在多线程环境中使用ThreadLocal,我们需要执行以下步骤: 1. 创建ThreadLocal对象:通过创建ThreadLocal对象来保存线程的局部变量。例如,可以使用`ThreadLocal<Integer>`来保存一个整数类型的线程局部变量。 2. 设置线程局部变量:在每个线程中,通过调用ThreadLocal对象的`set()`方法来设置该线程的局部变量的值。例如,使用`threadLocal.set(value)`来设置线程局部变量的值为value。 3. 获取线程局部变量:在每个线程中,通过调用ThreadLocal对象的`get()`方法来获取该线程的局部变量的值。例如,使用`threadLocal.get()`来获取线程局部变量的值。 4. 移除线程局部变量(可选):如果需要,在每个线程结束时,可以通过调用ThreadLocal对象的`remove()`方法来移除该线程的局部变量。例如,使用`threadLocal.remove()`来移除线程局部变量。 下面是一个示例代码,演示如何在多线程环境中使用ThreadLocal: ```java public class ThreadLocalExample { private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>(); public static void main(String[] args) { Thread thread1 = new Thread(() -> { threadLocal.set(1); System.out.println("Thread 1: " + threadLocal.get()); threadLocal.remove(); }); Thread thread2 = new Thread(() -> { threadLocal.set(2); System.out.println("Thread 2: " + threadLocal.get()); threadLocal.remove(); }); thread1.start(); thread2.start(); } } ``` 这个示例中,我们创建了一个ThreadLocal对象来保存整数类型的线程局部变量。在每个线程中,我们使用`threadLocal.set()`方法来设置线程局部变量的值,并使用`threadLocal.get()`方法来获取线程局部变量的值。最后,我们调用`threadLocal.remove()`方法来移除线程局部变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值