What is circle breaker and hystrix sample in Spring cloud

1.Why we need circle breaker and service downgrade?

    In a distributed system, a service depends on multiple services, and there may be a service call failure, such as timeout, exception, etc. How can it be ensured that the overall service will not fail if a dependency service fail?

    For example,program bugs cause infinite loops, slow queries, and program logic errors cause memory exhaustion. From outside,hacker attacks, promotions, and slow response from third-party systems

    If whole service flow blocked due to some service fail,We should protect core business and the majority of users. For example, the login function is very important. When the internet traffic is too high, stop the registration function to free up resources for login.

Some methods we can use , like: Fuse, downgrade, current limit, queuing

2.what is circle breaker?

    When an abnormal condition is triggered, directly fuse the entire service instead of waiting until the service times out. In order to prevent the failure of the entire system.

    For example, the X function of A service depends on a certain interface of B service. When the response of B service interface is very slow, the response of A service X function will be slowed down, which further causes A service threads to be stuck in X function.

    On the other hand, other functions of the A service will also get stuck or slow down. At this time, a fuse mechanism is needed, that is, service A does not request the interface of B, but can be directly degraded.

3.What is service downgrade?

    When the pressure on the server increases sharply, some services and pages are downgraded according to the current conditions and traffic. In this way, the pressure on server resources is relieved to ensure the normal operation of core business, while maintaining customer and most customers get the correct response.

4.Hystrix example

In this example, we have three parts:

Eureka-server:7001   register
product-server:8001 product service
order-server:9001 order service

Order service application:

//circle breaker annotation
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);


if findByIdfail, the request will fall to ProductClientFallback

@FeignClient(name = "product-service",fallback = ProductClientFallback.class)
public interface ProductClient {
    String findById(@RequestParam(value = "id") int id);


The downgrade service

public class ProductClientFallback implements ProductClient {
    public String findById(int id) {
        System.out.println("downgrade service for findById");
        return null;


public class OrderController {
    private ProductOrderService productOrderService;
    //if fail on this method , it will switch to saveOrderFail
    @HystrixCommand(fallbackMethod = "saveOrderFail")
    public Object save(@RequestParam("user_id")int userId, @RequestParam("product_id") int productId){
        return productOrderService.save(userId, productId);
    //this method MUST have the same parameters
    private Object saveOrderFail(int userId, int productId){
        System.out.println("service downgrade for save");
        Map<String, Object> msg = new HashMap<>();
        msg.put("code", 500);
        msg.put("msg", "too many request");
        return msg;

As you can see , when we request order controller in order service ,this controller will call product service. So if we stop the product service ,the request will fall to method “saveOrderFail”, and then we will get response:

 msg:"too many request",