问题描述
我正在尝试将我的断路器代码从 Hystrix 迁移到 Resilience4j。通信是在两个应用程序之间进行的,其中一个应用程序包含 Java 代码本身中的所有弹性 4j 配置,第二个应用程序作为微服务直接使用它。
有一个 RequestId 在微服务中生成并传播到工件上下文,并在该上下文中打印在日志中。使用 Hystrix,它运行得非常好,但自从我转向弹性后,我的请求 ID 为空。
以下是我的隔板和上下文传播器的配置:
ThreadPoolBulkheadConfig bulkheadConfig = ThreadPoolBulkheadConfig.custom()
.maxThreadPoolSize(maxThreadPoolSize)
.coreThreadPoolSize(coreThreadPoolSize)
.queueCapacity(queueCapacity)
.contextPropagator(new DummyContextPropagator())
.build();
// Bulk Head Registry
ThreadPoolBulkheadRegistry bulkheadRegistry = ThreadPoolBulkheadRegistry.of(bulkheadConfig);
// Create Bulk Head
ThreadPoolBulkhead bulkhead = bulkheadRegistry.bulkhead(name,bulkheadConfig);
虚拟上下文传播器:
public class DummyContextPropagator implements ContextPropagator {
private static final Logger log = LoggerFactory.getLogger( DummyContextPropagator.class);
@Override
public Supplier<Optional<Object>> retrieve() {
return () -> (Optional<Object>) get();
}
@Override
public Consumer<Optional<Object>> copy() {
return (t) -> t.ifPresent(e -> {
clear();
put(e);
});
}
@Override
public Consumer<Optional<Object>> clear() {
return (t) -> DummyContextHolder.clear();
}
public static class DummyContextHolder {
private static final ThreadLocal threadLocal = new ThreadLocal();
private DummyContextHolder() {
}
public static void put(Object context) {
if (threadLocal.get() != null) {
clear();
}
threadLocal.set(context);
}
public static void clear() {
if (threadLocal.get() != null) {
threadLocal.set(null);
threadLocal.remove();
}
}
public static Optional<Object> get() {
return Optional.ofNullable(threadLocal.get());
}
}
}
但是,似乎没有任何工作可以让我获得 RequestId。
我做的一切都正确还是有其他方法可以做到这一点?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)