Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Loss of ThreadLocal Information in OpenFeign with Resilience4j Circuit Breaker

未关闭
#949 1 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
java, spring
领域
api, backend

调研方向

首先阅读 FeignCircuitBreakerInvocationHandler#asSupplier 和 Resilience4JCircuitBreakerFactory#configureExecutorService,它们是所描述的两个扩展点。比较它们的上下文传播行为,并在实现任何内容之前明确受支持的 API。只有在就如何在异步 circuit breaker 执行期间保留请求 ThreadLocal 信息、同时避免其在任务之间泄漏这一点达成一致方案后,才算完成。

由索引模型根据 Issue 内容生成。

描述

enhancement help wanted

Is your feature request related to a problem? Please describe.

Issue Summary:
I encountered an issue while using Spring Cloud OpenFeign. When configuring circuit breaking (resilience4j), the FeignCircuitBreakerInvocationHandler submits our Feign client to be processed by an asynchronous thread, resulting in the loss of ThreadLocal information in the request context. Do you have any suggestions for resolving this issue?

Business Scenario:
In a Spring Web project, we have a custom RequestInterceptor that retrieves information stored in ThreadLocal before making a request. This information is used to populate RequestHeader and is then passed to downstream services. However, after introducing resilience4j as a circuit breaker in OpenFeign, resilience4j executes requests in a thread pool, causing ThreadLocal information to be lost.

Describe the solution you'd like

Proposal:
Therefore, I propose adding a hook (template method) within the org.springframework.cloud.openfeign.FeignCircuitBreakerInvocationHandler#asSupplier method in OpenFeign. This hook would allow the application service to retrieve the ThreadLocal object and, within the Supplier, set the ThreadLocal to the executing thread's ThreadLocal, similar to how it is done with RequestContextHolder. Is this approach feasible?

Below is sample code, assuming SubjectContext is a hook

private Supplier<Object> asSupplier(final Method method, final Object[] args) {
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    final Thread caller = Thread.currentThread();
    // Sample code
    **SubjectContext context = SubjectContext.getContext();**
    return () -> {
        boolean isAsync = caller != Thread.currentThread();
        try {
            if (isAsync) {
                RequestContextHolder.setRequestAttributes(requestAttributes);
                **SubjectContext.setContext(context);**
            }
            return dispatch.get(method).invoke(args);
        }
        catch (RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
        finally {
            if (isAsync) {
                RequestContextHolder.resetRequestAttributes();
                **SubjectContext.clear();**
            }
        }
    };
}

Describe alternatives you've considered

I have also considered using a hook provided by resilience4j during application startup, specifically within org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory#configureExecutorService. By passing a custom thread pool object here, I could potentially propagate the ThreadLocal information. Sample code is as follows:

@Configurable
@AllArgsConstructor
public class CircuitBreakerConfiguration implements ApplicationRunner {

    private final Resilience4JCircuitBreakerFactory factory;
    
    @Override
    public void run(ApplicationArguments args) throws Exception {

        ContextThreadPoolExecutor contextThreadPoolExecutor = 
                new ContextThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(1024));

        // **change ThreadPoolExecutor**
        factory.configureExecutorService(contextThreadPoolExecutor);
    }
    
    public static class ContextThreadPoolExecutor extends ThreadPoolExecutor {

        public ContextThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
            super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        }

        public void execute(Runnable command) {
            super.execute(wrap(command));
        }

        private static Runnable wrap(Runnable runnable) {
            **SubjectContext context = SubjectContext.getContext();**
            return () -> {
                **SubjectContext.setContext(context);**
                try {
                    runnable.run();
                } finally {
                    **SubjectContext.clear();**
                }
            };
        }
    }
}

However, I find it less elegant, and I believe resolving it within the asSupplier() method of OpenFeign would be more versatile and applicable to a broader range of scenarios.

Additional context
spring-cloud-openfeign-3.1.2

主要语言
Java
星标
1.3k
派生
835
平均合并
23 小时 57 分钟
30 天内合并 PR
25

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

spring-cloud/spring-cloud-openfeign 的其他 Issue

查看 spring-cloud/spring-cloud-openfeign 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。