网站建设建站流程方案/百度客服电话24小时
前言:作者查阅了Sentinel官网、51CTO、CSDN、码农家园、博客园等很多技术文章都没有很准确的springmvc集成Sentinel的示例,因此整理了本文,主要介绍SpringMvc集成Sentinel
SpringMvc集成Sentinel
一、Sentinel 介绍
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
GitHub主页:https://github.com/alibaba/Sentinel
中文文档:https://sentinelguard.io/zh-cn/docs/introduction.html
控制台文档:https://sentinelguard.io/zh-cn/docs/dashboard.html
核心类解析:https://github.com/alibaba/Sentinel/wiki/Sentinel-核心类解析
Sentinel示例项目:https://github.com/alibaba/Sentinel/tree/master/sentinel-demo
https://github.com/alibaba/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/sentinel-example
二、Sentinel 基本概念
资源
资源是 Sentinel 的关键概念。它可以是 Java 应用程序中的任何内容,例如,由应用程序提供的服务,或由应用程序调用的其它应用提供的服务,甚至可以是一段代码。在接下来的文档中,我们都会用资源来描述代码块。
只要通过 Sentinel API 定义的代码,就是资源,能够被 Sentinel 保护起来。大部分情况下,可以使用方法签名,URL,甚至服务名称作为资源名来标示资源。
规则
围绕资源的实时状态设定的规则,可以包括流量控制规则、熔断降级规则以及系统保护规则。所有规则可以动态实时调整。
三、springMVC集成Sentinel
这里用的是spring
<spring.version>5.3.18</spring.version>
<servlet.api.version>2.5</servlet.api.version> <sentinel.version>1.8.6</sentinel.version>
1、springmvc项目引入依赖pom
<!-- 这是sentinel的核心依赖 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>${sentinel.version}</version></dependency><!-- 这是将自己项目和sentinel-dashboard打通的依赖 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>${sentinel.version}</version></dependency><!-- 这是使用sentinel对限流资源进行AOP --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-annotation-aspectj</artifactId><version>${sentinel.version}</version></dependency><!--这是使用sentinel适配Web Servlet的依赖--><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-web-servlet</artifactId><version>${sentinel.version}</version></dependency><!-- 这是使用sentinel热点参数限流功能依赖 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-parameter-flow-control</artifactId><version>${sentinel.version}</version></dependency>
2、添加配置文件
在application.properties下创建同级文件sentinel.properties,内容如下
# 集成到sentinel的项目名称
project.name=spring-sentinel-demo
# 对应的sentinel-dashboard地址
csp.sentinel.dashboard.server=localhost:8080
3、添加配置类引入配置文件
创建配置类SentinelAspectConfiguration并引入配置文件sentinel.properties
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;@Configuration
@PropertySources({@PropertySource("classpath:/sentinel.properties")})
public class SentinelAspectConfiguration {@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();}
}
4、web.xml新增如下过滤器配置
<filter><filter-name>SentinelCommonFilter</filter-name><filter-class>com.alibaba.csp.sentinel.adapter.servlet.CommonFilter</filter-class></filter><filter-mapping><filter-name>SentinelCommonFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
接入 filter 之后,所有访问的 Web URL 就会被自动统计为 Sentinel 的资源—来源于 https://sentinelguard.io/zh-cn/docs/open-source-framework-integrations.html
开源框架适配的Web 适配下的Web Servlet
5、创建Controller接口
@Controller
public class WebMvcTestController {@GetMapping("/hello")@ResponseBodypublic String apiHello(String id) {System.out.println("id = " + id);Date date = new Date();System.out.println("date = " + date);return "Hello!";}@GetMapping("/doBusiness")@ResponseBodypublic String doBusiness(String id) {System.out.println("doBusiness = ");return "Oops...";}}
6、下载控制台-即图形化实时监控平台
参考 https://sentinelguard.io/zh-cn/docs/dashboard.html Sentinel 控制台的下载和启动
访问 https://github.com/alibaba/Sentinel/releases/tag/1.8.6
点击下载
7、运行控制台
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
运行控制台后运行springmvc项目,然后访问某一个路径,就可以在控制台看到了,实时监控如果这个路径没有被访问是显示不出来的
这个时候我们配置限流为即Qps为2
8、测试限流
这里我们用postman进行压测,填写请求的路径保存。并点击run调出压测执行器
执行后查看结果如下
四、自定义异常返回
1、定义如下接口(这里只进行了常见的url定义方式的定义)
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;@Controller
@RequestMapping("/test")
public class WebMvcTestController {/*** 1.未配置流控规则 1000次请求没有间隔发起请求,应该在1秒中完成09:44:33* @param id* @return*/@RequestMapping("/RequestMapping")@ResponseBodypublic String RequestMapping(String id) {return "RequestMapping!";}@GetMapping("/GetMapping")@ResponseBodypublic String GetMapping(String id) {return "GetMapping...";}@PostMapping("/PostMapping")@ResponseBodypublic String PostMapping(String id) {return "PostMapping...";}/** 路径变量(Path Variables):使用花括号 {} 来标记路径中的变量,并通过 @PathVariable 注解将其绑定到方法参数上* */@GetMapping("/GetMapping/{id}")@ResponseBodypublic String apiFoo(@PathVariable("id") Long id) {return "Hello " + id;}/** Ant风格的路径匹配:* 使用 ? 表示任意单个字符,* * 表示任意多个字符(不包含路径分隔符 /),* ** 表示任意多个字符(包含路径分隔符 /)。* */@GetMapping("/Ant/*/{id}")@ResponseBodypublic String Ant(@PathVariable("id") Long id) {return "Ant " + id;}/** 正则表达式路径匹配:使用 @RequestMapping 注解的 value 属性结合正则表达式来定义请求路径。* */@GetMapping(value = "/users/{id:\\d+}")@ResponseBodypublic String pattern(@PathVariable("id") int id) {return "Ant " + id;}
}
2、自定义限流返回处理Handler
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.alibaba.fastjson.JSON;
import org.springframework.context.ApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.util.UrlPathHelper;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;public class SentinelExceptionHandler implements UrlBlockHandler {@Overridepublic void blocked(HttpServletRequest request, HttpServletResponse httpServletResponse, BlockException e) throws IOException {String contextPath = request.getContextPath();String servletPath = request.getServletPath();String requestURI = request.getRequestURI();String url = request.getRequestURL().toString();System.out.println("servletPath = " + servletPath);System.out.println("requestURI = " + requestURI);System.out.println("url = " + url);if (contextPath == null) {contextPath = "";}ApplicationContext controllerApplicationContext = LiteFlowApplicationContext.getControllerApplicationContext();RequestMappingHandlerMapping handlerMapping = controllerApplicationContext.getBean(RequestMappingHandlerMapping.class);// 查找匹配的处理方法Class<?> returnType = null;Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {RequestMappingInfo requestMappingInfo = entry.getKey();HandlerMethod handlerMethod = entry.getValue();//匹配urlif (requestMappingInfo.getPatternsCondition().getPatterns().contains(requestURI)) {System.out.println("Controller: " + handlerMethod.getBeanType().getName());System.out.println("Method: " + handlerMethod.getMethod().getName());System.out.println("getReturnType: " + handlerMethod.getMethod().getReturnType());returnType = handlerMethod.getMethod().getReturnType();break;}//匹配路径带参数的urlUrlPathHelper pathHelper = new UrlPathHelper();String lookupPath = pathHelper.getPathWithinApplication(request);PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();if (patternsCondition != null && patternsCondition.getMatchingPatterns(lookupPath).size() > 0) {System.out.println("Controller1111: " + handlerMethod.getBeanType().getName());System.out.println("Method111: " + handlerMethod.getMethod().getName());System.out.println("getReturnType: " + handlerMethod.getMethod().getReturnType());returnType = handlerMethod.getMethod().getReturnType();break;}}httpServletResponse.setContentType("application/json;charset=utf-8");ResponseData data = null;if (returnType != null) {if (returnType == String.class) {String str = "返回类型为字符串方法限流";httpServletResponse.getWriter().write(str);return;} else if (returnType == Integer.class) {httpServletResponse.getWriter().write(new Integer(1));return;} else if (returnType == Long.class) {httpServletResponse.getWriter().write(2);return;} else if (returnType == Double.class) {} else if (returnType == Boolean.class) {} else if (returnType == Float.class) {} else if (returnType == Byte.class) {}}//BlockException 异常接口,包含Sentinel的五个异常// FlowException 限流异常// DegradeException 降级异常// ParamFlowException 参数限流异常// AuthorityException 授权异常// SystemBlockException 系统负载异常if (e instanceof FlowException) {data = new ResponseData(-1, "流控规则被触发......");} else if (e instanceof DegradeException) {data = new ResponseData(-2, "降级规则被触发...");} else if (e instanceof AuthorityException) {data = new ResponseData(-3, "授权规则被触发...");} else if (e instanceof ParamFlowException) {data = new ResponseData(-4, "热点规则被触发...");} else if (e instanceof SystemBlockException) {data = new ResponseData(-5, "系统规则被触发...");}httpServletResponse.getWriter().write(JSON.toJSONString(data));}
}
3、使用自定义限流处理类
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import org.springframework.context.annotation.Configuration;@Configuration
public class SentinelConfig {public SentinelConfig() {WebCallbackManager.setUrlBlockHandler(new SentinelExceptionHandler());}
}
4、配置限流并测试结果
当开启限流后访问触发自定义UrlBlockHandler后结果如下
访问对应得url后控制台打印
servletPath = /test/RequestMapping
requestURI = /test/RequestMapping
url = http://localhost:8081/test/RequestMapping
Controller: org.example.WebMvcTestController
Method: RequestMapping
getReturnType: class java.lang.String
servletPath = /test/GetMapping
requestURI = /test/GetMapping
url = http://localhost:8081/test/GetMapping
Controller: org.example.WebMvcTestController
Method: GetMapping
getReturnType: class java.lang.String
servletPath = /test/PostMapping
requestURI = /test/PostMapping
url = http://localhost:8081/test/PostMapping
Controller: org.example.WebMvcTestController
Method: PostMapping
getReturnType: class java.lang.String
servletPath = /test/GetMapping/4
requestURI = /test/GetMapping/4
url = http://localhost:8081/test/GetMapping/4
Controller1111: org.example.WebMvcTestController
Method111: apiFoo
getReturnType: class java.lang.String
servletPath = /test/Ant/a/5
requestURI = /test/Ant/a/5
url = http://localhost:8081/test/Ant/a/5
Controller1111: org.example.WebMvcTestController
Method111: Ant
getReturnType: class java.lang.String
servletPath = /test/users/5
requestURI = /test/users/5
url = http://localhost:8081/test/users/5
Controller1111: org.example.WebMvcTestController
Method111: pattern
getReturnType: class java.lang.String
5、页面效果如下
五、springboot集成Sentinel
因为springboot集成文章较多,这里不多做赘述
Sentinel 与 Spring Boot/Spring Cloud 的整合见 Sentinel Spring Cloud Starter。
作者:京东健康 马仁喜
来源:京东云开发者社区 转载请注明来源
相关文章:

SpringMvc集成开源流量监控、限流、熔断降级、负载保护组件Sentinel | 京东云技术团队
前言:作者查阅了Sentinel官网、51CTO、CSDN、码农家园、博客园等很多技术文章都没有很准确的springmvc集成Sentinel的示例,因此整理了本文,主要介绍SpringMvc集成Sentinel SpringMvc集成Sentinel 一、Sentinel 介绍 随着微服务的流行&…...

[LeetCode] 12. 整数转罗马数字
12. 整数转罗马数字 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 …...

深入了解Java Period类,对时间段的精确控制
阅读建议 嗨,伙计!刷到这篇文章咱们就是有缘人,在阅读这篇文章前我有一些建议: 本篇文章大概2900多字,预计阅读时间长需要3分钟。本篇文章的实战性、理论性较强,是一篇质量分数较高的技术干货文章&#x…...

企业软件的分类有哪些|app小程序定制开发
企业软件的分类有哪些|app小程序定制开发 企业软件是指为了满足企业运营和管理需求而开发的软件系统。根据不同的功能和应用领域,企业软件可以分为以下几个分类: 1. 企业资源计划(Enterprise Resource Planning,ERP)软…...

选择更好的Notes索引附件方式
大家好,才是真的好。 首先介绍最近产品更新消息。在上一周,HCL主要发布了以下几个产品更新:HCL Verse 3.2.0、HCL Volt MX Go 2.0.2、HCL Domino Rest API 1.0.8。 HCL Verse是今后Domino的产品当中主要使用的webmail功能,这一次…...

Vue混淆与还原
Vue混淆与还原 引言 Vue是一种流行的JavaScript框架,用于构建用户界面。它简单易用且功能强大,备受开发者喜爱。然而,在传输和存储过程中,我们需要保护Vue代码的安全性。混淆是一种有效的保护措施,可以加密和压缩代码…...

R语言单因素方差分析+差异显著字母法标注+逐行详细解释
R语言单因素方差分析 代码如下 df <- read.csv("data.csv",header TRUE,row.names 1) library(reshape2) df <- melt(df,idc()) names(df) <- c(trt, val) df aov1 <- aov(val~trt,datadf) summary(aov1)library(agricolae) data <- LSD.test(aov…...

linux 消息队列apache-activemq服务的安装
1.下载 官网下载地址:https://activemq.apache.org/ 操作如下: 2. 解压 执行:tar -zxvf apache-activemq-5.18.3-bin.tar.gz -C /user/ 3. 进入目录 执行:cd /user/apache-activemq-5.18.3 4.修改配置文件 执行࿱…...

前端数据加密相关问题
什么是数据加密? 数据加密是一种安全性技术,通过使用算法将明文转换为密文,以保护数据的保密性和完整性。数据加密通常用于保护机密信息,例如个人身份、财务信息、企业机密等。在数据传输过程中,数据可以被拦截或窃听…...

Vue3中reactive和ref对比
Vue3中reactive和ref对比 数据角度对比原理角度对比使用角度对比 数据角度对比 ref用来定义: 基本类型reactive用来定义: 对象(或数组)类型数据备注:ref也可以定义对象或数组类型数据,它内部会调用reactive转为代理对象. 原理角度对比 ref通过Object.defineProperty()中的ge…...

【尘缘送书第五期】Java程序员:学习与使用多线程
目录 1 多线程对于Java的意义2 为什么Java工程师必须掌握多线程3 Java多线程使用方式4 如何学好Java多线程5 参与方式 摘要:互联网的每一个角落,无论是大型电商平台的秒杀活动,社交平台的实时消息推送,还是在线视频平台的流量洪峰…...

Linux C语言 34-库封装操作
Linux C语言 34-库封装操作 本节关键字:C语言 封装库 动态库 相关C库函数: 什么是库? 库是现成的,可以复用的代码。从本质上看,库是一种可执行代码的二进制形式,可以被操作系统载入内存执行。在C语言的编…...

JavaWeb-Tomcat
1. Web服务器 web服务器由硬件和软件组成: 硬件:计算机系统软件:计算机上安装的服务器软件,安装后可以为web应用提供网络服务。 常见的JavaWeb服务器: Tomcat(Apache):应用最广泛的…...

k8s之Pod常用命令详解、镜像拉取策略(imagePullPolicy)
常用命令 kubectl api-resources #查询可操作的资源对象列表kubectl get pod #查看默认命名空间下所有pod kubectl describe pod podname #获取默认命名空间下POD详情# 如果要查看制定命名空间则使用 -n nsname kubectl get pod -n ns kubectl describe pod podname -n ns# 以Y…...

Spark低版本适配Celeborn
Spark-3.5版本以下使用Celeborn时,无法使用动态资源,对于低版本的Spark,Celeborn提供了patch。各版本patch如下 https://github.com/apache/incubator-celeborn/tree/main/assets/spark-patch 下载patch,这里下载spark-3版本&am…...

idea报错:Error:java: 不允许在使用 -release 时从系统模块 java.xml 导出程序包?
File->Settings, 把红框编译选中选项全部删除掉...

Vector Quantized Diffusion Model for Text-to-Image Synthesis
Vector Quantized Diffusion Model for Text-to-Image Synthesis Shuyang Gu, University of Science and Technology of China, Microsoft, CVPR2022, Cited: 340, Code, Paper 1. 前言 我们提出了用于文本到图像生成的矢量量化扩散(Vector Quantized Diffusion Model&…...

solidity实现ERC1155多代币标准
文章目录 1、NFT - 维基百科2、IERC1155MetadataURI3、IERC1155Receiver4、IERC11555、ERC11556、NFT11557、开源地址 1、NFT - 维基百科 ERC-1155 标准于2018年6月由Witek Radomski、Andrew Cooke、Philippe Castonguay、James Therien、Eric Binet及Ronan Sandford提出。此标…...

10、外观模式(Facade Pattern,不常用)
外观模式(Facade Pattern)也叫作门面模式,通过一个门面(Facade)向客户端提供一个访问系统的统一接口,客户端无须关心和知晓系统内部各子模块(系统)之间的复杂关系,其主要…...

<软考>软件设计师-3程序设计语言基础(总结)
(一) 程序设计语言概述 1 程序设计语言的基本概念 1-1 程序设计语言的目的 程序设计语言是为了书写计算机程序而人为设计的符号语言,用于对计算过程进行描述、组织和推导。 1-2 程序语言分类 低级语言 : 机器语言(计算机硬件只能识别0和1的指令序列)&…...

C/C++---------------LeetCode第278. 第一个错误的版本
第一个错误的版本 题目及要求二分查找 题目及要求 你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。 假设你…...

C语言三种循环输出9*9乘法表
解题思路: 1、外层循环控制1~9循环 2、内层控制循环的次数 比如: 1 * 1 1 循环一次 1 * 1 1 1 * 2 循环两次 依此类推 int i, j;printf("for 打印9*9乘法表\r\n");for(i 1; i <10; i) {for(j 1; j < i;j) {printf("%d * %d %d…...

IntelliJ IDEA 之初体验
文章目录 第一步:下载与安装 IntelliJ IDEA1)官网下载2)选择那种安装包3)开始下载4)解压 第二步:启动 IntelliJ IDEA第三步:创建第一个 Java 项目第四步:运行第一个 Java 程序1&…...

java中synchronized和Lock的区别是什么?
synchronized 和 Lock 都是 Java 中用于实现线程同步的机制,但它们在使用方式和功能上存在一些区别。 使用方式 synchronized: 是 Java 语言关键字,可用于方法或代码块。通过 synchronized 关键字实现的同步是隐式的,无需手动释…...

ESP32-Web-Server编程-通过 Base64 编码在网页中插入图片
ESP32-Web-Server编程-通过 Base64 编码在网页中插入图片 概述 不同于上节 ESP32-Web-Server编程-在网页中通过 src 直接插入图片,本节引入 Base64 编码来显示图片。 Base64 是一种用64个字符来编码表示任意二进制数据的方法。任何符号都可以转换成 Base64 字符集…...

聊一聊大模型 | 京东云技术团队
事情还得从ChatGPT说起。 2022年12月OpenAI发布了自然语言生成模型ChatGPT,一个可以基于用户输入文本自动生成回答的人工智能体。它有着赶超人类的自然对话程度以及逆天的学识。一时间引爆了整个人工智能界,各大巨头也纷纷跟进发布了自家的大模型&#…...

pandas空格及网页空格符NBSP替换处理
df3[动作一课程内容]df3[动作一课程内容].str.replace( ,) df3[动作一课程内容]df3[动作一课程内容].str.replace( ,) 截图中代码为python展示代码,由于网页空格符和常规空格符看起来大致相同,但却不能用常规空格替换解决...

智能优化算法应用:基于战争策略算法无线传感器网络(WSN)覆盖优化 - 附代码
智能优化算法应用:基于战争策略算法无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用:基于战争策略算法无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.战争策略算法4.实验参数设定5.算法结果6.参考…...

数据结构和算法-栈
数据结构和算法-栈 1. 栈的介绍 栈的介绍: 栈的英文为(stack)栈是一个先入后出的有序列表栈是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的一端,为变化的一端,称为栈顶,另一端为固…...

C#基础与进阶扩展合集-进阶篇(持续更新)
目录 本文分两篇,基础篇点击:C#基础与进阶扩展合集-基础篇 一、进阶 1、Predicate 2、设置C#语言版本 3、ListCollectionView过滤集合 4、值类型与引用类型 5、程序设置当前项目工作目录 6、获取App.config配置文件中的值 7、Linq常用语句 8、…...