这里写自定义目录标题
- [1. 下载好的 sentinel 放我网盘了,大家有使用的可以直接下载](#1. 下载好的 sentinel 放我网盘了,大家有使用的可以直接下载)
-
- [1.1 官网地址](#1.1 官网地址)
- [1.2 下载好的 sentinel 放我网盘了,大家有使用的可以直接下载](#1.2 下载好的 sentinel 放我网盘了,大家有使用的可以直接下载)
- [1.3 放在自己电脑合适的目录](#1.3 放在自己电脑合适的目录)
- [2. 启动 Sentinel 服务](#2. 启动 Sentinel 服务)
- [4. 项目里面集成](#4. 项目里面集成)
- [5. 项目中使用](#5. 项目中使用)
-
- [5.1 接口限流策略实现](#5.1 接口限流策略实现)
- [5.2 全局限流异常统一管理](#5.2 全局限流异常统一管理)
- [6. Apifox测试](#6. Apifox测试)
1. 下载好的 sentinel 放我网盘了,大家有使用的可以直接下载
1.1 官网地址
https://sentinelguard.io/zh-cn/docs/dashboard.html
1.2 下载好的 sentinel 放我网盘了,大家有使用的可以直接下载
通过网盘分享的文件:sentinel-dashboard-1.8.9.jar
链接: https://pan.baidu.com/s/1dZKJSJeCwt43HjJQogWUCA?pwd=vb79 提取码: vb79
1.3 放在自己电脑合适的目录

2. 启动 Sentinel 服务
管理员身份打开 CMD 窗口
进入 D:
cd Project
java -jar -Dserver.port=1111 sentinel-dashboard-1.8.9.jar

访问启动好的默认地址
地址:
http://localhost:1111/#/login
账号:sentinel
密码:sentinel

4. 项目里面集成
导入maven依赖
<properties>
<!-- Spring Cloud Alibaba 版本 -->
<spring-cloud-alibaba.version>2025.1.0.0</spring-cloud-alibaba.version>
</properties>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>${spring-cloud-alibaba.version}</version>
</dependency>
修改yml配置,添加senticel服务控制台地址
spring:
cloud:
sentinel:
transport:
dashboard: localhost:1111
5. 项目中使用
5.1 接口限流策略实现
需要注意的点:如果在接口设置了单个限流,那么它是抛出的自定义实现,如果没有写接口限流走的全局实现。
比如 手机号验证码登录 接口

指定资源名称和限流的实现类
@SentinelResource(value="client-login-mobile-captcha",blockHandler="exceptionHandler", blockHandlerClass=ClientloginMobileCaptcha.class)
ClientloginMobileCaptcha
java
package com.zkkj.sg.client.sentinel;
import com.zkkj.sg.common.request.LoginMobileRequest;
import com.zkkj.sg.common.response.LoginResponse;
import com.zkkj.sg.common.result.CommonResult;
/**
* 限流降级
* 手机号验证码登录
*/
public final class ClientloginMobileCaptcha {
/**
* 限流降级处理方法
* 注意:方法签名必须与原始方法一致,可以额外添加BlockException参数
*/
public static CommonResult<LoginResponse> exceptionHandler(LoginMobileRequest loginRequest,
com.alibaba.csp.sentinel.slots.block.BlockException ex) {
return CommonResult.failed("手机号验证码登录 接口请求过多,服务器开始限流...");
}
}
5.2 全局限流异常统一管理

java
package com.zkkj.sg.client.handler;
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.zkkj.sg.common.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* Sentinel全局异常处理器
* 处理所有Sentinel相关的限流、降级、熔断等异常
*/
@Slf4j
@RestControllerAdvice
public class SentinelGlobalExceptionHandler {
/**
* 处理流控异常(请求过多)
*/
@ExceptionHandler(FlowException.class)
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public CommonResult<?> handleFlowException(FlowException e) {
log.warn("接口请求过多,触发流控限制: {}", e.getMessage());
return CommonResult.failed("请求过于频繁,请稍后再试");
}
/**
* 处理降级异常(服务降级)
*/
@ExceptionHandler(DegradeException.class)
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public CommonResult<?> handleDegradeException(DegradeException e) {
log.warn("服务降级: {}", e.getMessage());
return CommonResult.failed("服务暂时不可用,请稍后再试");
}
/**
* 处理热点参数限流异常
*/
@ExceptionHandler(ParamFlowException.class)
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public CommonResult<?> handleParamFlowException(ParamFlowException e) {
log.warn("热点参数限流: {}", e.getMessage());
return CommonResult.failed("参数请求过于频繁,请稍后再试");
}
/**
* 处理系统保护异常
*/
@ExceptionHandler(SystemBlockException.class)
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public CommonResult<?> handleSystemBlockException(SystemBlockException e) {
log.warn("系统保护触发: {}", e.getMessage());
return CommonResult.failed("系统资源紧张,请稍后再试");
}
/**
* 处理授权异常
*/
@ExceptionHandler(AuthorityException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public CommonResult<?> handleAuthorityException(AuthorityException e) {
log.warn("授权限制: {}", e.getMessage());
return CommonResult.failed("访问权限受限");
}
/**
* 处理其他Sentinel阻塞异常(兜底处理)
*/
@ExceptionHandler(BlockException.class)
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public CommonResult<?> handleBlockException(BlockException e) {
log.warn("Sentinel阻塞异常: {}", e.getMessage());
return CommonResult.failed("系统繁忙,请稍后再试");
}
}
6. Apifox测试

新增手机号验证码限流机制

运行Apifox测试

