在SpringBoot2.0中使用Sentinel如何实现动态限流

发布时间:2020-11-16 14:42:53 作者:Leah
来源:亿速云 阅读:768

这篇文章将为大家详细讲解有关在SpringBoot2.0中使用Sentinel如何实现动态限流,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

Sentinel 是什么?

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

Sentinel 具有以下特征:

Sentinel 的主要特性:

在SpringBoot2.0中使用Sentinel如何实现动态限流

Sentinel 的开源生态:

在SpringBoot2.0中使用Sentinel如何实现动态限流

Sentinel 分为两个部分:

控制台配置

Sentinel 控制台最少应该包含如下功能:

可以直接从[ release 页面](https://github.com/alibaba/Sentinel/releases " release 页面") 下载最新版本的控制台 jar 包,启动 Sentinel 控制台需要 JDK 版本为 1.8 及以上版本。。

启动脚本 sentinel.sh:

#!/bin/bash
java -Dsentinel.dashboard.auth.username=admin \
-Dsentinel.dashboard.auth.password=admin \
-Dserver.port=8084 -Dcsp.sentinel.dashboard.server=localhost:8084 \
-Dproject.name=sentinel-dashboard \
-jar sentinel-dashboard-1.6.3.jar &

用户可以通过如下参数进行配置:

在SpringBoot2.0中使用Sentinel如何实现动态限流

客户端配置

pom.xml 引入以下依赖:

 <!-- https://blog.52itstyle.vip -->
<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.1.5.RELEASE</version>
 <relativePath/>
</parent>
<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-sentinel</artifactId>
 </dependency>
</dependencies>
<dependencyManagement>
 <!--注意跟 SpringBoot 保持一致 2.1.x for Spring Boot 2.1.x-->
 <dependencies>
 <dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
 <version>2.1.0.RELEASE</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency>
 </dependencies>
</dependencyManagement>

配置文件:

# 应用名称 https://blog.52itstyle.vip
spring.application.name=blog
spring.cloud.sentinel.transport.port=8720
# 测试请替换为自己的地址
spring.cloud.sentinel.transport.dashboard=116.190.247.112:8084

这里的 spring.cloud.sentinel.transport.port端口配置会在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互。比如 Sentinel 控制台添加了1个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中。

代码配置:

/**
 * 博文 https://blog.52itstyle.vip
 */
@RequestMapping("{id}.shtml")
@SentinelResource("blogView")
public String page(@PathVariable("id") Long id, ModelMap model) {
 try{
 Blog blog = blogService.getById(id);
 String key = "blog_"+id;
 Long views = redisUtil.size(key);
 blog.setViews(views+blog.getViews());
 model.addAttribute("blog",blog);
 } catch (Throwable e) {
 return "error/404";
 }
 return "article";
}

@SentinelResource 注解用来标识资源是否被限流、降级。上述例子上该注解的属性 'blogView' 表示资源名。

默认情况,Sentinel 会拦截所有的 Controller 请求,这里标识资源名,是因为所有的文章都会走这个请求,为了方便统计和流控,这里自定义资源标识。

更多注解支持,请参考:Sentinel/wiki/注解支持。

访问客户端项目,随便点击几个页面,然后登录 Sentinel 控制台,如果看到以下界面,说明配置成功。

在SpringBoot2.0中使用Sentinel如何实现动态限流

在SpringBoot2.0中使用Sentinel如何实现动态限流

配置限流,搜索我们刚才配置的资源名称,选择流控功能。

在SpringBoot2.0中使用Sentinel如何实现动态限流

在SpringBoot2.0中使用Sentinel如何实现动态限流

输入阈值参数,为了测试方便,这里直接输入2,连续刷新浏览器,如果后台出现以下错误,并伴随着前台页面无法正常显示说明配置生效。

Caused by: com.alibaba.csp.sentinel.slots.block.flow.FlowException: null

当然,Sentinel 流程功能不仅仅这么简单,还支持集群模式,在终极版十万博文中,我们可以为集群中的节点,设置单机均分,也可以设置一个总体的阈值。

在SpringBoot2.0中使用Sentinel如何实现动态限流

生产环境中使用

Sentinel 核心库目前已可用于生产环境,目前除了阿里巴巴以外,也有多家企业在生产环境中使用它们。

规则管理及推送

原生版本的规则管理通过API 将规则推送至客户端并直接更新到内存中,并不能直接用于生产环境。

在SpringBoot2.0中使用Sentinel如何实现动态限流

不过 Sentinel提供了扩展读数据源ReadableDataSource,规则中心统一推送,客户端通过注册监听器的方式时刻监听变化,比如使用 Nacos、Zookeeper 等配置中心。这种方式有更好的实时性和一致性保证。

在SpringBoot2.0中使用Sentinel如何实现动态限流

监控

Sentinel 会记录资源访问的秒级数据(若没有访问则不进行记录)并保存在本地日志中。Sentinel 控制台可以通过 Sentinel 客户端预留的 HTTP API 从秒级监控日志中拉取监控数据,并进行聚合。

目前 Sentinel 控制台中监控数据聚合后直接存在内存中,未进行持久化,且仅保留最近 5 分钟的监控数据。若需要监控数据持久化的功能,可以自行扩展实现。

在SpringBoot2.0中使用Sentinel如何实现动态限流

注意事项

由于一开始没有认真读文档,把控制台部署到了外网,而客户端在内网启动,导致客户端无法被访问到,实时链路和簇点链路数据无法正常显示。

测试的小伙伴注意了,原始模式下,客户端和控制台必须相互被访问到,客户端会向控制台定时发送心跳请求,控制台会向客户端推送规则、拉取流控数据并聚合。

关于在SpringBoot2.0中使用Sentinel如何实现动态限流就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. spring cloud gateway整合sentinel实现网关限流的方法
  2. Spring Cloud Alibaba使用Sentinel实现接口限流

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot sentinel 动态限流

上一篇:vue项目中使用js-cookie如何实现存储token

下一篇:Mysql数据库中如何实现查询JSON函数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》