Resilience4J – 全局备用方法

huangapple go评论75阅读模式
英文:

Resilience4J - global fallback method

问题

关于与SpringBoot Starter结合使用的Resilience4j的问题。

在这里的文档中:https://resilience4j.readme.io/docs/getting-started-3
它提到:“如果多个方法具有相同的返回类型,并且您想一劳永逸地为它们定义相同的带有异常参数的全局回退方法。”

有人有例子吗?

我看了这个演示 https://github.com/resilience4j/resilience4j-spring-boot2-demo 并且看到了重复的方法

 private Flux<String> fluxFallback(Exception ex) {
        return Flux.just("Recovered: " + ex.toString());
    }

我有一个相同的情况,我有大约十二个共享同一个回退方法的类。一个默认的列表内容。

看了这个演示,我将不得不将这个默认列表的回退方法复制粘贴十二次。我相信肯定有更聪明的方法来做这个。只是我找不到。

有人有代码片段吗?

谢谢

英文:

Question regarding Resilience4j combined with SpringBoot starter.

In the doc here: https://resilience4j.readme.io/docs/getting-started-3
It mentions: "You can define one global fallback method with an exception parameter only if multiple methods has the same return type and you want to define the same fallback method for them once and for all."

Anyone has an example please?

I had a look at the demo https://github.com/resilience4j/resilience4j-spring-boot2-demo and could see duplicated methods

 private Flux&lt;String&gt; fluxFallback(Exception ex) {
        return Flux.just(&quot;Recovered: &quot; + ex.toString());
    }

I am having a same case, I have some twelve classes that all share one same fallback method. A default list of something.

Looking at the demo, I am going to copy paste this default list fallback twelve times. I am sure there is a smarter way to do this. Just that I could not find it.

Anyone has a snippet please?

Thank you

答案1

得分: 2

一个全局的回退方法仅适用于单个类。
目前还没有办法定义一个全局的回退方法,可以供多个类使用。

你只能将回退逻辑提取到一个单独的类中,这样就不必多次复制粘贴回退逻辑。

英文:

A global fallback method is only possible for a single class.
Currently there is no way to define a global fallback method which can be used by multiple classes.

You can only extra the fallback logic into a single class so that you don't have to copy&paste the fallback logic multiple times.

huangapple
  • 本文由 发表于 2020年10月22日 21:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64483513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定