Source code address: gitee.com/fighter3/es…

Continuously updated…

In the previous section we used OpenFeign to make calls between services. Think about it, if we have a dozen service chain upstream and downstream on the services, each service has a number of nodes, one of the node failure, request to upstream node failure, to join the request has been blocked, accumulated a large number of requests may beat service collapse, may cause cascading failure, and even the whole link failure, this is the avalanche of services, If the fault is serious, the system may hang up. To avoid this dire situation, the necessary fault-tolerant protection mechanisms are required.

1. Introduction to Hystrix

Hystrix is an important component of Netflix, providing circuit breakers, resource isolation, and self-healing capabilities.

The Hystrix below acts as a circuit breaker to prevent cascading failures.

But hystrix version 1.5.18 went into maintenance mode, and that’s what we’re using. In SpringCloud Alibaba’s system, there is another component sentinel that can be used as a substitute, which we will use later.

Although Hystrix has ceased to be updated, it is now a mature product after many years of iteration, so it still has a wide range of applications.

Hystrix is also very simple to use in the SpringCloud architecture, so let’s get started!

2. Introduce Hystrix

Again, using the example we did in the last video.

  • Spring-cloud-starter is adopted to introduce:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

Copy the code
  • Start Hystrix in application.yml:
feign:
  hystrix:
    enabled: true
Copy the code
  • Add the @enablehystrix annotation to the service startup class to EnableHystrix functionality.
@SpringBootApplication
@MapperScan("cn.fighter3.mapper")
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "cn.fighter3.client")
@EnableHystrix
public class EshopGoodsApplication {

    public static void main(String[] args) { SpringApplication.run(EshopGoodsApplication.class, args); }}Copy the code
  • What is the purpose of writing a StockClientFallback class that implements the StockClientFeign interface? Is used for Feign client remote call failure callback.
/ * * *@AuthorThree points *@Date 2021/5/29
 * @DescriptionInventory service callback exception callback class */
@Component
@Slf4j
public class StockClientFallback implements StockClientFeign {

    public Integer addStock(StockAddDTO stockAddDTO) {
        log.error("Inventory Services - Add inventory not available!");
        return 0;
    }

    public Integer getAccountById(Integer goodsId) {
        log.error("Inventory Services - Get inventory not available!");
        return 0; }}Copy the code
  • Added a failed callback configuration to StockClientFeign@ FeignClient (value = "stock - service")
@FeignClient(value = "stock-service", fallback = StockClientFallback.class)
Copy the code

There is another way to define the service degradation method using @hystrixCommand (fallbackMethod = “getDefaultUser”) on the method.

3. Test Hystrix

  • Start NACOS-Server, goods service, and notice that we did not start the inventory service

  • Open the http://localhost:8020/doc.html, call the add goods interface. Think about it. What would happen in the normal case? Since the inventory service is not up, the product service must also return an exception, but after adding Hystrix, it is found that the interface returns a successful result.

Looking at the log we typed, we see that the callback method was called.

Well, the Hystrix implementation circuit breaker ends here.

Continue to update, please pay attention to……





“Do simple things repeatedly, do repetitive things carefully, and do serious things creatively!” –

I am three points evil, a full stack of literary and military development, let’s see next period!



Reference:

[1] : Small column “SpringCloudAlibaba Micro-service Practice”

[2] : Spring Cloud Hystrix: Service fault tolerance protection