Master Sentinel Flow Control and Circuit Breaking in Spring Cloud Alibaba
This tutorial shows how to use the @SentinelResource annotation to define custom resource points, configure flow‑limiting and degradation rules, and implement block handlers and fallback methods for Spring Cloud Alibaba applications, with step‑by‑step code examples and dashboard screenshots.
Custom Resource Points
Based on a project that already includes Spring Cloud Alibaba Sentinel, you first add annotation support in the main application class.
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
// Annotation support bean
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}Then annotate the method you want to protect with @SentinelResource and give it a resource name.
@Slf4j
@Service
public class TestService {
@SentinelResource(value = "doSomeThing")
public void doSomeThing(String str) {
log.info(str);
}
}Implementing Flow Limiting
Expose the protected method through a REST controller.
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/hello")
public String hello() {
testService.doSomeThing("hello " + new Date());
return "didispace.com";
}
}Start the application and the Sentinel Dashboard, then send requests to /hello. The dashboard will display two resource points: the HTTP endpoint and the custom doSomeThing resource, where you can set a QPS limit (e.g., 2). When the limit is exceeded, Sentinel throws a FlowException and logs an error.
Handling Flow‑Limiting Exceptions
Define a blockHandler method to process blocked requests instead of returning a raw exception.
@Slf4j
@Service
public class TestService {
@SentinelResource(value = "doSomeThing", blockHandler = "exceptionHandler")
public void doSomeThing(String str) {
log.info(str);
}
public void exceptionHandler(String str, BlockException ex) {
log.error("blockHandler: " + str, ex);
}
}The block handler must have the same parameters as the original method plus a BlockException and return the same type.
Implementing Circuit Breaking (Degrade)
Use @SentinelResource with a fallback method to define degradation logic.
@Slf4j
@Service
public class TestService {
@SentinelResource(value = "doSomeThing2", fallback = "fallbackHandler")
public void doSomeThing2(String str) {
log.info(str);
throw new RuntimeException("exception occurred");
}
public void fallbackHandler(String str) {
log.error("fallbackHandler: " + str);
}
}After configuring a degradation rule (e.g., 50% exception ratio, 2‑second window) on the Dashboard for the doSomeThing2 resource, frequent calls that exceed the rule will trigger the fallback method instead of propagating the exception.
More Annotation Attributes
value: resource name (required). entryType: entry type, default EntryType.OUT. blockHandler / blockHandlerClass: method or class handling BlockException. fallback / fallbackClass: method or class handling business exceptions. defaultFallback: global fallback method (since 1.6.0). exceptionsToIgnore: exceptions excluded from fallback handling.
Reference
Sentinel official documentation and the sample project GitHub / Gitee .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
