- 引入依赖
<dependencies>
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-coreartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-annotation-aspectjartifactId>
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);
rule.setCount(1);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 业务代码编写
@RestController
@RequestMapping("/hello")
public class HelloController {
@SentinelResource("HelloWorld")
@GetMapping("/index")
public String index(){
return "hello index" ;
}
}