亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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
提交
取消