在应用启动时,通过 FlowRuleManager.loadRules()方法加载限流规则。以下示例配置资源 helloCode的 QPS(每秒查询率)阈值为 2。@Configurationpublic class SentinelConfig {@PostConstructpublic void initFlowRules() { FlowRule rule = new FlowRule(); rule.setResource(RESOURCE_NAME); // 资源名,需与 SphU.entry 中一致 rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 限流阈值类型:QPS rule.setCount(2); // 阈值:每秒最多 2 次请求 FlowRuleManager.loadRules(Collections.singletonList(rule));}}
grade: 阈值类型,FLOW_GRADE_QPS(1) 表示按 QPS 限流,FLOW_GRADE_THREAD(0) 表示按并发线程数限流。
count: 具体的阈值,如 QPS 为 2 表示每秒最多通过 2 个请求。
配置完成后,快速访问http://localhost:8080/code/hello,当 QPS 超过 2 时,页面将返回降级信息。对于 Spring 项目,@SentinelResource注解是更优雅的选择,它能将方法标记为一个 Sentinel 资源,并支持自定义限流和降级处理逻辑。
使用@SentinelResource注解标记方法,并通过 blockHandler属性指定限流/降级时执行的方法。@Servicepublic class AnnotationService {public static final String RESOURCE_NAME = "helloAnno";@SentinelResource(value = RESOURCE_NAME, blockHandler = "handleBlock")public String hello(String name) { // 模拟业务异常 if ("error".equals(name)) { throw new RuntimeException("业务出错了"); } return "hello " + name + " (anno)";}// 注意:降级方法必须与原方法在同一个类中,参数和返回值类型需一致,最后增加一个 BlockException 参数public String handleBlock(String name, BlockException ex) { return "请求被限流/降级了(anno),name=" + name;}}
在 Controller 中注入并调用该 Service 即可。@RestController@RequestMapping("/anno")public class AnnotationController {@Autowiredprivate AnnotationService annotationService;@GetMapping("/hello")public String hello(@RequestParam(defaultValue = "sentinel") String name) { return annotationService.hello(name);}}
@SentinelResource还提供了 fallback属性,专门用于处理业务异常,与 blockHandler处理流控/熔断的场景区分开。@SentinelResource(value = "helloFallback",blockHandler = "handleBlock",fallback = "handleFallback")public String helloFallback(String name) {if ("error".equals(name)) {throw new RuntimeException("业务出错了");}if ("slow".equals(name)) {try { Thread.sleep(200); } catch (InterruptedException e) {}}return "hello " + name + " (fallback)";}// 处理业务异常public String handleFallback(String name, Throwable t) {return "业务异常被捕获:" + t.getMessage();}// 处理限流/熔断public String handleBlock(String name, BlockException ex) {return "请求被限流/降级了(fallback),name=" + name;}
blockHandler: 仅在触发限流、熔断、系统保护时执行。
fallback: 在业务方法抛出非 BlockException异常时执行,用于业务异常处理。
通过引入 sentinel-transport-simple-http依赖并配置 dashboard地址,即可将应用接入 Sentinel 控制台,实现规则的动态管理和实时监控。
从 Sentinel GitHub Releases 页面下载 sentinel-dashboard.jar并启动。使用默认端口 8080,账号密码均为 sentineljava -jar sentinel-dashboard.jar
java -Dserver.port=18080 \-Dsentinel.dashboard.auth.username=sentinel \-Dsentinel.dashboard.auth.password=123456 \-jar sentinel-dashboard.jar
访问http://localhost:18080即可登录。确保项目中已引入 spring-cloud-starter-alibaba-sentinel并配置了 dashboard地址。启动应用后访问一次其接口,Sentinel 客户端便会自动注册到控制台。在控制台的 “簇点链路” 菜单中,可以找到所有可配置的接口资源(如/code/hello, /anno/hello)。点击对应资源右侧的 “+流控” 按钮,即可通过图形化界面配置规则,例如设置 QPS 阈值为 1。配置完成后,再次快速访问接口,即可在控制台实时看到通过的 QPS 和被拒绝的 QPS 数据,直观地验证限流效果。本文介绍了 Sentinel 的三种核心用法,它们并非互斥,而是可以根据场景灵活组合:代码方式 (SphU.entry): 适用于对性能要求极高、需要完全自定义埋点的场景。
注解方式 (@SentinelResource): 适用于大多数 Spring 项目,开发效率高,与业务代码解耦。
控制台方式: 用于规则的动态管理、实时监控和快速验证,是生产环境的标配。