1、环境准备
1.1Nacos
单机启动:startup.cmd -m standalone
1.2 Sentinel
启动命令:java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.0.jar
1.3 JMeter
2、流控规则限流
2.0 环境搭建
2.0.1 依赖
<!-- nacos 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- sentinel 流量防卫依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- 暴露/actuator/sentinel端点 单独配置,management开头 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.0.2 application.yml
# 端口
server:
port: 9604
# 服务名
spring:
application:
name: kgcmall-sentinel
# 数据源配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/kh96_alibaba_kgcmalldb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
username: root
password: 17585273765
# jpa配置
jpa:
hibernate:
ddl-auto: update
show-sql: true
cloud:
#nacos 配置
nacos:
discovery:
server-addr: 127.0.0.1:8848
#sentinel 配置
sentinel:
transport:
dashboard: 127.0.0.1:8858 # sentinel 控制台地址
port: 9605 # 客户端(核心应用)和控制台的通信端口,默认8719,子当以一个为被使用的唯一端口即可
web-context-unify: false #关闭收敛
# 暴露/actuator/sentinel端点 单独配置,management 开顶格写
management:
endpoints:
web:
exposure:
include: '*'
2.0.3 测试
http://localhost:9604/actuator/sentinel
2.1 流控模式
2.1.1 直接模式
2.1.1.1 测试请求
/**
* @param : [sentinelDesc]
* @return : java.lang.String
* @author : huayu
* @date : 26/11/2022
* @description : 测试 Sentinel 流控 - 直接失败
*/
@GetMapping("testSentinelFlowFail")
public String testSentinelFlowFail(@RequestParam String sentinelDesc) {
log.info("------ testSentinelFlowFail 接口调用 ------ ");
return sentinelDesc;
}
2.1.1.2 添加直接流控规则
2.1.1.2.1 需要先发起异常请求
2.1.1.2.2 簇点链路 添加流控规则
2.1.1.2.3 设置流控规则
2.1.1.3查看流控规则
2.1.1.4 测试
2.1.1.5 自定义sentinel统一已成返回处理
/**
* Created On : 26/11/2022.
* <p>
* Author : huayu
* <p>
* Description: 自定义sentinel统一已成返回处理
*/
@Slf4j
@Component
public class MySentinelBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
// 记录异常日志
log.warn("------ MySentinelBlockExceptionHandler 规则Rule:{} ------", e.getRule());
// 增加自定义统一异常返回对象
RequestResult<String> requestResult = null;
// 针对不同的流控异常,统一返回
if (e instanceof FlowException) {
requestResult = ResultBuildUtil.fail("9621", "接口流量限流");
} else if (e instanceof DegradeException) {
requestResult = ResultBuildUtil.fail("9622", "接口服务降级");
} else if (e instanceof ParamFlowException) {
requestResult = ResultBuildUtil.fail("9623", "热点参数限流");
} else if (e instanceof SystemBlockException) {
requestResult = ResultBuildUtil.fail("9624", "触发系统保护");
} else if (e instanceof AuthorityException) {
requestResult = ResultBuildUtil.fail("9625", "授权规则限制");
}
// 统一返回json结果
httpServletResponse.setStatus(HttpStatus.FORBIDDEN.value());
httpServletResponse.setCharacterEncoding("utf-8");
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
// 借助SpringMVC自带的Jackson工具,返回结果
new ObjectMapper().writeValue(httpServletResponse.getWriter(), requestResult);
}
}
2.1.1.6 再次测试
2.1.2 关联模式
2.1.2.1 测试请求
/**
* @param : [sentinelDesc]
* @return : java.lang.String
* @author : huayu
* @date : 26/11/2022
* @description : 测试 Sentinel 流控 - 关联
*/
@GetMapping("testSentinelFlowLink")
public String testSentinelFlowLink(@RequestParam String sentinelDesc) {
log.info("------ testSentinelFlowLink 接口调用 ------ ");
return sentinelDesc;
}
2.1.1.2 添加关联流控规则
2.1.1.3 JMeter压测配置
2.1.1.3.1 线程组
2.1.1.3.2 Http请求
2.1.1.3.3 测试 testSentinelFlowLink 接口
2.1.3 链路模式
链路流控模式指的是,当从某个接口过来的资源达到限流条件时,开启限流。它的功能有点类似于针对来源配置项,区别在于ÿ