Hystrix vs. Sentinel: A Tale of Two Circuit Breakers (Part 2)

Bookstore Example

The example used here is from this Spring tutorial. This is a famous bookstore sample app.

public class BookService {
private final RestTemplate restTemplate;
public BookService(RestTemplate rest) {
this.restTemplate = rest;
@SentinelResource(value = "readingList", fallback = "reliable")
public String readingList() {
URI uri = URI.create("http://localhost:8090/recommended");
return this.restTemplate.getForObject(uri, String.class);
public String reliable() {
return "Cloud Native Java (O'Reilly)";
new DegradeRule("readingList") // resource name
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) // strategy
.setCount(0.5) // threshold
.setTimeWindow(10) // circuit breaking timeout (in second)
Cloud Native Java (O'Reilly)
Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)

Wait, There’s More

So far, we’ve looked at the same feature as Hystrix has performed. And actually, Sentinel needs one more step. But here is the justification: users can achieve more with that flexibility. Now, let’s see an example.

public class BookstoreApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(BookstoreApplication.class);
@SentinelResource(value = "readingList", blockHandler = "handleTooManyRequests")
@RequestMapping(value = "/recommended")
public String readingList(){
return "Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)";
public String handleTooManyRequests(BlockException ex) {
LOGGER.error("Too many requests: " + ex.getClass().getSimpleName());
return "Sentinel in Action";
public static void main(String[] args) {
SpringApplication.run(BookstoreApplication.class, args);
FlowRule rule = new FlowRule("readingList")
Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)
Sentinel in Action


Sentinel aims to provide users with multiple options to control the flow into their services. By doing so, it requires users to define the rules via GUI or code. Other than QPS, users can control the number of threads, or even create a white list for access control. With the growing complexity of distributed services, this model will better serve the user’s requirements.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com