1: What is Hystrix
In a distributed environment, some of many service dependencies will inevitably fail. Hystrix is a library that helps you control the interaction between these distributed services by adding latency tolerance and fault tolerance logic. Hystrix improves the overall resilience of the system by isolating access points between services, stopping cascading failures therebetween, and providing fallback options.
Hystrix is designed to do the following
1: Provide protection and control latency and failures for dependencies accessed through third-party client libraries (usually over the network).
2: Isolate cascading failures in complex distributed systems.
3: Quickly detect the fault and recover as soon as possible.
4: Rewind and downgrade as gracefully as possible.
5: Enable near real-time monitoring, alerts and operational controls.
2: Why do you need Hystrix?
In a large distributed system, a client or service depends on external services. If a service is down, then we set the service call system timeout time, which will inevitably affect the corresponding time. In the case of high concurrency, the thread pool of most servers will be blocked (BLOCK), affecting the stability of the entire online service.
(Photo official pictures)
When everything is healthy, the request can look like this
When one of many backend service systems goes down, the entire user requests:
If multiple clients call the same exception service, the situation occurs:
3: What problems does Hystrix solve?
Applications in distributed architectures have dozens of dependencies, and each dependency will inevitably have an exception at some point. If the application is not isolated from these external failures, thread pool blocking may occur, causing system avalanche.
For example, for applications that rely on 30 services, each service has uptime of 99.99%, you can:
99.99% of power of 30 = 99.7% uptime
0.3% of 1 billion requests = 3,000,000 failures
2+ hours downtime/month, even with all dependencies uptime.
When using Hystrix for circuit breaking, each dependency is isolated from each other, limiting blockage when delay occurs.
Four: Hystrix combined with Feign
Create a project eureka_feign_hystrix_client
pom.xml file content
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Create a startup file
FeignHystrixApplication
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class FeignHystrixApplication { public static void main(String[] args) { SpringApplication.run(FeignHystrixApplication.class, args); }}UserClient class
@FeignClient(value = "biz-service-0",fallback = UserClientHystrix.class)public interface UserClient { @RequestMapping(method = RequestMethod.GET, value = "/getuser") public User getuserinfo(); @RequestMapping(method = RequestMethod.GET, value = "/getuser") public String getuserinfostr(); @RequestMapping(method = RequestMethod.GET, value = "/info") public String info();}Create a flash breaker class UserClientHystrix
@Servicepublic class UserClientHystrix implements UserClient { @Override public User User getuserinfo() { throw new NullPointerException(" User getuserinfo() service is unavailable.."); } @Override public String getuserinfostr() { return " UserClientHystrix getuserinfostr() is fallback service is unavailable.."; } @Override public String info() { return " UserClientHystrix info() is fallback service is unavailable.."; }}When an exception occurs in the network, it may jump directly to the implementation class here
Create an action class
UserController
@Autowired UserClient userClient; @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET) public User getuserinfo() { return userClient.getuserinfo(); } @RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET) public String getuserinfostr() { return userClient.getuserinfostr(); } @RequestMapping(value = "/info", method = RequestMethod.GET) public String info() { return userClient.info(); }Start the: eureka_register_service (register center) project
Then run the FeignHystrixApplication we wrote
At this time, we obviously found that the biz-service-0 service was not running, so we open http://127.0.0.1:8005/getuserinfostr
Appear
UserClientHystrix getuserinfostr() is fallback The service is not available. .
This is our custom circuit return result
If you don't need to break the page, this will appear
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Wed Mar 22 14:32:21 CST 2017There was an unexpected error (type=Internal Server Error, status=500).getuserinfo failed and fallback failed.
Code address: https://github.com/zhp8341/SpringCloudDemo
I have also read some Hystrix related principles. Since I haven't finished reading them all, I haven't written them yet. This article is based on Feign's use and learning.
If you are interested, you can check out the official Hystrix details, but it looks a bit difficult.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.