一、Sentinel 是什么?

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

Sentinel 具有以下特征:

  • 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。

  • 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。

  • 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。

  • 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

Sentinel 的主要特性:

Sentinel 的开源生态:

二、如何使用Sentinel

我们说的资源,可以是任何东西,服务,服务里的方法,甚至是一段代码。使用Sentinel 来进行资源保护,主要分为两个步骤:

  • 定义资源
  • 定义规则

先把可能需要保护的资源定义好,之后再配置规则。也可以理解为,只要有了资源,我们就可以在任何时候灵活地定义各种流量控制规则。在编码的时候,只需要考虑这个代码是否需要保护,如果需要保护,就将之定义为一个资源。
对于主流的框架,我们提供适配,只需要按照适配中的说明配置,Sentinel 就会默认定义提供的服务,方法等为资源。

三、示例

3.1、Sentinel编码限流

先来简单的体验下 Sentinel 吧,在你的Maven项目中增加 Sentinel 的依赖:

    <dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.4.0</version>
</dependency>

Sentinel中需要限流的称之为资源,对资源进行处理,下面来看最简单的一段代码:

package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit; import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; public class LimitByCoding {
public static void main(String[] args) throws InterruptedException {
initFlowRules();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
/* 您的业务逻辑 - 开始 */
System.out.println("hello world");
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
long startTime2 = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
Entry entry = null;
try {
entry = SphU.entry("HelloWorld");
/* 您的业务逻辑 - 开始 */
System.out.println("hello world");
/* 您的业务逻辑 - 结束 */
} catch (BlockException e1) {
/* 流控逻辑处理 - 开始 */
System.out.println("block!");
/* 流控逻辑处理 - 结束 */
} finally {
if (entry != null) {
entry.exit();
}
}
}
long endTime2 = System.currentTimeMillis();
System.out.println(endTime2 - startTime2);
} private static void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("HelloWorld");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Set limit QPS to 20.
rule.setCount(20);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}

第一行中初始化限流的规则,创建了一个资源叫 HelloWorld,设置了这个资源的QPS 为 20。

结果:

上面的例子中次数为1000次,只输出一句话,不做任何限制,执行完成的时间大概在9毫秒左右。

下面加上限流的逻辑,执行完成的时间基本上就超过91毫秒了,可见限流起了作用。

在业务开始前使用SphU.entry();方法标识开始,结束使用entry.exit();,如果触发了流控逻辑就会抛出BlockException异常让用户自行处理。

代码运行之后,我们可以在日志 ~/logs/csp/${appName}-metrics.log.xxx 里看到下面的输出:

    |--timestamp---|------date time--|-resource-|p |block|s  |e|rt

    1529998904000|2018-06-26 15:41:44|HelloWorld|20|0    |20|0|0

    1529998905000|2018-06-26 15:41:45|HelloWorld|20|5579 |20|0|728

    1529998906000|2018-06-26 15:41:46|HelloWorld|20|15698|20|0|0

    1529998907000|2018-06-26 15:41:47|HelloWorld|20|19262|20|0|0

    1529998908000|2018-06-26 15:41:48|HelloWorld|20|19502|20|0|0

    1529998909000|2018-06-26 15:41:49|HelloWorld|20|18386|20|0|0
  • p:通过的请求,

  • block:被阻止的请求

  • s:成功执行完成的请求个数

  • e:用户自定义的异常

  • rt:平均响应时长。

3.2、Sentinel注解限流

这种做法不好的地方在于每个需要限制的地方都得写代码,从 0.1.1 版本开始,Sentinel 提供了 @SentinelResource 注解的方式,非常方便。

要使用注解来保护资源需要引入下面的Maven依赖:

        <dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.4.0</version>
</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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.19.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.dxz</groupId>
<artifactId>LimitDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LimitDemo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

引入之后我们需要配置SentinelResourceAspect切面让其生效,因为是通过SentinelResourceAspect切面来实现的,我这边以Spring Boot中使用进行配置示列:

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect; @Configuration
public class AopConfiguration
{
@Bean
public SentinelResourceAspect sentinelResourceAspect()
{
return new SentinelResourceAspect();
} }

然后在需要限制的方法上加SentinelResource注解即可:

package com.example.demo.service;

