spring cloud alibaba 学习记录 接入 Sentinel

该文介绍了Sentinel的快速入门,包括引入依赖、定义资源和规则,展示了如何处理流控异常。同时,文中提到了Sentinel在Web-MVC和注解版中的应用,以及如何与SentinelDashboard进行集成。在SpringCloud环境下,Sentinel的配置和使用也进行了说明。
摘要由CSDN通过智能技术生成
前言

仅记录个人学习日日常 也希望能帮到在做各位

Sentinel Demo
  • 快速入门

    • 引入依赖

       				<dependency>
                  <groupId>com.alibaba.csp</groupId>
                  <artifactId>sentinel-core</artifactId>
                  <version>1.8.5</version>
              </dependency>
      
    • 定义资源

      //  定义资源
        private static final String RESOURCE_NAME = "hello word";
      
        public static void main(String[] args) {
      
          // 配置规则.
          initFlowRules();
      
          while (true) {
            // 1.5.0 版本开始可以直接利用 try-with-resources 特性
            try (Entry entry = SphU.entry(RESOURCE_NAME)) {
              // 被保护的逻辑
              System.out.println(RESOURCE_NAME);
            } catch (BlockException ex) {
              // 处理被流控的逻辑
              System.out.println("blocked!");
            }
          }
        }
      
    • 定义规则

      private static void initFlowRules() {
          List<FlowRule> rules = new ArrayList<>();
          FlowRule rule = new FlowRule();
          rule.setResource(RESOURCE_NAME);
          rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
          // Set limit QPS to 20.
          rule.setCount(20);
          rules.add(rule);
          FlowRuleManager.loadRules(rules);
        }
      
    • 测试

      截屏2023-03-20 15.43.51
  • Web-MVC 版

    • 正常版

      • 接口+定义资源

        @RestController
        @RequestMapping("/flow")
        public class FlowRuleController {
        
          @GetMapping("hello")
          public String hello(){
            try {
              Entry entry = SphU.entry("flow");
              // 被保护的逻辑
              System.out.println(SentinelExample.RESOURCE_NAME);
              return SentinelExample.RESOURCE_NAME;
            } catch (BlockException ex) {
              // 处理被流控的逻辑
              System.out.println("blocked!");
              return "nlocked";
            }
          }
        
        
      • 定义规则

            @PostConstruct // Spring Bean实例化之后执行
          public static void initFlowRules() {
            List<FlowRule> rules = new ArrayList<>();
            FlowRule rule = new FlowRule();
            rule.setResource("flow");
            rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
            // Set limit QPS to 2. 手动点不了20一秒就过了
            rule.setCount(2);
            rules.add(rule);
            FlowRuleManager.loadRules(rules);
          }
        
      • 测试

        截屏2023-03-20 16.07.33
    • 注解版

      • 添加依赖

                <dependency>
                    <groupId>com.alibaba.csp</groupId>
                    <artifactId>sentinel-annotation-aspectj</artifactId>
                    <version>1.8.5</version>
                </dependency>
        
        
      • 添加configuration

        @Configuration
        public class SentinelConfiguration {
        
          @Bean
          public SentinelResourceAspect sentinelResourceAspect(){
            return new SentinelResourceAspect();
          }
        }
        
      • 添加接口+@SentinelResource注解+异常捕获方法

        	@GetMapping("/test")
          @SentinelResource(value="flow",blockHandler = "handleBlock")
          public String test() {
            System.out.println("@Sentinel hello sentinel.");
            return "@Sentinel hello sentinel.";
          }
        
          public String handleBlock(BlockException e){
            System.out.println("流控异常: "+e);
            return "QPS超过阈值,流控了.";
          }
        
      • 测试

        截屏2023-03-20 16.44.18

        其实就是通过AOP给目标代码的前后添加上了SphU#entry相关的逻辑,可以在 SentinelResourceAspect#invokeResourceWithSentinel方法上打个断点

    • 客户端整合Sentinel控制器(DashBoard)

      • 下载控制器源码
        下载jar包

      • java -jar 启动

        java -Dserver.port=8040 -Dcsp.sentinel.dashboard.server=localhost:8040 - Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.5.jar

        • 搞清命令参数

          -Dserver.port=8040 启动端口

          -Dcsp.sentinel.dashboard.server=localhost:8040 加上等于给snetinel通信

          -Dproject.name=sentinel-dashboard 应用名 没有应用名 在SentinelDashboard中是一串路径难看

      • 添加与控制器通信依赖

        <dependency>
        	<groupId>com.alibaba.csp</groupId>
        	<artifactId>sentinel-transport-simple-http</artifactId>
        	<version>1.8.5</version>
        </dependency>
        
      • Sentinel Demo配置启动参数

        -Dcsp.sentinel.dashboard.server=localhost:8040

        加在启动配置 VM options 内

      • 效果

        截屏2023-03-20 19.45.36
    • Sentinel DashBoard整合SpringCloud

      • 引入依赖

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

        引入starter-alibaba-sentinel后可以把前面配置的sentinel相关的依赖去掉了这个里面都有

      • 增加配置

        在applicaltion.properties内增加配置

        spring.cloud.sentinel.transport.dashboard=localhost:8040
        
      • 测试

        截屏2023-03-22 14.19.51
      • 疑惑

        接入spring cloud增加的配置并没有生效能接入还是靠的启动时加的配置参数

        忘大佬解答

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值