Java Feign Fallback类

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

Java Feign Fallback class

问题

以下是您提供的内容的翻译部分:

我正在使用SpringBoot版本1.5.9。
我不明白为什么我的降级类不起作用。
也许是我做错了什么?

我的Feign客户端:

@FeignClient(
        name = "prices",
        url = "${prices.url}",
        configuration = MyFeignConfig.class,
        fallbackFactory = FallbackClass.class
)
public interface PricesFeignClient {

    @GetMapping("/{userId}")
    PriceModel get(
            @PathVariable("userId") String userId
    );

}

这是降级类:

@Component
public class FallbackClass implements FallbackFactory<PricesFeignClient> {

    @Override
    public PricesFeignClient create(Throwable cause) {
        return new PricesFeignClient() {

            @Override
            public PriceModel get(String userId) {
                System.out.println("LALALA");
                return null;
            }
        };
    }
}

理论上,如果我的Feign客户端返回错误,我的降级方法应该起作用。
在这里,在文件中的***prices.url***中,我指定了错误的URL(模拟了我正在调用的远程服务不可用的情况)。我知道我的Feign客户端应该返回错误,并且应该调用降级类,在降级类中,我应该在控制台中收到消息:***"LALALA"***

但是这条消息没有出现在控制台上:我的降级类没有被调用。相反,我收到一个错误,表示未找到请求的资源。

请告诉我可能的问题是什么?我是否可能在某处出错了?

问题在于我现在正试图使我的降级类起作用。然后,我想在降级类中调用另一个Feign客户类,使用不同的URL,以便在我的主要服务不可用时起作用。
请告诉我,谢谢。

英文:

I am using SpringBoot version 1.5.9.
I can’t understand why my Fallback class doesn’t work out.
Maybe I'm doing something wrong?

My Feign client:

@FeignClient(
        name = &quot;prices&quot;,
        url = &quot;${prices.url}&quot;,
        configuration = MyFeignConfig.class,
        fallbackFactory = FallbackClass.class
)
public interface PricesFeignClient {

    @GetMapping(&quot;/{userId}&quot;)
    PriceModel get(
            @PathVariable(&quot;userId&quot;) String userId
    );

}

Here is the fallback class:

@Component
public class FallbackClass implements FallbackFactory&lt;PricesFeignClient&gt; {

    @Override
    public PricesFeignClient create(Throwable cause) {
        return new PricesFeignClient() {

            @Override
            public PriceModel get(String userId) {
                System.out.println(&quot;LALALA&quot;);
                return null;
            }
        };
    }
}

In theory, my fallback method should work out if my Feign client returns an error.
Here in the Feign client in the files in prices.url I specified the wrong URL (simulated the situation that my remote service to which I am making a call is unavailable). Knowing my Feign client should return with an error and the Fallback class should be called in which in the console I should receive the message: "LALALA".

This message is not in the console: my Fallback class is not being called. Instead, I get an error stating that the requested resource was not found.

Please tell me what could be the problem? Can I make a mistake somewhere?

The thing is that now I'm trying to get my Fallback class to work. And then I want to call another Fagnet class in the Fallback class with a different URL so that it works out if my main service is unavailable.
Tell me, please. thanks

答案1

得分: 0

我不得不将这些添加到依赖项中才能使其工作(还要记得在评论中提到插入 feign.hystrix.enabled: true

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>
英文:

I had to add to dependencies this for it to work (also don't forget to insert feign.hystrix.enabled: true as was said in the comments)

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-netflix-hystrix&lt;/artifactId&gt;
    &lt;version&gt;2.2.10.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年4月10日 04:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61129587.html
匿名

发表评论

匿名网友

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

确定