假hystrix后备不起作用



i具有以下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错误,而不是执行后返回。为什么会发生?

CustomerFeignImpl标记为@Component或从中创建@Bean

这对我有用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!

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

我正在使用春季云版本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);
   }
 }

最后,我的假装客户,后备和后备工厂:

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的答案

相关内容

  • 没有找到相关文章

最新更新