SpringCloud microservice architecture-based Hystrix circuit breaker, springcloudhystrix

Source: Internet
Author: User

SpringCloud microservice architecture-based Hystrix circuit breaker, springcloudhystrix

I. What is Hystrix?

In a distributed environment, some of the Service dependencies inevitably fail. Hystrix is a database that helps you control the interaction between distributed services by adding latency tolerance and fault tolerance logic. Hystrix isolates access points between services, stops cascade failures, and provides rollback options to improve the overall system elasticity.

Hystrix is designed to perform the following operations

1: provides protection for dependencies that are accessed through third-party client libraries (usually through the network) and controls latency and faults.

2: isolate cascade faults in complex distributed systems.

3: quickly discover faults and restore them as soon as possible.

4: roll back and downgrade as elegantly as possible.

5: enables near real-time monitoring, alarm and operation control.

Ii. Why do we need Hystrix?

In a large distributed system, a client or service depends on an external service. If a service goes down, the timeout time of the service calling system is set, which will inevitably affect the corresponding time, in the case of high concurrency, most server thread pools are blocked, affecting the stability of the entire online service.

(Official Images)

When everything is healthy, the request can look like this

When one of the many backend service systems goes down, the entire user request:

If multiple clients call the same exception service, the following occurs:

 

Iii. What problems does Hystrix solve?

Applications in a distributed architecture have dozens of dependencies, and each dependency may encounter exceptions at a certain time. If the application is not isolated from these external faults, thread pool blocking may occur, causing system avalanche.

For example, for applications dependent on 30 services, the normal running time of each service is 99.99%. You can:

99.99% to the power of 30 = 99.7% normal running time

0.3% of 1 billion requests = 3,000,000 failures

2 + hours of downtime/month, even if all dependencies are running normally.

When Hystrix is used for fusing, each dependency is isolated from each other, limiting blocking when latency occurs.

 

4. Use Hystrix in combination 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 circuit breaker UserClientHystrix

@ Servicepublic class UserClientHystrix implements UserClient {@ Override public User getuserinfo () {throw new NullPointerException ("User getuserinfo () Service unavailable .. ") ;}@ Override public String getuserinfostr () {return" UserClientHystrix getuserinfostr () is fallback service unavailable .. ";}@ Override public String info () {return" UserClientHystrix info () is fallback service unavailable .. ";}}

When a network exception occurs, you can directly jump to the implementation class here.

Create 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 eureka_register_service (Registry) project first

Then run the FeignHystrixApplication we wrote.

At this time we obviously found that the biz-service-0 service is not running, then we open http: // 127.0.0.1: 8005/getuserinfostr

Appears

UserClientHystrix getuserinfostr () is fallback service unavailable ..

This is our custom fuse return result.

This will appear if you do not need to fuse the page

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. 

Address: https://github.com/zhp8341/SpringCloudDemo

I have also read some related principles of Hystrix, and I haven't written them yet because I haven't fully read them yet. This article uses Feign for usage and learning.

If there is a rise, you can see that the official Hystrix is very detailed, that is, it looks a little difficult,

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.