sentinel-core

  1. 引入依赖
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-coreartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-annotation-aspectjartifactId>
        dependency>
    dependencies>
    
  2. 添加sentinel注解支持配置
    @Configuration
    public class SentinelAspectConfiguration {
        @Bean
        public SentinelResourceAspect sentinelResourceAspect() {
            return new SentinelResourceAspect();
        }
    }
    
  3. 编写配置并加载
    @Slf4j
    @SpringBootApplication
    public class WebMvcApplication {
       public static void main(String[] args) {
           SpringApplication.run(WebMvcApplication.class, args) ;
           // 加载限流规则
           initFlowRules();
       }
       private static void initFlowRules(){
           log.info("load sentinel flow rule .....");
           List<FlowRule> rules = new ArrayList<>();
           FlowRule rule = new FlowRule();
           rule.setResource("HelloWorld");
           rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
           // Set limit QPS to 1.
           rule.setCount(1);
           rules.add(rule);
           FlowRuleManager.loadRules(rules);
       }
    }
    
  4. 业务代码编写
    @RestController
    @RequestMapping("/hello")
    public class HelloController {
        @SentinelResource("HelloWorld")
        @GetMapping("/index")
        public String index(){
            return "hello index" ;
        }
    }
    

你可能感兴趣的:(sentinel)