pom
<!-- hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
1:单个方法降级
@HystrixCommand(fallbackMethod = "paymenInfo_TimeOutHandler", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") }) // @HystrixCommand //没有特别指明,则用统一配置 public String paymentInfo_TimeOut() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return "线程池: " + Thread.currentThread().getName() + " paymentInfo_TimeOut"; }
降级的方法
/** * 单个服务降级 * * @return */ public String paymenInfo_TimeOutHandler() { return "线程池: " + Thread.currentThread().getName() + " paymenInfo_TimeOutHandler"; }
2:全局服务降级
@Service @DefaultProperties(defaultFallback = "paymenInfo_GloableHandler") public class PaymentService {
@HystrixCommand //没有特别指明,则用统一配置 public String paymentInfo_TimeOut() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return "线程池: " + Thread.currentThread().getName() + " paymentInfo_TimeOut"; }
降级的方法
/** * 全局服务降级 * * @return */ public String paymenInfo_GloableHandler() { return "线程池: " + Thread.currentThread().getName() + " paymenInfo_GloableHandler"; }
备注:服务降级一般用在客户端