Source address: https://gitee.com/fighter3/eshop-project.git
Continuously updating
In the previous section, we have used OpenFeign to complete the call between services. Think about it. If there are more than a dozen services upstream and downstream of a service link, and each service has several nodes, one of which fails, the upstream request hits the failed node, and the join request is blocked all the time. A large number of accumulated requests may collapse the service, may lead to cascading failure, or even the failure of the whole link. This is the so-called service avalanche, Serious may directly cause the system to hang up. In order to avoid this terrible situation, the necessary fault-tolerant protection mechanism is necessary.
1. Introduction to Hystrix
Hystrix is an important component of Netflix, providing circuit breaker, resource isolation and self-healing functions.
The following is Hystrix as a circuit breaker to prevent cascading failure.
But hystrix1 After version 5.18, we entered the maintenance mode, and we adopted this version. In the system of spring cloud and Alibaba, sentinel, another component, can be used as a substitute, which we will use later.
Although the update of Hystrix has stopped, it is now a relatively mature product after years of iteration, so it is still widely used.
The use of Hystrix in the spring cloud system is also very simple. Let's start now!
2. Introducing Hystrix
We still use the example in the previous section.
- Spring cloud starter is adopted to introduce:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
- In application YML enable hystrix:
feign: hystrix: enabled: true
- Add the @ EnableHystrix annotation to the service startup class to enable the system to support the functions of hystrix.
@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); } }
- Write a StockClientFallback class to implement the StockClientFeign interface. What is this class used for? It is used for Feign client remote call failure callback.
/** * @Author Three evil * @Date 2021/5/29 * @Description Inventory service callback exception callback class */ @Component @Slf4j public class StockClientFallback implements StockClientFeign { public Integer addStock(StockAddDTO stockAddDTO) { log.error("Inventory service-Add inventory not available!"); return 0; } public Integer getAccountById(Integer goodsId) { log.error("Inventory service-Get inventory not available!"); return 0; } }
- Add the failed callback configuration in StockClientFeign, which was @ feignclient (value = "stock service")
@FeignClient(value = "stock-service", fallback = StockClientFallback.class)
There is another way to define the service degradation method by using @ HystrixCommand(fallbackMethod = "getDefaultUser") on the method.
3. Test Hystrix
- Start the Nacos server and commodity service successively. Note that we did not start the inventory service
- Open http://localhost:8020/doc.html , call the add product interface. Think about it. What would happen under normal circumstances? Since the inventory service does not work, the commodity service must return an exception. However, when hystrix is added, it is found that the interface returns a successful result.
Look at our log and find that the callback method has been called.
Well, the Hystrix circuit breaker is over.
Continuous update, please pay attention to
"Do simple things repeatedly, do repeated things seriously, and do serious things creatively!"——
I am three evil, a full stack development capable of writing and martial arts. I'll see you next time!
reference resources:
[1] : small column "spring cloud Alibaba micro service pragmatic war"
[2]: Spring Cloud Hystrix: service fault tolerance protection