【问题标题】:Java + Webflux + Resilience4j: Name of the method triggering the fallBack methodJava + Webflux + Resilience4j:触发fallBack方法的方法名
【发布时间】:2021-03-27 00:41:45
【问题描述】:

请提出关于 Java SpringBoot Webflux 与 Resilience4J(不是 Spring Cloud 断路器)的小问题。

我有以下直截了当:

    @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);
    }

还有一个相同的被调用者fallbackMethod,由所有调用者共享,如:

    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是从methodOne还是methodTwo调用的?
  • 正确,是的,请:)
  • 你能检查这个链接吗stackoverflow.com/questions/421280/…
  • 试过了,它没有给调用者(在我的情况下是methodOne,methodTwo),而是所有中间调用者(onError等:'()

标签: java resilience4j


【解决方案1】:

您可以尝试从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() {
...
}

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2021-02-05
    • 1970-01-01
    • 2011-05-28
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    相关资源
    最近更新 更多