1. The background

Hystrix is a fault-tolerant framework that uses circuit breakers to protect as many services as possible when multiple services could cause a snowball effect.

2. Knowledge

Because each failure unit is running in a different process, calling each other remotely, failures or delays can occur due to network reasons or service health. In turn, the caller also has delays. If the caller’s requests keep piling up, it will eventually form a backlog and its own service will crash.

In order to solve the problem, a series of service protection mechanisms such as circuit breakers are produced.

The Spring Cloud Circuit Breaker provides an abstraction that can support different “breaker implementations.” It provides a consistent API for use in applications, allowing developers to choose the breaker implementation that best fits the needs of the application.

It supports the following:

  • Netfix Hystrix
  • Resilience4J
  • Sentinel
  • Spring Retry

Create circuit breakers To create circuit breakers, you can use the CircuitBreakerFactory API. When you include the Spring Cloud Circuit Breaker “launcher” in your classpath, beans that implement this API are automatically created for you.

Example 3.

Add the dependent

dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix', version: '2.2.8. RELEASE' testImplementation 'org. Springframework. The boot: spring - the boot - starter - test'}Copy the code

To create a circuit breaker, perform the following steps:

  • 1. Load a CircuitBreakerFactory
  • 2. Use the cbFactory.create().run() method to execute your task.

Format example:

return cbFactory.create("slow").run(() -> rest.getForObject("/slow", String.class), throwable -> "fallback");
Copy the code

Complete example:

package ribbonconsumer.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.time.Duration; /** * @description: * @author: zhangyunfei * @date: 2021/5/11 16:35 */ @RestController public class ReadController { @Autowired private CircuitBreakerFactory cbFactory; @Autowired private RestTemplate restTemplate; @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } @RequestMapping(value = "/read", method = RequestMethod.GET) public String index() { return cbFactory.create("slow").run(() -> { long start = System.currentTimeMillis(); String str = restTemplate.getForObject("http://HELLO-SERVICE-1/hello", String.class); long end = System.currentTimeMillis(); log("Spend time : " + (end - start)); return str; }, throwable -> "on error"); } private static void log(String str) { System.out.println(str); }}Copy the code

4. The extension

Configuring the Timeout Period

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
Copy the code

My demo address: github.com/vir56k/demo…

Note: In service degradation logic, we need to implement a generic response result, and the processing logic for that result should be fetched from cache or according to some static logic.

5. Reference:

Spring. IO /projects/sp… Juejin. Cn/post / 684490…