Spring Cloud Alibaba学习笔记(十四)流控防护组件Sentinel

简介

Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量控制、熔断降级、系统自适应保护等多个维度来帮助用户保障微服务的稳定性。

Sentinel现在已经是Spring Cloud官方推荐的主流熔断降级方案,它是Spring Cloud Alibaba的成员之一。

Sentinel的优点

  • 轻量级,核心库无多余依赖,性能损耗小。
  • 方便接入,开源生态广泛。Sentinel 对 Dubbo、Spring Cloud、Web Servlet、gRPC 等常用框架提供适配模块,只需引入相应依赖并简单配置即可快速接入;同时针对自定义的场景 Sentinel 还提供低侵入性的注解资源定义方式,方便自定义接入。
  • 丰富的流量控制场景。Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,流控维度包括流控指标、流控效果(塑形)、调用关系、热点、集群等各种维度,针对系统维度也提供自适应的保护机制。
  • 易用的控制台,提供实时监控、机器发现、规则管理等能力。
  • 完善的扩展性设计,提供多样化的 SPI 接口,方便用户根据需求给 Sentinel 添加自定义的逻辑。

之前学习过Hystrix,对于二者区别,可以看看这篇文章:https://yq.aliyun.com/articles/633786/

资源和规则

资源是Sentinel的关键概念。它可以是 Java 应用程序中的任何内容,例如,由应用程序提供的服务,或由应用程序调用的其它应用提供的服务,甚至可以是一段代码。只要通过 Sentinel API 定义的代码,就是资源,能够被 Sentinel 保护起来。大部分情况下,可以使用方法签名,URL,甚至服务名称作为资源名来标示资源。

围绕资源的实时状态设定的规则,可以包括流量控制规则、熔断降级规则以及系统保护规则,所有规则可以动态实时调整。

使用Sentinel

首先下载Sentinel,地址:https://github.com/alibaba/Sentinel/releases,我这里下载了1.7.1版本,首先启动服务

启动Nacos

找到Nacos目录,启动Nacos

startup.cmd -m standalone

工程改造 

基于上一节的项目工程,POM引入Sentinel依赖

<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
	<version>2.1.0.RELEASE</version>
</dependency>

修改配置文件

server:
  port: 9001
spring:
  application:
    name: service-provider-a
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
      discovery:
        server-addr: 127.0.0.1:8848

    sentinel:
      transport:
        dashboard: localhost:8080
      eager: true   #取消控制台懒加载
management:
  endpoints:
    web:
      exposure:
        include: '*'

提供一个Controller web接口sayHi

package org.dothwinds.serviceprovider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class SpringCloudStudyServiceProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudStudyServiceProviderApplication.class, args);
	}

	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}

	@Value(value = "${user.name}")
	private String name;

	@GetMapping("/sayHi")
	public String sayHi() {
		return "hi," + name + ", welcome to my world ";
	}

}

启动服务,Nacos里注册成功

 进入Sentinel dashboard,http://localhost:8080,用户名和密码都是sentinel

我们访问接口,http://localhost:9001/sayHi,可以成功访问到,多访问几次

查看Sentinel的实时监控,如下图

流控规则 

点击簇点链路,选择我们的sayHi接口,点击流控规则

针对sayHi接口新建流控规则

生效之后,我们测试一下接口,可以看到流控生效了

降级规则

如果RT(响应时间)超过5毫秒,那么10秒内(时间窗口)访问不了。RT最大配置时间4900ms,可以修改配置扩大这个值。

Web接口的代码里我们加入一个延迟,模拟响应时间长一些,启动服务

@GetMapping("/sayHi")
public String sayHi() throws Exception {
	Thread.sleep(1000);
	return "hi," + name + ", welcome to my world ";
}

 快速刷新服务,如下图,间隔5秒左右刷新还是这个错误,说明窗口时间也生效了

参考资料:

https://github.com/alibaba/Sentinel/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值