SpringCloud 在Feign上使用Hystrix(断路由)
第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中添加以下配置:
feign.hystrix.enabled=true.
第二步:在Feign的接口上添加Hystrix(断路由)
@FeignClient(name = "这里写服务名称", fallbackFactory = InsuranceCompany4OthersHystrixFactory.class)@RequestMapping("/basebusiness/insurancecompany/4others")public interface InsuranceCompany4OthersApi{
@RequestMapping("getAllInsuranceCompany") public RetDTO getAllInsuranceCompany();
}
第三步:编写InsuranceCompany4OthersHystrixFactory类,类上加@Component注解,注入Ioc容器中。
@Componentpublic class InsuranceCompany4OthersHystrixFactory implements FallbackFactory<InsuranceCompany4OthersApi>{
@Override public InsuranceCompany4OthersApi create(Throwable arg0) { return new InsuranceCompany4OthersApi() {
@Override public RetDTO getAllInsuranceCompany() {
RetDTO retDTO = new RetDTO(); retDTO.setRetStatus(RetDTO.SUCCESS); retDTO.setRetData("服务不通"); return retDTO;
}
};
}
}
第四部:测试。把你需要调用的服务挂掉,使用Feign调用服务,如果返回 “服务不通“ 则Hystrix(断路由)生效。
转载于:https://www.cnblogs.com/wuxiang/p/9007902.html
相关资源:垃圾分类数据集及代码