服务雪崩效应
复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免的失败.
正常情况下的访问 :
但是,当请求的服务中出现无法访问、异常、超时等问题时(图中的I),那么用户的请求将会 :
如果多个用户的请求中,都存在无法访问的服务,那么他们都将陷入阻塞的状态中。
多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微服务,这就是所谓的"扇出” ,。如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系充崩溃 “雪崩效应”.
对于高流量的应用来说,单一的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障。这些都表示需要对改障和延迟进行隔离和管理,以便单个依赖关系的失败,不能取消整个应用程序或系统。
Hystrix是一个用于处理处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时,异常等,Hystix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性.
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的,可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间,不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩
Hystrix能做到:服务降级,服务熔断,服务限流,接近实时的监控.
Hystrix断路器简介
hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用了对应的卡通形象做作为logo。
在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。
服务熔断
熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回"错误"的响应信息
。当检测到该节点微服务调用响应正常后恢复调用链路。
在SpringCloud框架里熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,当失败的调用到一定阈值,缺省是5秒内20次调用失败就会启动熔断机制。熔断机制的注解是@HystrixCommand。
服务提供者
我们写一个新的带服务熔断的服务提供者项目
microservice-book-provider-hystrix-1005
pom.xml
hystrix支持
<!--Hystrix相关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
全部pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cpc</groupId>
<artifactId>cpc-spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-book-consumer-hystrix-dashboard-90</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--Hystrix服务监控Dashboard依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml修改下端口和实例名称
server:
port: 1005
context-path: /
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cpc?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-book
eureka:
instance:
hostname: localhost
appname: microservice-book
instance-id: microservice-book:1005
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka2001.cpc.com:2001/eureka/,http://eureka2002.cpc.com:2002/eureka/,http://eureka2003.cpc.com:2003/eureka/
info:
groupId: com.cpc.testSpringcloud
artifactId: microservice-book-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://cpc.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
启动类添加注解 @EnableCircuitBreaker
:
package com.cpc.microservicebookproviderhystrix1005;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableCircuitBreaker //关键部分
@EntityScan("com.cpc.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceBookProviderHystrix1005Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceBookProviderHystrix1005Application.class, args);
}
}
4,服务提供者controller新增
@GetMapping("/hystrix")
@HystrixCommand(fallbackMethod="hystrixFallback")
public Map<String, Object> hystrix() throws InterruptedException {
Thread.sleep(2000);
Map<String,Object> map=new HashMap<String,Object>();
map.put("code", 200);
map.put("info","工号【"+port+"】正在为您服务");
return map;
}
public Map<String,Object> hystrixFallback() throws InterruptedException{
Map<String,Object> map=new HashMap<String,Object>();
map.put("code", 500);
map.put("info", "系统【"+port+"】繁忙,稍后重试");
return map;
}
这里我正常访问 返回的是 200 业务数据xxxxx 但是我们这里Thread.sleep(2000) 模拟超时;
这里的话 我们加上@HystrixCommand
注解 以及 fallbackMethod
表明这个方法我们再 没有异常以及没有超时(hystrix默认1秒算超时)的情况,才返回正常的业务数据;
否则,进入我们fallback指定的本地方法,我们搞的是500 系统出错,稍后重试,有效的解决雪崩效应,以及返回给用户界面很好的报错提示信息;
服务消费者
在消费者项目的 controller 中添加如下代码:
@Autowired //这是服务端的调用名
private final static String SERVER_IP_PORT = "http://MICROSERVICE-BOOK";
@Autowired
private RestTemplate restTemplate;
/**
* 测试Hystrix服务降级
* @return
*/
@GetMapping(value="/hystrix")
@ResponseBody
public Map<String,Object> hystrix(){
return restTemplate.getForObject(SERVER_IP_PORT+"/book/hystrix/", Map.class);
}
测试
先启动eureka,在启动 hystrix 对应的 provider, 然后启动消费者
请求 hystrix这个方法看看服务熔断效果
因为 Hystrix默认1算超时,所有 sleep了2秒 所以进入自定义fallback方法,防止服务雪崩;我们这里改sleep修改成100毫秒;
Hystrix默认超时时间设置
Hystrix默认超时时间是1秒,我们可以通过hystrix源码看到,
找到 hystrix-core.jar com.netflix.hystrix包下的HystrixCommandProperties类
default_executionTimeoutInMilliseconds属性局势默认的超时时间
默认1000毫秒 1秒,我们系统里假如要自定义设置hystrix的默认时间的话;application.yml配置文件加上
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
注意:这段配置idea居然没有提示功能,我比较郁闷;
我们把 sleep 设置成 2000,再次访问的时候测试结果:
Hystrix服务监控Dashboard
Hystrix服务监控Dashboard仪表盘Hystrix提供了 准实时的服务调用监控项目Dashboard,能够实时记录通过Hystrix发起的请求执行情况,可以通过图表的形式展现给用户看。
我们新建项目:microservice-book-consumer-hystrix-dashboard-90
加依赖:
<!--Hystrix服务监控Dashboard依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml配置
server:
port: 90
context-path: /
启动类一定要加上 @EnableHystrixDashboard
注解
package com.cpc.microservicebookconsumerhystrixdashboard90;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableHystrixDashboard
public class MicroserviceBookConsumerHystrixDashboard90Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceBookConsumerHystrixDashboard90Application.class, args);
}
}
这样就完事了。 我们启动项目,浏览器输入 : http://localhost:90/hystrix
嗯,实不相瞒这是一头豪猪,最上方的文本框是让你输入需要监控的地址,格式如下:
http://localhost:1005/hystrix.stream也就是ip地址加端口号/hystrix.stream
下方左边的输入框是指刷新信息的毫秒数,右边的是给监控的服务取个名字,我们就随便拿一个服务来测试一下,点击下方的按钮:
指标含义:
各种情况: