天天看点

hystrix熔断

熔断操作放在服务提供层,是在类的方法上,而降级是在消费者的接口层面设置

1.加入jar包

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix</artifactId>
   <version>1.4.5.RELEASE</version>
</dependency>
           

2.启动器注解,支持熔断机制

@EnableCircuitBreaker

3.定义 @HystrixCommand(fallbackMethod = “errMethod”)

@RequestMapping("/placeOrder")
@HystrixCommand(fallbackMethod = "errMethod")
public String placeOrder(){
  // return orderService.placeOrder();
    throw new RuntimeException("模拟该程序发生了错误");
}
public String errMethod(){
  return "熔断成功";
}

           

4.当程序发生错误,熔断成功

hystrix熔断

继续阅读