「整合系列」限流熔断(“Integrated series” current limiting fuse)

本篇主要内容是使用Sentinel给微服务加上限流熔断功能,防止异常情况拖垮应用服务。

Sentinel是面向分布式服务框架的轻量级流量控制框架,主要以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度来维护系统的稳定性。

在SpringCloud体系中,sentinel主要是为了替换原Hystrix的功能,与Hystrix相比,sentinel的隔离级别更加精细,提供的Dashboard可以在线更改限流熔断规则,而且使用也越加方便。

基础准备

要使用Sentinel提供的限流熔断能力,需要先做如下准备:

(1)安装Sentinel

Windows下安装sentinel-dashboard

(2)引入Sentinel

在需要配置限流熔断服务的POM文件中引入Sentinel组件:

<!--Sentinel-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

(3)自定义资源@SentinelResource

我们只需要在相关方法上加上@SentinelResource注解,让其可以成为sentinel识别的资源即可。如:

————————

The main content of this article is to use sentinel to add upper limit current fusing function to microservices to prevent abnormal conditions from collapsing application services.

Sentinel is a lightweight flow control framework for distributed service framework. It mainly takes flow as the starting point to maintain the stability of the system from multiple dimensions such as flow control, fuse degradation and system load protection.

In the spring cloud system, sentinel is mainly used to replace the functions of the original hystrix. Compared with hystrix, sentinel has a finer isolation level. The dashboard provided can change the current limiting and fusing rules online, and it is more convenient to use.

Basic preparation

To use the current limiting fusing capability provided by sentinel, the following preparations need to be made:

(1) Installing Sentinel

Windows下安装sentinel-dashboard

(2) Introduction of sentinel

Introduce sentinel component into POM file that needs to configure current limiting fuse service:

<!--Sentinel-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

(3) Custom resource @ sentinelresource

We only need to add @ sentinelresource annotation to the relevant methods to make them become the resources identified by sentinel. For example: