服务容错和Hystrix
在微服务架构中,通常有多个服务层调用,当有一个服务层调用出现故障时,其他服务也跟着出现故障,我们把这种现象称为雪崩效应。
Spring Cloud Hystrix组件可以防止雪崩事件的发生,Hystrix可以常用的解决场景有:
- 服务降级
- 服务熔断
- 依赖隔离
- 监控(Hystrix Dashboard)
服务降级
服务降级规则:
- 有限处理核心服务,非核心服务不可拥或弱可用
- 通过HystrixCommand 注解指定
- fallbackMehtod(回退函数)中具体实现降级逻辑。
通过订单服务访问商品服务,当商品服务关闭时,发生熔断。
代码示例:
1、导入依赖:
<!-- Hystri熔断器依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2、在启动类添加注解@EnableCircuitBreaker
3、服务降级测试代码
- 在方法上添加注解
@HystrixCommand
- 注解中属性fallbackMethod指定返回方法
- 实现fallback()方法
- 可以直接在类上添加默认回调方法。
@RestController
@DefaultProperties(defaultFallback = "defaultFallback" )
public class HystrixController {
@HystrixCommand(fallbackMethod = "fallback")
@GetMapping("/getProductInfoList")
public String getProductInfoList() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForObject("http://127.0.0.1:8005/product/listForProduct",
Arrays.asList("157875196366160022"),
String.class);
}
private String fallback() {
return "太拥挤了,请稍后再试...";
}
private String defaultFallback() {
return "默认提示:太拥挤了,请稍后再试...";
}
}
如果服务访问时间比较长,并不是服务出现问题了,仍然导致服务降级,这时候可以进行时间设置。
方法上添加该注解内容
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
服务熔断
服务熔断中可以在方法上进行注解@HystrixCommand
配置
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"), // 设置熔断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), // 断路器的最小请求数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 断路器尝试连接的窗口休眠时间
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60") // 断路器打开的错误率
})
Feign中使用Hystrix
feign组件中已经依赖了Hystrix组件,我们就不需要再导入Hystrix组件。
1、需要在 Feign客户端进行如下配置:
feign:
hystrix:
enabled: true
2、 再请求服务的中进行配置
- 注解
@FeignClient
中配置属性fallback - 新建 类ProductClientFallback ** 实现ProductClient**
@FeignClient(name = "product",fallback = ProductClient.ProductClientFallback.class)
public interface ProductClient {
@PostMapping("/product/listForOrder")
List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList);
@PostMapping("/product/decreaseStock")
void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList);
@Component
static class ProductClientFallback implements ProductClient {
@Override
public List<ProductInfoOutput> listForOrder(List<String> productIdList) {
return null;
}
@Override
public void decreaseStock(List<DecreaseStockInput> decreaseStockInputList) {
}
}
}
监控(Hystrix Dashboard)
1、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>
如果其他组件没有导入下面依赖,也要加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、 启动类上添加注解@EnableHystrixDashboard
3、启动服务,访问:localhsot:8081/hystrix ,出现如下页面
如果页面进入不能访问
需要进行如下配置ServletRegistrationBean
类:
@Configuration
public class ServletRegistrationBeanConfig {
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream"); // 指定路径为 /hystrix.sream
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
当该服务发生请求时,会发生进行请求监控