Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Hystrix Open Source Framework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to carry out the Hystrix open source framework, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Spring Cloud integrates Hystrix,Hystrix and is widely used in many big companies. I wrote an open source framework called Jboot (open source URL git.oschina.net/fuhai/jboot), which also integrates Hystrix, with Hystrix as the core interspersed within the entire framework.

I have some former colleagues in Ali, Baidu, Tencent, Didi and other big companies, they are also widely using Hystrix, but there is no good article to introduce what is Hystrix? Why is it so popular? In what situation do you use it? Solve what pain point?

Introduction to Hystrix:

"Hystrix is used by distributed systems, providing delay and fault tolerance, isolating access points of remote systems, access and third-party libraries, preventing cascading failures, and ensuring that complex distributed systems can still be resilient in the face of inevitable failures."

In fact, this explanation is so general that it is still confusing for students who do not know Hystrix.

In traditional projects, the general architecture is as follows:

In the case of high outbreak, if the appropriate cache is not used, the browser request will be directly sent to the database by the web application, that is, there will be as many database queries as there are browser requests. In this case, the database will often be overwhelmed and abnormal.

For example:

When the database query is too large, the memory consumption of the database server is too high.

When the SQL is more complex, the database CPU is too high, or the result is slow to return.

There are too many connections, causing the link to time out.

Wait.

In our entire web application, the pressure on database queries caused by different logic (or pages) is often different.

For example:

Check the article details page, may be according to the article's primary key query, hit the database primary key index, the efficiency is very high.

An article statistics page, according to complex SQ statements to query the database will get the results, the efficiency is very high, while occupying the database memory and CPU.

If "a certain article statistics page" is heavily accessed, the database is abnormal, and when there is a new request, the database connection timeout will always be returned, including the "View article details page". At this point, a 500 error may occur for users who access the details of the article.

In other words, the two different pages (or two different logic) of "View article details page" and "one article statistics page" will affect each other.

How to solve this kind of problem?

This involves that the Hystrix,Hystrix we want to reduce to this article can isolate the two different pages: "View article details page" and "some article statistics page". When there is a problem with either page, it will not affect the other pages.

In other words, when the "article statistics page" is heavily accessed, there is a database exception, resulting in 500 web applications, then Hystrix should try its best to ensure that the "view article details page" is normal.

How do you do that?

Hystrix offers two solutions:

Thread pool.

Semaphore.

When the "web application" accesses the "database", Hystrix will put different logic into different thread pools, and will enable a "thread pool administrator" to check the execution of all threads in the thread pool. If a certain amount of connection pool exception occurs, then all requests will no longer be requested to the database and will be directly intercepted by Hystrix. And a local method is called to handle the case where "the exception reaches a certain amount", a process also known as "degradation".

In the above business description, "some article statistics page" and "view article details page" are two different logics, which will be put into two different thread pools by Hystrix. When the error rate (exception) of "some article statistics page" reaches a certain amount (which can be configured), Hystrix immediately cuts off the connection between "some article statistics page" and the database. Ensure that the "some article statistics page" will no longer consume database resources. On the other hand, the error rate of "View article details page" is very low or the system delay is very low, so Hystrix releases it normally. In order to achieve the role of isolation.

However, Hystrix is more often used in the use scenario of RPC (Remote Procedure Call). In a distributed architecture, different systems will call each other. Once an exception occurs in a subsystem, Hystrix will immediately cut off the connection between other systems and the exception subsystem.

As shown in the following figure:

Suppose the "User Request" at the top of the above picture is to visit the personal center page of an online mall, which shows: profile, recommended goods, shopping cart and my express information, etc.

Personal data, recommended goods, shopping cart, my express information respectively come to the four subsystems. It probably consists of the following code:

Public void userCenter () {List recommendGoodsList = system1.queryGoodsList (); List myGoodsList = system2.queryMyGoodsList (); List myExpressInfoList = system3.queyrMyExpressList (); User myInfo = system4.queryMyInfo (); request.setAttribute ("recommendGoodsList", recommendGoodsList); request.setAttribute ("myGoodsList", myGoodsList); request.setAttribute ("myExpressInfoList", myExpressInfoList); request.setAttribute ("myInfo", myInfo); render ("userCenter.html") }

At this point, if there is a problem with the "express information" subsystem, as shown in the following figure:

Public void userCenter () {

List recommendGoodsList = system1.queryGoodsList ()

List myGoodsList = system2.queryMyGoodsList ()

List myExpressInfoList = system3.queyrMyExpressList ()

User myInfo = system4.queryMyInfo ()

Request.setAttribute ("recommendGoodsList", recommendGoodsList)

Request.setAttribute ("myGoodsList", myGoodsList)

Request.setAttribute ("myExpressInfoList", myExpressInfoList)

Request.setAttribute ("myInfo", myInfo)

Render ("userCenter.html")

}

At this point, a wait occurs in the fourth line of code "system3.queyrMyExpressList ();", which causes all requests to wait, as shown in the following figure:

What is even more frightening is that when all requests are waiting, the system resources will be quickly exhausted, causing the system to collapse and not even to restart the system (because requests come in and consume resources immediately after reboot). The waiting of one server may also cause other servers to wait (different servers often call data each other), when all business systems collapse, also known as avalanche effect.

When there is Hystrix isolation, each business will be managed in its own thread pool, when List myExpressInfoList = system3.queyrMyExpressList (); when a problem (delay or error) reaches a certain amount, system3.queyrMyExpressList (); data will be immediately returned (usually empty data, the degraded function will return the result) without waiting, so as to ensure the normal operation of the system.

Let's go back to the definition of Hystrix:

"Hystrix is used by distributed systems, providing delay and fault tolerance, isolating access points of remote systems, access and third-party libraries, preventing cascading failures, and ensuring that complex distributed systems can still be resilient in the face of inevitable failures."

At this point, do you understand the definition of Hystrix?

To its credit, Hystrix manages thread pools and provides a visual monitoring system, hystrix-dashboard, to view each thread pool. If you have docker installed on your computer, you can start hystrix-dashboard with the following command:

Docker run-- rm-ti-p 7979V 7979 kennedyoliveira/hystrix-dashboard

It doesn't matter if you don't have a docker environment, you can also compile and start hystrix-dashboard by:

$git clone https://github.com/Netflix/Hystrix.git

$cd Hystrix/hystrix-dashboard

$.. / gradlew appRun

> Building >: hystrix-dashboard:appRun > Running at http://localhost:7979/hystrix-dashboard

After starting hystrix-dashboard successfully, you can visit http://localhost:7979/hystrix-dashboard through a browser to view the hystrix-dashboard.

As shown in the following figure:

After filling in the stream address of the Hystrix of "web App", click the "monitor stream" button, and we can see how Hystrix monitors the entire application.

Note: in the application developed by the Jboot framework, we need to configure "jboot.hystrix.url = / hystrix.stream" in jboot.properties, where the stream address of Hystrix is: http://host:port/hystrix.stream.

Srping Cloud and so on have a lot of things to do, unlike Jboot one-line configuration can be used, please check its help documentation.

This is the end of the introduction of Hystrix, this article is not concerned with how to use Hystrix, there are many articles on the Internet to explain how to use Hystrix, but there is no article to systematically introduce what is Hystrix. I think understanding any technology is much more important than using it, which is why I wrote this article.

The above is how to implement the Hystrix open source framework, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report