Java + Webflux + Resilience4j: 触发fallBack方法的方法名

Java + Webflux + Resilience4j: Name of the method triggering the fallBack method

请问关于 Java Spring使用 Resilience4J(不是 Spring Cloud Circuit Breaker)启动 Webflux 的小问题。

我有以下直截了当的:

    @TimeLimiter(name = "methodOne", fallbackMethod = "fallbackMethod")
    public Mono<String> methodOne(@RequestParam(value = "name", defaultValue = "World") String name) {
        return WebClient.builder().baseUrl("http://localhost:8081/serviceBgreeting?name=" + name).build().get().retrieve().bodyToMono(String.class);
    }

    @TimeLimiter(name = "methodTwo", fallbackMethod = "fallbackMethod")
    public Mono<String> methodTwo(@RequestParam(value = "name", defaultValue = "World") String name) {
        return WebClient.builder().baseUrl("http://localhost:8082/anotherMethodTwo?name=" + name).build().get().retrieve().bodyToMono(String.class);
    }

和一个相同的被调用者回退方法,由所有调用者共享,如:

    public Mono<Object> fallBackMethod(Exception exception) {
        System.out.println("For debug purpose, I need to know what is the exact method falling back to this fallback method");
        return //the fallback thing needed;

检索两种方法 methodOnemethodTwo 中哪一种实际上是此回退的触发器的最佳方法是什么?

我有大约 1000 多个方法使用这种模式,所以我不能只创建 1000 多个后备方法。 (比如 methodOne 落到 fallbackOne,methodTwo 落到 fallbackTwo 等...,我不能)

我确信有一种聪明的方法可以获取触发回退的方法名称。

尝试了 StackWalker,它只提供(fallBackMethod、invoke、fallback、lambda$reactorOnErrorResume$1、onError 等...)但不提供触发 fallBack 的方法。

请提供一点帮助。

谢谢

你能尝试从 fallbackMethod 执行这个吗 我们正在收集正在执行的调用中可用方法的列表,并从堆栈跟踪中检查最近的调用者是否存在。

Method[] methods = this.getClass().getMethods();
StackTraceElement[] stackTrace = throwable.getStackTrace();
Optional<StackTraceElement> first = Arrays.stream(stackTrace)
                .filter(s -> Arrays.stream(methods).anyMatch(m -> m.getName().equalsIgnoreCase(s.getMethodName())))
                .findFirst();
log.error("Method name Starts here");
first.ifPresent(System.out::println);
log.error("Method name Ends here");

这是打印类似的内容

in.silentsudo.webapi.services.PaymentOptionService.availablePaymentOptions(PaymentOptionService.java:36)

我们可以从这里提取方法名称。

这是我的来电者的样子

@CircuitBreaker(
            name = "payment-option-service-circuit-breaker",
            fallbackMethod = "paymentOptionOnCircuitBreak"
)
public Map<String, List<PaymentOption>> availablePaymentOptions() {
...
}