服务间调用出现故障,而这个服务又被其他服务调用,然后级联效应,又由于请求是同步的,导致服务发生雪崩效应
一、
二、作用
服务降级、依赖隔离、服务熔断、监控
服务降级:
- 优先核心服务,非核心服务不可用或若可用
- 通过HystrixCommand注解指定
- fallbackMethod(回退函数)中具体实现降级逻辑
、三、环境配置
<dependency> <!--触发服务降级-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//服务调用feign
@EnableCircuitBreaker//开启熔断
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
1、触发服务降级
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
//出现问题将回调fallbackMethod方法
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
@GetMapping("hytrix/testBreak")
@HystrixCommand(commandProperties = {
//超时时间超过将调用降级方法
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "100")
}) //这个逻辑出现问题,可以使用降级回调方案
public String testBreak() {
try {
int i = 10 / 0;
} catch (Exception e) {
throw new RuntimeException("出现异常,然后降级处理");
}
return "testBreak ok 啦";
}
//没有标注 @HystrixCommand 出现异常就不会降级
@GetMapping("hytrix/test")
public String test() {
int i = 10 / 0;
return "test 啦";
}
//对于上面的一种补偿方案
private String fallbackMethod() {
return "人数过多,请稍后再试!";
}
//默认一种补偿方案
private String defaultFallback() {
return "默认方案:人数过多,请稍后再试!";
}
}