Learn Circuit Breaking Pattern With Netflix Hystrix
Go Back to Course Outline Introduction In this video, we will learn how to use Hystrix to solve the problem that comes with a distri...
https://www.czetsuyatech.com/2019/11/spring-microservice-circuit-breaking-pattern-with-netflix-hystrix.html
Go Back to Course Outline
Introduction
In this video, we will learn how to use Hystrix to solve the problem that comes with a distributed microservice architecture, like in the project that we have. And these problems are fault and latency tolerance.
Looking at our business API service, it is fetching data from both catalog and inventory service. But what if those services failed, or the response is delayed? It will affect the overall integrity of the application or even worst it might bring the whole system down.
And that is where Hystrix comes in, it wraps the call to remote services and handles the exception or return a default or computed value.
Setting up Hystrix
- Add a dependency to spring-cloud-starter-netflix-hystrix.
- Annotate the SpringBoot class with @EnableHystrix.
- Set the timeout in application.properties.
- feign.client.config.default.connect-timeout=5000
- feign.client.config.default.read-timeout=5000
- There are 2 ways to use Hystrix with Spring:
- Via annotating FeignClient with fallback property
- Via annotating FeignClient with fallbackFactory
Project Architecture Review
Using the fallback Property
See the CatalogServiceProxy class below.
CatalogServiceFallback Class
See CatalogServiceFallback class below.
Using the fallbackFactory Property
See the CatalogServiceProxy_fallback class below.
CatalogServiceFallbackFactory Class
See CatalogServiceFallbackFactory class below.
Let’s run the application!
- Set the following properties
- terawarehouse-service-discovery
- eureka.server.enable-self-preservation=false
- terawarehouse-catalog and terawarehouse-inventory
- eureka.instance.lease-renewal-interval-in-seconds=1
- eureka.instance.lease-expiration-duration-in-seconds=1
*These are not production settings.
References
- https://github.com/Netflix/Hystrix
- Codes are available at Github:
Post a Comment