Spring Cloud Alibaba(三)Sentinel之熔断降级
Spring Cloud Alibaba(三)Sentinel之熔断降级
本项目演示如何使用 Sentinel 完成 Spring Cloud 应用的熔断降级调用。
Sentinel 是阿里巴巴开源的分布式系统的流量防卫组件,Sentinel 把流量作为切入点,从流量控制,熔断降级,系统负载保护等多个维度保护服务的稳定性。
OpenFeign是一款声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API,需要了解OpenFeign使用基础,可以参考cloud-feign示例源码。
本项目服务注册中心使用nacos,服务提供者使用Spring Cloud Alibaba(一) 如何使用nacos服务注册和发现创建的ali-nacos-provider服务
Sentinel介绍
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
- 1.丰富的应用场景
Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
- 2.完备的实时监控
Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 3.广泛的开源生态
Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
- 4.完善的 SPI 扩展点
Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
Sentinel 分为两个部分
- 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
- 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。
Sentinel主要特性

Sentinel开源生态

熔断降级
对调用链路中不稳定的资源进行熔断降级是保障高可用的重要措施之一。由于调用关系的复杂性,如果调用链路中的某个资源不稳定,最终会导致请求发生堆积。Sentinel 熔断降级会在调用链路中某个资源出现不稳定状态时(例如调用超时或异常比例升高),对这个资源的调用进行限制,让请求快速失败,避免影响到其它的资源而导致级联错误。当资源被降级后,在接下来的降级时间窗口之内,对该资源的调用都自动熔断(默认行为是抛出 DegradeException)
降级策略
- 平均响应时间 (DEGRADE_GRADE_RT):当 1s 内持续进入 5 个请求,对应时刻的平均响应时间(秒级)均超过阈值(count,以 ms 为单位),那么在接下的时间窗口(DegradeRule 中的 timeWindow,以 s 为单位)之内,对这个方法的调用都会自动地熔断(抛出 DegradeException)。
- 异常比例 (DEGRADE_GRADE_EXCEPTION_RATIO):当资源的每秒请求量 >= 5,并且每秒异常总数占通过量的比值超过阈值(DegradeRule 中的 count)之后,资源进入降级状态。
- 异常数 (DEGRADE_GRADE_EXCEPTION_COUNT):当资源近 1 分钟的异常数目超过阈值之后会进行熔断。
熔断降级代码实现
服务提供方
创建ali-nacos-provider项目
- 首先, 依赖nacos 注册中心
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
- 定义服务提供方接口
@RestController
@Slf4j
public class HelloController {
@GetMapping(value = "/hello/{str}", produces = "application/json")
public String hello(@PathVariable String str) {
log.info("-----------收到消费者请求-----------");
log.info("收到消费者传递的参数:" + str);
String result = "我是服务提供者,见到你很高兴==>" + str;
log.info("提供者返回结果:" + result);
return result;
}
}
服务提消费方
创建ali-nacos-sentinel-feign项目
1.首先,pom.xml添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
2.定义FeignClient,及其降级配置
- 定义FeignClient
package com.easy.ansFeign.service;
import com.easy.ansFeign.fallback.HelloServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "ali-nacos-provider", fallbackFactory = HelloServiceFallbackFactory.class)
public interface HelloService {
/**
* 调用服务提供方的输出接口.
*
* @param str 用户输入
* @return hello result
*/
@GetMapping("/hello/{str}")
String hello(@PathVariable("str") String str);
}
- 定义fallback 工厂,获取异常
@Component
public class HelloServiceFallbackFactory implements FallbackFactory<HelloServiceFallback> {
@Override
public HelloServiceFallback create(Throwable throwable) {
return new HelloServiceFallback(throwable);
}
}
- 定义具体的fallback 实现
public class HelloServiceFallback implements HelloService {
private Throwable throwable;
HelloServiceFallback(Throwable throwable) {
this.throwable = throwable;
}
/**
* 调用服务提供方的输出接口.
*
* @param str 用户输入
* @return
*/
@Override
public String hello(String str) {
return "服务调用失败,降级处理。异常信息:" + throwable.getMessage();
}
}
- 测试入口
@RestController
public class TestController {
@Autowired
private HelloService helloService;
@GetMapping("/hello-feign/{str}")
public String feign(@PathVariable String str) {
return helloService.hello(str);
}
}
使用示例
示例关联项目
在Spring Cloud Alibaba(一) 如何使用nacos服务注册和发现基础上,我们新建了ali-nacos-sentinel-feign项目,并调用ali-nacos-provider项目用作该示例的服务提供方,有以下二个项目做测试。
ali-nacos-provider:服务提供者,服务名:ali-nacos-provider,端口:9000
ali-nacos-sentinel-feign:服务消费者,服务名:ali-nacos-sentinel-feign,端口:9102
运行示例测试
首先要启动服务注册中心 nacos、ali-nacos-provider服务及ali-nacos-sentinel-feign服务
返回
我是服务提供者,见到你很高兴==>yuntian
表示我们的服务成功调用到了
- 关闭ali-nacos-provider服务,访问: http://localhost:9102/hello-feign/yuntian
返回
服务调用失败,降级处理。异常信息:com.netflix.client.ClientException: Load balancer does not have available server for client: ali-nacos-provider
表示执行了我们预定的回调,服务成功降级了。
资料
Spring Cloud Alibaba(三)Sentinel之熔断降级的更多相关文章
- Spring Cloud Alibaba:Sentinel实现熔断与限流
一.什么是Sentinel Sentinel,中文翻译为哨兵,是为微服务提供流量控制.熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩效应”,为微服务系统提供了稳 ...
- Spring Cloud Alibaba(10)---Sentinel控制台搭建+整合SpringCloudAlibaba
上一篇博客讲了Sentinel一些概念性的东西 Spring Cloud Alibaba(9)---Sentinel概述 这篇博客主要讲 Sentinel控制台搭建,和 整合SpringCloudAl ...
- Spring Cloud Alibaba整合Sentinel
Spring Cloud Alibaba 整合 Sentinel 一.需求 二.实现步骤 1.下载 sentinel dashboard 2.服务提供者和消费者引入sentinel依赖 3.配置控制台 ...
- Spring Cloud Alibaba(11)---Sentinel+Nacos持久化
Sentinel+Nacos持久化 有关Sentinel之前有写过两篇 Spring Cloud Alibaba(9)---Sentinel概述 Spring Cloud Alibaba(10)--- ...
- Spring Cloud alibaba网关 sentinel zuul 四 限流熔断
spring cloud alibaba 集成了 他内部开源的 Sentinel 熔断限流框架 Sentinel 介绍 官方网址 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentine ...
- Spring Cloud Alibaba 之 Sentinel 限流规则和控制台实例
这一节我们通过一个简单的实例,学习Sentinel的基本应用. 一.Sentinel 限流核心概念 在学习Sentinel的具体应用之前,我们先来了解一下Sentinel中两个核心的概念,资源和规则. ...
- Spring Cloud Alibaba 使用Sentinel实现接口限流
Sentinel是什么 Sentinel的官方标题是:分布式系统的流量防卫兵.从名字上来看,很容易就能猜到它是用来作服务稳定性保障的.对于服务稳定性保障组件,如果熟悉Spring Cloud的用户,第 ...
- Spring Cloud微服务如何实现熔断降级?
熔断限流概述 在基于Spring Cloud的微服务架构体系下,按照系统功能边界的不同划分,原先大而全的系统会被拆分为多个不同的微服务,而相应的微服务会提供一组功能关联的服务接口,并向系统中的其他微服 ...
- Spring Cloud Alibaba(9)---Sentinel概述
Sentinel概述 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel是面向分布式服务架构的流量控制组件,主要以流量为切入点,从限流.流量整形.熔断降级.系统自适应保护.热点防 ...
- Spring Cloud Alibaba整合Sentinel流控
前面我们都是直接通过集成sentinel的依赖,通过编码的方式配置规则等.对于集成到Spring Cloud中阿里已经有了一套开源框架spring-cloud-alibaba,就是用于将一系列的框架成 ...
随机推荐
- (七十五)c#Winform自定义控件-控件水印组件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (3)安装elastic6.1.3及插件kibana,x-pack,essql,head,bigdesk,cerebro,ik
6安装nginx 6.1安装nginx 安装 pcre,zlib,openssl,nginx 6.2生成web访问用户密码 htpasswd –c –b /usr/local/nginx/conf/p ...
- CSS3相关编码规范
一.CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等)2.大小(width, height, padding, margin) ...
- SpringBootSecurity学习(13)前后端分离版之JWT
JWT 使用 前面简单介绍了把默认的页面登录改为前后端分离的接口异步登录的方法,可以帮我们实现基本的前后端分离登录功能.但是这种基本的登录和前面的页面登录还有一个一样的地方,就是使用session和c ...
- Kafka 学习笔记之 Kafka0.11之console-producer/console-consumer
Kafka 学习笔记之 Kafka0.11之console-producer/console-consumer: 启动Zookeeper 启动Kafka0.11 创建一个新的Topic: ./kafk ...
- ELK 学习笔记之 elasticsearch 版本控制
版本控制: elasticsearch 版本控制: 内部版本控制 外部版本控制 内部版本控制: 内部版本会检查你提供的版本值和文档的版本值是否一致,如果不一致就报错,一致则可以更新. curl -XP ...
- 【MySQL】mysql5.7数据库的安装和配置
第一步:直接从官网下载安装包,.msi 可以直接点击安装..zip直接解压到目录,本人是C:\Program Files\MySQL\mysql-5.7 第二步:需要配置环境变量,我的电脑->属 ...
- 服务网关Spring Cloud Zuul
Spring Cloud Zuul 开发环境 idea 2019.1.2 jdk1.8.0_201 Spring Boot 2.1.9.RELEASE Spring Cloud Greenwich S ...
- display:none和visibility:hidden的区别?
css控制元素不可见的方法 { display: none; /* 不占据空间,无法点击 */ } /************************************************* ...
- 【TencentOS tiny】深度源码分析(4)——消息队列
消息队列 在前一篇文章中[TencentOS tiny学习]源码分析(3)--队列 我们描述了TencentOS tiny的队列实现,同时也点出了TencentOS tiny的队列是依赖于消息队列的, ...