- 引入依赖
<dependencies> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> </dependency> </dependencies>
- 添加sentinel注解支持配置
@Configuration public class SentinelAspectConfiguration { @Bean public SentinelResourceAspect sentinelResourceAspect() { return new SentinelResourceAspect(); } }
- 编写配置并加载
@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); } }
- 业务代码编写
@RestController @RequestMapping("/hello") public class HelloController { @SentinelResource("HelloWorld") @GetMapping("/index") public String index(){ return "hello index" ; } }
Sentinel整合SpringBoot
于 2023-09-04 18:11:51 首次发布