【问题标题】:Feign Hystrix fallback not workingFeign Hystrix 后备不起作用
【发布时间】:2018-03-07 19:03:41
【问题描述】:

我有下面的 FeignClient:

@FeignClient(name="FooMS",fallback=CustomerFeign.CustomerFeignImpl.class)
public interface CustomerFeign {

    @RequestMapping(value="/bar/{phoneNo}")
    List<Long> getFriends(@PathVariable("phoneNo") Long phoneNo);


    class CustomerFeignImpl implements CustomerFeign{

        @Override
        public List<Long> getFriends(Long phoneNo) {
            return new ArrayList<Long>(108);
        }

    }

}

当 FooMS 实例关闭时,我收到 500 错误,而不是执行回退。为什么会这样?

【问题讨论】:

  • 你能分享你的堆栈跟踪吗?

标签: java spring-boot hystrix netflix-feign spring-cloud-feign


【解决方案1】:

谢谢rostlvan

我在下面概述了我的实现:

我使用的是 Spring Cloud 版本 2020.0.4,以下配置对我有用:

pom.xml,我有这些依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.9.RELEASE</version>
</dependency>

虽然我不确定我们是否需要同时拥有 openfeignhystrix 依赖项。有人可以验证!

在我的application.properties 我有feign.circuitbreaker.enabled=true

在我的主应用程序类中,我有

@SpringBootApplication
@EnableFeignClients
public class MySpringBootApplication{

   public static void main(String[] args) {
       SpringApplication.run(MySpringBootApplication.class, args);
   }

 }

最后,我的 Feign 客户端,后备和后备工厂:

UserServiceFeignClient.java

@FeignClient(name = "USER-SERVICE", fallbackFactory = UserServiceFallbackFactory.class)
public interface UserServiceFeignClient {
    
    @GetMapping("/api/users/{userId}")
    public ResponseEntity<User> getUser(@PathVariable String userId);
}   

UserServiceFeignClientFallback.java

public class UserServiceFeignClientFallback implements UserServiceFeignClient{
    
    @Override
    public ResponseEntity<User> getUser(String userId) {
        return ResponseEntity.ok().body(new User());
    }
}

还有,UserServiceFeignClientFallbackFactory.java

@Component
public class UserServiceFallbackFactory implements FallbackFactory<UserServiceFeignClientFallback>{

    @Override
    public UserServiceFeignClientFallback create(Throwable cause) {
        return new UserServiceFeignClientFallback();
    }
    
}

我自己也面临这个问题,直到我偶然发现@rostlvan的答案

【讨论】:

    【解决方案2】:

    这对我有用 2020.0.3:

    application.properties

    feign.circuitbreaker.enabled=true
    

    pom.xml

     <spring-cloud.version>2020.0.3</spring-cloud.version>
    

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        <version>2.2.9.RELEASE</version>
    </dependency>
    

    【讨论】:

    • 感谢@rostlvan,我能够得到这个工作。已发布我的实现作为答案。请检查一下。
    【解决方案3】:

    添加 @Component 和 feign.hystrix.enabled=true 效果很好

    【讨论】:

      【解决方案4】:

      将您的CustomerFeignImpl 标记为@Component 或从中创建一个@Bean

      【讨论】:

      • 谢谢。我还必须添加 feign.hystrix.enabled=true 属性。我发现我无法通过属性文件修改 hystrix 超时。即使我给,hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=50000 或 hystrix.command.getFriends.execution.isolation.thread.timeoutInMilliseconds=50000,带有 Feign 的 hystrix 仍然会在 1 秒内超时。知道如何解决这个问题吗?
      • 嗨,这个答案对最新版本的 cloud 2020.0.3 有效吗?我收到 java.net.ConnectException: Connection denied: no further information 错误。我有 feign.hystrix.enabled=true + interface @FeignClient(name="verbservice", fallback = VerbClientFallback.class) public interface VerbClient { ....} + 另一个类 @Component public class VerbClientFallback 实现 VerbClient { ...}
      • @KrisSwat 你能尝试使用feign.circuitbreaker.enabled=true 属性吗?这对我有用。
      猜你喜欢
      • 2017-08-22
      • 2017-02-22
      • 2015-06-13
      • 2019-08-18
      • 2021-05-02
      • 2018-06-21
      • 2017-12-27
      • 1970-01-01
      • 2020-11-30
      相关资源
      最近更新 更多