import org.springframework.stereotype.Component;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.demo.ExceptionUtil; @Component
public class BuzService { @SentinelResource(value = "get", blockHandler = "exceptionHandler")
public String get(String id) {
return "http://cxytiandi.com";
} @SentinelResource(value = "get2", blockHandler = "handleException", blockHandlerClass = { ExceptionUtil.class })
public String get2() {
return "http://cxytiandi.com";
} public String exceptionHandler(String id, BlockException e) {
e.printStackTrace();
return "错误发生在" + id;
}
}

SentinelResource:value

表示资源名,必填项

SentinelResource:blockHandler

处理 BlockException 的方法名,可选项。若未配置,则将 BlockException 直接抛出。

  • blockHandler 函数访问范围需要是 public

  • 返回类型需要与原方法相匹配

  • 参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException

  • blockHandler 函数默认需要和原方法在同一个类中

如果你不想让异常处理方法跟业务方法在同一个类中,可以使用 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

业务方法:

    @SentinelResource(value = "get2", blockHandler = "handleException", blockHandlerClass = { ExceptionUtil.class })
public String get2() {
return "http://cxytiandi.com";
}

异常处理类:

package com.example.demo;

import com.alibaba.csp.sentinel.slots.block.BlockException;

public final class ExceptionUtil {
public static String handleException(BlockException ex) {
System.err.println("错误发生: " + ex.getClass().getCanonicalName());
return "error";
} }

如何测试?

我们可以在Spring Boot的启动类中定义规则,然后快速访问接口,就可以看出效果啦,或者用压力测试工具ab等。

package com.example.demo;

import java.util.ArrayList;
import java.util.List; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; @SpringBootApplication
public class LimitDemoApplication { public static void main(String[] args) {
initFlowRules();
SpringApplication.run(LimitDemoApplication.class, args);
} private static void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("get");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(1);
rules.add(rule); rule = new FlowRule();
rule.setResource("get2");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(1);
rules.add(rule); FlowRuleManager.loadRules(rules);
}
}

结果:

源码分析

只需要配置了SentinelResourceAspect就可以使用注解,我们来简单的看下SentinelResourceAspect的源码

    @Aspect
public class SentinelResourceAspect extends AbstractSentinelAspectSupport {
@Pointcut("@annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)")
public void sentinelResourceAnnotationPointcut() { } @Around("sentinelResourceAnnotationPointcut()")
public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable { // 获取当前访问的方法
Method originMethod = resolveMethod(pjp);
// 获取方法上的SentinelResource注解
SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class);
if (annotation == null) {
// Should not go through here.
throw new IllegalStateException("Wrong state for SentinelResource annotation");
} // 获取资源名
String resourceName = getResourceName(annotation.value(), originMethod);
EntryType entryType = annotation.entryType();
Entry entry = null;
try {
entry = SphU.entry(resourceName, entryType, 1, pjp.getArgs());
Object result = pjp.proceed();
return result;
} catch (BlockException ex) {
// 处理被限制的异常,回调事先配置的异常处理方法
return handleBlockException(pjp, annotation, ex);
} catch (Throwable ex) {
Tracer.trace(ex);
throw ex;
} finally {
if (entry != null) {
entry.exit();
}
}
}
}

上面是整个切面的代码,对所有加了SentinelResource注解的方法进去切入。细节代码在AbstractSentinelAspectSupport中,大家自己去看看。

