为了账号安全,请及时绑定邮箱和手机立即绑定

SpringBoot集成Sentinel实战:从依赖到自定义限流与监控

本文已收录在Github关注我,紧跟本系列专栏文章,咱们下篇再续!

  • 🚀 魔都架构师 | 全网30W技术追随者
  • 🔧 大厂分布式系统/数据中台实战专家
  • 🏆 主导交易系统百万级流量调优 & 车联网平台架构
  • 🧠 AIGC应用开发先行者 | 区块链落地实践者
  • 🌍 以技术驱动创新,我们的征途是改变世界!
  • 👉 实战干货:编程严选网

1 添加依赖

1.1 JDK8

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.10-RC1</version>
</dependency>

groupId也可为 org.springframework.cloud。

涵盖依赖包:

1.2 JDK21

我使用的 JDK23+SpringBoot3.4.1:

<!-- Sentinel 依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2023.0.3.2</version>
</dependency>

2 暴露端点

整合成功后,会暴露actuator/Sentinel端点,所以再添依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

还需配置才能暴露端点(默认不暴露):

management:
  endpoints:
    web:
      exposure:
        include: '*'

3 配置文件

连接Sentinel控制台的地址信息配置

spinrg:
 cloud:
  sentinel:
    transport:
      dashboard: localhost:8080

4 Sentinel 自定义限流响应与实时监控

Sentinel 自定义资源与限流规则

如针对接口限流:

@RestController
@RequestMapping("/pilot")
public class PilotController {

    @SentinelResource(value = "pilot_list", 
                     blockHandler = "blockHandler")
    @GetMapping("/getList")
    public ResultBody list() {
        Map<String, List<Pilot>> pilotServiceList = pilotService.getList();
        return ResultBody.success(pilotServiceList);
    }

    // 限流降级方法
    public ResultBody blockHandler(BlockException e) {
        log.warn("触发限流", e);
        return ResultBody.error("服务繁忙,请稍后再试");
    }
}

value对应的资源名称:

@Configuration
public class SentinelConfig implements BlockExceptionHandler {
    
    @PostConstruct
    private void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();
        
        // 创建流控规则
        FlowRule rule = new FlowRule();
        // 设置受保护的资源
        rule.setResource("pilot_list");
        // 设置流控规则 QPS
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // 设置受保护的资源阈值
        rule.setCount(1);
        rules.add(rule);
        
        // 加载规则
        FlowRuleManager.loadRules(rules);
    }

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, String resourceName, BlockException e) throws Exception {
        response.setStatus(429);
        response.getWriter().write("访问过于频繁,请稍后再试");
    }
}

限流效果:

注意

升级后,注意验证规则是否失效,避免版本差异bug。

参考:

本文由博客一文多发平台 OpenWrite 发布!

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
1.4万
获赞与收藏
1479

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消