Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Loss of ThreadLocal Information in OpenFeign with Resilience4j Circuit Breaker

Đang mở
#949 1 bình luận 2 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
java, spring
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu bằng cách đọc FeignCircuitBreakerInvocationHandler#asSupplier và Resilience4JCircuitBreakerFactory#configureExecutorService, đây là hai điểm mở rộng được mô tả. So sánh hành vi truyền context của chúng và làm rõ API được hỗ trợ trước khi triển khai bất kỳ thứ gì. Được xem là hoàn tất khi đã thống nhất được một cách tiếp cận để giữ lại thông tin ThreadLocal của request trong quá trình thực thi circuit breaker bất đồng bộ mà không làm rò rỉ thông tin đó giữa các task.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Java
Star
1.3k
Fork
835
Merge trung bình
23 giờ 57 phút
Pull request đã merge (30 ngày)
25

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của spring-cloud/spring-cloud-openfeign

Tất cả issue của spring-cloud/spring-cloud-openfeign

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.