Sentinel限流示例:编码和注解限流的更多相关文章

  1. JAVASE(十六) IO流 :File类、节点流、缓冲流、转换流、编码集、对象流

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.File类型 1.1.File类的理解 File类是在java.io包下 File可以理解成一个文件 ...

  2. 35 编码 ASCII Unicode UTF-8 ,字符串的编码、io流的编码

    * 编码表: * 信息在计算机上是用二进制表示的,这种表示法让人理解就很困难.为保证人类和设备,设备和计算机之间能进行正确的信息交换,人们编制的统一的信息交换代码,这就是ASCII码表 *ASCII ...

  3. (19)IO流之字符流FileReader和FileWriter,缓冲字符流---缓冲输入字符流BufferedReader和缓冲输出字符流BufferedWriter

    字符流,读取的文件是字符的时候,有两个基类一个是Reader,一个是Writer这有点拟人的感觉,人直接看懂的是文字 字符流 字节流:读取的是文件中的二进制字节流并不会帮你转换成看的懂得字符 字符流: ...

  4. 6.Sentinel源码分析—Sentinel是如何动态加载配置限流的?

    Sentinel源码解析系列: 1.Sentinel源码分析-FlowRuleManager加载规则做了什么? 2. Sentinel源码分析-Sentinel是如何进行流量统计的? 3. Senti ...

  5. 限流(三)Redis + lua分布式限流

    一.简介 1)分布式限流 如果是单实例项目,我们使用Guava这样的轻便又高性能的堆缓存来处理限流.但是当项目发展为多实例了以后呢?这时候我们就需要采用分布式限流的方式,分布式限流可以以redis + ...

  6. Sentinel 发布里程碑版本,添加集群流控功能

    自去年10月底发布GA版本后,Sentinel在近期发布了另一个里程碑版本v1.4(最新的版本号是v1.4.1),加入了开发者关注的集群流控功能. 集群流控简介 为什么要使用集群流控呢?假设我们希望给 ...

  7. Sentinel Dashboard(基于1.8.1)流控规则持久化到Nacos——涉及部分Sentinel Dashboard源码改造

    前言 之前虽然也一直在使用sentinel实现限流熔断功能,但却没有好好整理之前看的源码与资料,今天有时间将之前自己整理过的资料写成一篇博文,或者是是一篇关于Sentinel(基于目前最近版本1.8, ...

  8. Java IO_003.Reader与Writer--字符流以及编码对数据的操作(读取与写入)

    Java IO之Reader与Writer对象常用操作(包含了编码问题的处理) 涉及到文件(非文件夹)内容的操作,如果是纯文本的情况下,除了要用到File(见之前文章),另外就必须用到字符输入流或字符 ...

  9. IO流-File,字节流,缓冲流

    1.1 IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把 ...

随机推荐

  1. APUE学习笔记——4.2结构体 struct stat 及其相关函数介绍

    以下不少内容来自man手册 结构体struct stat         结构体struct stat用于保存文件相关的所有信息.         struct stat的基本成员如下所示 struc ...

  2. New Concept English there (1)Typing speed exercise

    Today,I start learn new concept english there,Mainly for listening practice and typing speed exercis ...

  3. (转)MapReduce Design Patterns(chapter 6 (part 2))(十二)

    Chain Folding 这是对job 链的一种优化.基本上是一种大体规则:每条记录都会提交给多个mapper,或者给reducer然后给mapper.这种综合处理方法会节省很多读文件和传输数据的时 ...

  4. 记录一下前端ajax实现增删改功能的步骤

    主要依赖三个按钮:新增,删除,编辑 新增:点击时创建新的LI或者TR并append到父级里,此时无需调动后台接口(如果新增需要弹窗输入val则可以调用): 删除:判断this是否有后台传过来的id值, ...

  5. LNMP架构基础搭建

    LNMP架构+wordpress博客 环境: centos6.7 2.6.32-573.el6.x86_64 nginx-1.6.3 mysql-5.5.49 php-5.3.27 wordpress ...

  6. 【剑指offer】输入一颗二叉树的根节点,判断是不是平衡二叉树,C++实现

    原创博文,转载请注明出处! # 题目 # 举例 # 思路 由平衡二叉树的定义可知,判断二叉树是否是平衡二叉树的关键在于判断任意结点是否是平衡结点.后序遍历二叉树,判断节点的子树是否平衡并计算节点的子树 ...

  7. rancher下的kubernetes之一:构建标准化vmware镜像

    学习kubernetes的时候,我们需要在kubernetes环境下实战操作,然而kubernetes环境安装并不容易,现在通过rancher可以简化安装过程,咱们来实战rancher下的kubern ...

  8. Jmeter简单的操作数据库

    mysql驱动包下载地址: https://dev.mysql.com/downloads/connector/j/ 1.添加驱动配置,把下载下来的驱动配置上去 2.添加‘配置元件-用户定义的变量’, ...

  9. HDU - 1142:A Walk Through the Forest (拓扑排序)

    Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...

  10. LaTex初学

    先用三句话来介绍什么是LaTeX:1.LaTeX是一类用于编辑和排版的软件,用于生成PDF文档.2.LaTeX编辑和排版的核心思想在于,通过\section和\paragraph等语句,规定了每一句话 ...