spring cloud: Hystrix(四):feign使用hystrix

@FeignClient支持回退的概念:fallback方法,这里有点类似于:@HystrixCommand(fallbackMethod = "notfindback")的fallbackMethod 方法。

fallback方法调用的是一个类.,feign也有:/health, /health.stream地址信息

http://192.168.1.4:7601/health

spring cloud: Hystrix(四):feign类似于hystrix的断容器功能:fallback-LMLPHP

1.首先要在配置件开启hystrix配置

#feign.hystrix
feign.hystrix.enabled=true

  

或者

feign:
hystrix:
enabled: true

  

2.在FeignClient客户端文件添加fallback

@FeignClient(name="spring-boot-user", fallback=HystrixClientFallback.class)
public interface UserFeignClient { // 两个坑:1. @GetMapping不支持 2. @PathVariable得设置value
@RequestMapping(value="/simple/{id}", method=RequestMethod.GET)
public User findById(@PathVariable("id") Long id); }

  

3.HystrixClientFallback友好文件(当feignClient地址不通是,默认返回本类信息)

@Component
public class HystrixClientFallback implements UserFeignClient{ @Override
public User findById(Long id) {
// TODO Auto-generated method stub
User user = new User();
user.setId(0L);
return user;
} }

  

4.controller调用

@RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable("id") Long id) {
return this.userFeignClient.findById(id);
} }

  

spring cloud: Hystrix(四):feign类似于hystrix的断容器功能:fallback-LMLPHP

04-26 14:30