Hystrix
服务可用性
复杂分布式体系结构中的服务具有多个依赖关系,每个依赖关系在某些时候都将不可避免地失败。如果主服务未能与这些外部故障隔离,则他们可能会收。
例如,对于依赖于 30 个服务的服务 A,其中每个服务的正常运行时间为 99.99%,服务 A 的最终可用性如下:
99.99^30 = 99.7%正常运行时间
十亿次请求中的 0.3%= 3,000,000 请求失败次数
每月平均 2 小时的服务宕机时间,即使所有依赖项都具有出色 99.99%的正常运行时间。
现实情况通常更糟。即使所有依赖项都表现良好,如果您没有为整个系统设计弹性,那么即使 0.01%停机时间对数十种服务中的每项服务的总体影响也相当于每月停机时间可能达到数小时。
分布式体系结构中服务之间的依赖问题
- 由业务原因在某一时刻,某服务并发请求骤增,导致该服务的延迟,响应过慢。
- 复杂分布式体系结构中的应用程序之间往往存在多层级联赖的关系,当 Provider 服务在某一时刻出现故障,如果 Consumer 服务如果没有与这些故障服务隔离,会导致 Consumer 服务也会出现请求堆积、资源占用、宕机不可用等问题,此时会引发系统服务间的雪崩效应,直到整个系统不可用。
综上所述,需要一种机制来处理服务延迟和服务故障引起雪崩问题,并保护整个系统处于可用稳定的状态。
Hystrix 设计目的
- 通过客户端库对延迟和故障进行保护和控制。
- 在一个复杂的分布式系统中终止级联故障。
- 快速失败和迅速恢复。
- 在合理的情况下回退和优雅的降级。
- 开启近乎于实时的监控、警告和操作控制。
传送门
入门案例
引入 Hystrix 的 Maven 依赖
1<!--Hystrix依赖-->
2 <dependency>
3 <groupId>org.springframework.cloud</groupId>
4 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
5 </dependency>
启动类添加注解 @EnableCircuitBreaker
@SpringCloudApplication
- @SpringBootApplication //SpringBoot 注解
- @EnableDiscoveryClient //注册服务中心 Eureka 注解
- @EnableCircuitBreaker //断路器注解
1@EnableFeignClients
2@SpringCloudApplication
3public class OrderServiceApplication {
4 public static void main(String[] args) {
5 SpringApplication.run(OrderServiceApplication.class, args);
6 }
7}
最外层 API 使用,好比异常处理(网络异常,参数或者内部调用问题)
1@RestController
2@RequestMapping("api/v1/order")
3public class OrderController {
4 @Autowired
5 private ProductOrderService productOrderService;
6
7 @RequestMapping("save")
8 @HystrixCommand(fallbackMethod = "saveOrderFail")
9 public Object save(@RequestParam("user_id")int userId, @RequestParam("product_id") int productId){
10 Map<String, Object> data = new HashMap<>();
11 data.put("code",0);
12 data.put("data",productOrderService.save(userId,productId));
13 return data;
14 }
15
16 //兜底数据
17 private Object saveOrderFail(int userId, int productId){
18 Map<String, Object> msg = new HashMap<>();
19 msg.put("code",-1);
20 msg.put("msg","抢购人数太多,稍后重试!"); //
21 return msg;
22 }
23}
正常访问
http://localhost:8781/api/v1/order/save?product_id=4&user_id=2
1{
2 "code": 0,
3 "data": {
4 "id": 0,
5 "productName": "\"电话 data from port=8771\"",
6 "tradeNo": "54354d6f-146f-49d1-acd1-b36b0e620888",
7 "price": 64345,
8 "createTime": "2019-07-01T05:38:37.650+0000",
9 "userId": 2,
10 "userName": null
11 }
12}
服务降级处理
http://localhost:8781/api/v1/order/save?product_id=4&user_id=2
1{
2 "msg": "抢购人数太多,稍后重试!",
3 "code": -1
4}
Feign 中使用断路器
Feign 集成了 Hysrix,需要引入以下依赖:
1<!--feign依赖-->
2 <dependency>
3 <groupId>org.springframework.cloud</groupId>
4 <artifactId>spring-cloud-starter-openfeign</artifactId>
5 </dependency>
6 <!--hystrix依赖-->
7 <dependency>
8 <groupId>org.springframework.cloud</groupId>
9 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
10 </dependency>
旧版本默认支持,新版本默认关闭,配置 feign 开启 hystrix 功能
1feign:
2 hystrix:
3 enabled: true
FeignClient 接口类中使用断路器
自定义异常类处理,并继承对应的 FeignClient 接口类
1/**
2 * 针对商品服务:错误降级处理
3 */
4@Component
5public class ProductClientFallback implements ProductClient {
6 @Override
7 public String findById(int id) {
8 System.out.println("feign client 调用product-service findbyid 异常");
9 return null;
10 }
11}
FeignClient 接口类中使用 fallback 属性指定异常处理类
1/**
2 * 商品服务客户端
3 */
4@FeignClient(name = "product-service", fallback = ProductClientFallback.class)
5public interface ProductClient {
6 @GetMapping("/api/v1/product/find")
7 String findById(@RequestParam(value = "id") int id);
8}
模拟故障 发现 order_service 项目的控制台打印如下信息:
1//访问 http://localhost:8781/api/v1/order/save?product_id=4&user_id=2
2feign client 调用product-service findbyid 异常
熔断、降级之异常报警通知
熔断有效的避免了分布式系统中雪崩效应的问题,而服务降级最大程度上以友好的方式保证了核心服务可用性。然后生成环境中我们需要及时准确的定位到故障的服务,因此在故障发生的第一时间需要有完善的通知机制用于第一时间发现问题并定位问题。
为了避免发生故障时,在同一时间有多个服务同时调用降级处理中的逻辑(通常是调用第三方短信服务接口)造成的资源浪费,这里引入了缓存标识位的概念,缓存可以使用 Redis。
引入 Redis 缓存依赖
1<!--springboot整合redis-->
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-data-redis</artifactId>
5 </dependency>
配置 Redis 信息
1spring:
2 redis:
3 database: 0
4 host: 192.168.31.210
5 port: 6379
6 timeout: 2000
创建 Redis 服务
1#拉取镜像
2docker pull redis
3#创建容器 windows查看端口的命令: netstat -aon|findstr "6379"
4docker run -id --name=redis -p 6379:6379 redis
5
6#使用redis-cli连接测试
7C:\Users\RyzenPlatform>d:
8D:\>cd D:\Development\Redis
9D:\Development\Redis>redis-cli.exe -h 192.168.31.210
10192.168.31.210:6379> set name abc
11OK
12192.168.31.210:6379> get name
13"abc"
在 fallback 中添加短信报警机制
1import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
2import net.xdclass.order_service.service.ProductOrderService;
3import org.apache.commons.lang.StringUtils;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.data.redis.core.StringRedisTemplate;
6import org.springframework.web.bind.annotation.RequestMapping;
7import org.springframework.web.bind.annotation.RequestParam;
8import org.springframework.web.bind.annotation.RestController;
9
10import javax.servlet.http.HttpServletRequest;
11import java.util.HashMap;
12import java.util.Map;
13import java.util.concurrent.TimeUnit;
14
15
16@RestController
17@RequestMapping("api/v1/order")
18public class OrderController {
19 @Autowired
20 private ProductOrderService productOrderService;
21
22 @Autowired
23 private StringRedisTemplate redisTemplate;
24
25 @RequestMapping("save")
26 @HystrixCommand(fallbackMethod = "saveOrderFail")
27 public Object save(@RequestParam("user_id")int userId, @RequestParam("product_id") int productId, HttpServletRequest request){
28 Map<String, Object> data = new HashMap<>();
29 data.put("code",0);
30 data.put("data",productOrderService.save(userId,productId));
31 return data;
32 }
33
34 //兜底数据
35 private Object saveOrderFail(int userId, int productId, HttpServletRequest request){
36 //监控报警标识位key
37 String saveOrderKey = "save-order";
38
39 //从redis查询监控报警标识位key
40 String sendValue = redisTemplate.opsForValue().get(saveOrderKey);
41
42 //从HttpServletRequest中获取请求这者的IP 和 本机服务IP
43 final String rip = request.getRemoteAddr();
44 final String lip = request.getLocalAddr();
45
46 //因为第三方短信接口响应速度比较慢,所以不能使用同步,同步会增加最终响应时间。
47 //因此这里使用异步任务,因为报警服务使用率并不高,所以这里没有采用以线程池的方式创建异步任务
48 new Thread(()->{
49 if(StringUtils.isBlank(sendValue)){
50 //发送一个http请求,调用短信服务。 TODO+
51 System.out.println("紧急短信,用户下单失败,请立刻查找原因!请求者ip地址是="+rip+",本机ip"+lip+
52 ",应用名称及URI:order-service:api/v1/order/save,依赖应用名称及URI:product-service:/api/v1/product/find");
53 //设置标识位
54 redisTemplate.opsForValue().set(saveOrderKey,"save-order-fail",20, TimeUnit.SECONDS);
55 }else {//redis不为空
56 System.out.println("已经发送过短信,20秒内不重复发送");
57 }
58 }).start();
59 Map<String, Object> msg = new HashMap<>();
60 msg.put("code",-1);
61 msg.put("msg","抢购人数太多,稍后重试!"); //
62 return msg;
63 }
64}
测试
http://192.168.31.230:8781/api/v1/order/save?product_id=4&user_id=2
1{
2 "msg": "抢购人数太多,稍后重试!",
3 "code": -1
4}
查看控制台
1feign client 调用product-service findbyid 异常
2紧急短信,用户下单失败,请立刻查找原因!请求者ip地址是=192.168.31.154,本机ip192.168.31.230,应用名称及URI:order-service:api/v1/order/save,依赖应用名称及URI:product-service:/api/v1/product/find
3
4feign client 调用product-service findbyid 异常
5已经发送过短信,20秒内不重复发送
6
7feign client 调用product-service findbyid 异常
8紧急短信,用户下单失败,请立刻查找原因!请求者ip地址是=192.168.31.154,本机ip192.168.31.230,应用名称及URI:order-service:api/v1/order/save,依赖应用名称及URI:product-service:/api/v1/product/find
降级超时策略调整
隔离策略
- THREAD 线程池隔离 (默认)
- SEMAPHORE 信号量 : 适用于接口并发量高的情况,如每秒数千次调用的情况,导致的线程开销过高,通常只适用于非网络调用,执行速度快
超时时间
- execution.isolation.thread.timeoutInMilliseconds (默认 1000 毫秒)
超时策略调整原则
最终超时时间是 hystrix 和 Feign 集成 robbin 的超时时间,默认取两者之中的最小值,Hystrix 的默认超时时间是 1 秒。在实际生产环境中,1 秒有些过短,通过会设置 5~10 秒左右,一般情况下 Ribbon 的时间应短于 Hystrix 超时时间。
Feign 集成 robbin 的超时时间配置
1feign:
2 hystrix:
3 enabled: true
4 client:
5 config:
6 default:
7 connectTimeout: 2000 #请求建立连接超时
8 readTimeout: 2000 #请求处理的超时
Hystrix 超时时间配置
1hystrix:
2 command:
3 default:
4 execution:
5 isolation:
6 thread:
7 timeoutInMilliseconds: 4000