一、配置文件开启Hystrix
通过feign.hystrix.enabled=true开启Hystrix
feign:
hystrix:
enabled: true #开启熔断支持
ribbon:
ReadTimeout: 3000
SocketTimeout: 3000
ConnectTimeout: 3000
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10300
二、Fiegn接口熔断
一.fallback方式
@FeignClient(value = "user-server",fallback = UserFeignClientFallback.class)
public interface UserFeignClient {
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
User getById(@PathVariable("id")Long id);
}
@Component
public class UserFeignClientFallback implements UserFeignClient {
private Logger log = LoggerFactory.getLogger(UserFeignClientFallback.class);
@Override
public User getById(Long id) {
log.info("用户服务不可用");
return new User(-1l,"无此用户","用户服务不可用");
}
}
二.fallbackFactory方式
@FeignClient(value = "user-server",fallbackFactory = UserFeignClientFallbackFactory.class)
public interface UserFeignClient {
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
User getById(@PathVariable("id")Long id);
}
@Component
public class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient>{
@Override
public UserFeignClient create(Throwable throwable) {
return new UserFeignClient() {
@Override
public User getById(Long id) {
throwable.printStackTrace();
return new User(-1L,"无此用户","用户服务不可用");
}
};
}
}
|