Git address: https://github.com/Netflix/Hystrix
At present, hystrix has entered the maintenance mode, and the official recommendation is to use resilience4j
Important concepts (theory):
Service downgrade (fallback):
For example: the server is busy, please try again later, do not let the client wait, and immediately return a friendly prompt, fallback
Under what circumstances will a downgrade be issued: (return to a good-looking interface, friendly prompts, not garbled characters or the like)
1、程序运行异常
2、超时
3、服务熔断触发服务降级
4、线程池/信号量打满也会导致服务降级
Service break (break):
After the analog fuse reaches the highest access, access is directly denied, the switch is switched off, and the service downgrade method is called and a friendly prompt is returned
Service flow limit (flowlimit):
Seckill high concurrency and other operations, it is strictly forbidden to swarm over and crowd, everyone queues up, N per second, and proceed in an orderly manner
case code
Service downgrade code, first enable Hystrix, and then configure the fallback method, if an exception occurs, it will also enter the fallback method, fallbackMethod
Fuse case
It is equivalent to 10 visits within 10 seconds* (request window period setting), and if 60% of the requests fail, the fuse will be turned on and the switch will be turned on. That is, if 10 requests fail 6 times within 10 seconds, the fuse will be opened.
The fuse has three states
OPEN open, HREF OPEN half open, CLOSE closed
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"), /* Whether to enable the circuit breaker*/ @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), // Number of requests @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // Time window period @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"), // Trip after the failure rate reaches
Annotation parameters can go to this place to find related configuration
Current limiting case
Graphical interface:
pom.xml dependency (note: there must be spring-boot-starter-actuator dependency for graphical interface )
<dependencies>
<!--新增hystrix dashboard 仪表盘监控-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--图形化设置 要是要有Dashboard界面就一要-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 9001
Annotate the main startup class
@EnableHystrixDashboard
/**
* 注意:新版本Hystrix需要在主启动类中指定监控路径
* 此配置是为了服务监控而配置,与服务容错本身无关,spring cloud升级后的坑
* ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
* 只要在自己的项目里配置上下面的servlet就可以了
*
* @return ServletRegistrationBean
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
// 一启动就加载
registrationBean.setLoadOnStartup(1);
// 添加url
registrationBean.addUrlMappings("/hystrix.stream");
// 设置名称
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}