In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the java downgrade component Hystrix has what features of the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that you read this java degraded component Hystrix what features of the article will have a harvest, let's take a look at it.
1. Interviewer: can you briefly introduce the functions of Hystrix?
Problem analysis: understand the functions of Hystrix, but also get the inspiration of architecture design from the excellent design concept of Hystrix.
A: I used in the project that the system can be highly available for a long time under the protection of Hystrix. The commonly used functions are as follows:
1. 1. Fail-fast (Fast failure)
Fail-fast (Quick failure) and fast recovery mechanisms are provided in the Hystrix design.
Tip: I don't know if you have known the fail-fast mechanism before, or when interviewing for the basis of Java, the Iterator iterator in HashMap, the design of Iterator is fail-fast, * * fail-fast * * is a mechanism in the Java collection. When traversing a collection object with an iterator, if the contents of the collection object are modified (added, deleted, modified) during traversal, a Concurrent Modification Exception will be thrown.
The first time I learned HashMap, I didn't know much about fail-fast. I felt that quick failure was only applied in Java collection classes to prevent the concurrent operation of Java non-thread-safe collections. After learning to use Hystrix, the original rapid failure mechanism can also be applied to system architecture design to quickly fail requests that can not be processed in time (fail-fast) to reduce system load instead of queuing.
1.2.The elegant downgrade mechanism of Fallback
Fallback literally means to start back when you encounter Fall, and as soon as I learn the mechanism of Fallback, I immediately use it in the project.
Look at the real example:
@ Override @ Degrade (key = "getOrderByParamFromES", fallBackMethod = "getOrderByParamFromMysql") public OrderResult getOrderByParamFromES (OrderSearchParam param) {/ / go ES query. After return OrderResult;} / / fallBack, call the getOrderByParamFromMysql method public OrderResult getOrderByParamFromMysql (OrderSearchParam param) {/ / go to mysql query. Return OrderResult;}
Code interpretation:
FallBackMethod = "getOrderByParamFromMysql"
That is, after the failure of the ES query, the system automatically degrades the getOrderByParamFromMysql method and uses the mysql query. Under normal circumstances, getOrderByParamFromMysql will not be called unless Fall.
1.3. Thread / semaphore isolation mechanism thread isolation:
The request acquires the thread execution in the corresponding thread pool according to its own key, and dynamically sets thread pool parameters, which naturally isolates different requests and supports asynchrony to improve the performance of the interface. Different requests have no direct impact, for example, service1 requests are slow, but service2 and service3 can still work properly, but thread switching affects performance.
Semaphore isolation:
Service1, service2 and service3 are accessed in one request, and the timeout of the service1 request will result in the whole semaphore not being released and other requests unacceptable.
For requests with low latency (such as accessing the cache or accessing the database locally), the overhead of thread pools is very high, and you can consider other methods. For example, non-blocking semaphores (timeouts are not supported) to achieve service-dependent isolation. In most cases, however, Netflix prefers to use thread pools to isolate dependent services because the extra overhead is acceptable and supports all features, including timeouts.
2. Interviewer: just talking about thread isolation, is the timeout thread interrupt switch turned on in practice?
Problem analysis: examine the actual use experience, according to the characteristics of the thread itself, thread timeout, if not interrupted in time, thread resources will be wasted.
A: in general, we will turn on the timeout interrupt switch in order to release thread resources in time.
Set by hystrix.command.default.execution.isolation.thread.interruptOnTimeout = true.
However, if you need to finish the execution of the command, you can turn off the timeout interrupt if you are writing a database command or recording a critical log command.
(the interviewer nodded with satisfaction. I believe I do have experience in Hystrix maintenance.)
Interviewer: so how do you estimate the thread pool size?
A: to set the thread pool size correctly, you need to analyze the number of CPU, memory size, task type (compute-intensive, IO-intensive, etc.) of the deployed system. For computing-intensive tasks, the thread pool size is similar to the number of CPU to achieve optimal utilization. For IO-intensive tasks, optimal utilization can be achieved. The formula for calculating the optimal size of the thread pool is: thread pool size = number of CPU * (1 + task wait time / task processing time).
In-depth analysis of the history of Hystrix
Hystrix is derived from a project started by the Netflix API team in 2011. In 2012, Hystrix grew and matured, and many teams within Netflix adopted it. Today, tens of billions of thread isolation and hundreds of billions of semaphore isolation calls are performed daily over Hystrix on the Netflix. This greatly improves uptime and flexibility.
Under the high concurrent access, the stability of the service that the system depends on has a great impact on the system, and there are many uncontrollable factors, such as slow network connection, sudden busy resources, temporary unavailability, offline services and so on. If we want to build a stable and reliable distributed system, we must have such a fault-tolerant method.
Main functional Features of Hystrix
Fuse mechanism: fuses can be understood as fuses. Hystrix Command is used in the project. After a Hystrix Command request, if the number of service failures exceeds a certain percentage (for example, 50% by default), the circuit breaker will automatically fuse, the service will enter the fuse state, and subsequent requests will enter fallback.
Degradation mechanism: through fallbackMethod comments, when an exception occurs in a request for a backend service, in order to avoid affecting other business logic, you can use the method specified by the fallback method to return quickly, or enable "spare tire solution".
Environment isolation: including thread isolation and semaphore isolation.
Cache:Hystrix supports caching the result of a request, and the next request with the same key will fetch the result directly from the cache, reducing request overhead.
Hystrix Demo
Quickly understand the use of Hystrix fallback through a demo
@ Servicepublic class OrderQueryService {/ * order query interface * / @ HystrixCommand (fallbackMethod = "queryOrderBack") public List queryOrderFromRedis (String userId) {/ / todo reids query logic return orderlist } / * order query API failed downgrade solution * / @ SuppressWarnings ("unused") private String queryOrderBack (String userId) {/ / todo, for example, follow the ES query logic or directly prompt the user "Please try again later" / / todo notify the maintenance staff to deal with the fault return ";}}
Code interpretation:
When the program is normal, the query order service is the logic of the queryOrderFromRedis method. When the queryOrderFromRedis method throws an exception, according to the set proportion of the exception, or specify which exception, the threshold value is reached and the fallback switch is reached, the program switches to queryOrderBack, and the setting program follows the ES query logic or directly prompts the user to "Please try again later" and set it according to the business.
Under what circumstances will fallback be triggered? Failure TypeException classException.cause triggers fallbackFAILUREHystrixRuntimeExceptionunderlying exception (user-controlled) YESSEMAPHORE_REJECTEDHystrixRuntimeExceptionj.l.RuntimeExceptionYESSHORT_CIRCUITEDHystrixRuntimeExceptionj.l.RuntimeExceptionYESTHREAD_POOL_REJECTEDHystrixRuntimeExceptionj.u.c.RejectedExecutionExceptionYESTIMEOUTHystrixRuntimeExceptionj.u.c.TimeoutExceptionYES
FAILURE: any RuntimeException exception can activate fallback.
THREAD_POOL_REJECTED: when the number of tasks executed concurrently exceeds the sum of thread pools and queues, this is the thread isolation mechanism of Hystrix.
SEMAPHORE_REJECTED: similar to THREAD_POOL_REJECTED, when the number of concurrency of the service is greater than the semaphore threshold, it will enter the fallback. For example, the number of concurrent execution of the configuration program cannot be greater than 3. Because no matter which command execution method is called under semaphore isolation, Hystrix will not create a new thread to execute run () / construct (), so the calling program needs to create multiple threads to simulate concurrent calls to execute (). Finally, we can see that once the concurrent thread > 3, subsequent requests enter fallback.
SHORT_CIRCUITED: within a certain period of time, when user requests fail in more than a certain proportion, such as timeout, exception, thread concurrency reaches a limited maximum, etc., the circuit breaker will be turned on. After the circuit breaker is opened, all requests will go directly to fallback and can be passed. The circuitBreakerErrorThresholdPercentage method sets the percentage, which defaults to 50.
TIMEOUT: timeout request.
Appendix: Hystrix policy configuration / *-Statistical correlation-* / / time window for statistical scrolling. Default: the number of Buckets of 5000 Ms (taken from circuitBreakerSleepWindowInMilliseconds) private final HystrixProperty metricsRollingStatisticalWindowInMilliseconds; / / statistical window. Default: 10, one Buckets statistical private final HystrixProperty metricsRollingStatisticalWindowBuckets per second. / / number of buckets in the statisticalWindow / / whether to enable the monitoring and statistics function. Default: true private final HystrixProperty metricsRollingPercentileEnabled; / *-the threshold of fuse-* / whether the fuse is enabled during the whole statistical time. Default is 20. That is, at least 20 requests within the metricsRollingStatisticalWindowInMilliseconds (default 10s) before the fuse works. The private final HystrixProperty circuitBreakerRequestVolumeThreshold; / / fuse time window defaults to 5 seconds. The fuse interrupt request will enter the half-open state after 5 seconds, drop a request to come in and try again, if the request is successful, turn off the fuse, otherwise continue to wait for a fuse time window private final HystrixProperty circuitBreakerSleepWindowInMilliseconds; / / whether to enable the fuse, the default true. Start private final HystrixProperty circuitBreakerEnabled; / / default: 50%. When the error rate exceeds 50%, the fuse starts private final HystrixProperty circuitBreakerErrorThresholdPercentage; / / whether to force the fuse to block all requests. Default: false, not enabled. When set to true, all requests will be rejected directly to fallback private final HystrixProperty circuitBreakerForceOpen; / / whether to allow fuses to ignore errors, default false, do not enable private final HystrixProperty circuitBreakerForceClosed; / *-semaphore dependent-* / / when using semaphore isolation, the maximum number of concurrency of command calls. Default: 10 private final HystrixProperty executionIsolationSemaphoreMaxConcurrentRequests / / when semaphore isolation is used, the command fallback (degraded) calls the maximum number of concurrency. Default: 10 private final HystrixProperty fallbackIsolationSemaphoreMaxConcurrentRequests; / *-other-* / / use command call isolation. Default: thread isolation, ExecutionIsolationStrategy.THREAD private final HystrixProperty executionIsolationStrategy / / when thread isolation is used, the timeout is called. Default: 1 second private final HystrixProperty executionIsolationThreadTimeoutInMilliseconds; / / key of thread pool, which is used to determine the thread pool in which the command executes private final HystrixProperty executionIsolationThreadPoolKeyOverride; / / whether to enable fallback degradation policy default: true private final HystrixProperty fallbackEnabled; / / whether to call interrupt (Thread.interrupt ()) operation on the command when thread isolation is used. Default: true private final HystrixProperty executionIsolationThreadInterruptOnTimeout; / / whether request log is enabled, default: true private final HystrixProperty requestLogEnabled; / / whether request cache is enabled, default: true private final HystrixProperty requestCacheEnabled; / / Whether request caching is enabled// request merge is the maximum number of requests allowed, default: delay time of each command during Integer.MAX_VALUE private final HystrixProperty maxRequestsInBatch; / / batch processing, default: 10 millisecond private final HystrixProperty timerDelayInMilliseconds / / whether to enable request caching during batch processing. Default: enable private final HystrixProperty requestCacheEnabled; / * configure thread pool size. Default is 10 * / private final HystrixProperty corePoolSize; / * configure thread value waiting queue length. Default:-1 recommended value:-1 means no waiting for direct rejection. Tests show that thread pool using direct rejection policy + non-retracted thread pool of appropriate size is the most efficient. Therefore, it is not recommended to modify this value. The queueSizeRejectionThreshold,keepAliveTimeMinutes parameter is invalid * / private final HystrixProperty maxQueueSize when using a non-retractable thread pool
Other commonly used current limiting and degraded components
Sentinel: Alibaba Group's internal basic technology module, covering all the core scenarios. As a result, Sentinel has accumulated a large number of traffic consolidation scenarios and production practices. In 2018, Sentinel is open source and continues to evolve.
Resilience4j: also a lightweight fault-tolerant component inspired by Hystrix but designed primarily for Java 8 and functional programming. Lightweight is reflected in the fact that it uses only the Vavr library (formerly Javaslang) without any external dependencies. While Hystrix relies on Archaius, Archaius itself relies on many third-party packages, such as Guava, Apache Commons Configuration, and so on.
Sentinel and Hystrixresilience4j compare SentinelHystrixresilience4j isolation strategy semaphore isolation (concurrent thread current limit) thread pool isolation / semaphore isolation fuse degradation strategy based on response time, exception ratio, exception number and other exception ratio mode, timeout fuse based on exception ratio, Real-time response time statistics implementation sliding window (LeapArray) sliding window (based on RxJava) Ring Bit Buffer dynamic rule configuration support multiple configuration sources support multiple data sources limited support scalable SPI extension interface plug-in form interface based on annotations support current limitation based on QPS Support call relationship based on limited current limit support Rate Limiter cluster flow control does not support traffic shaping support preheat mode, uniform queuing mode and other complex scenarios do not support simple Rate Limiter mode system adaptive protection does not support console provides out-of-the-box console, configurable rules, view seconds monitoring, machine discovery and other simple monitoring view does not provide console Can be connected to other monitoring systems multi-language support Java / C++JavaJava open source community status active stop maintenance more active about "what are the functions of Hystrix, the degraded component of java?" this article ends here, thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the functions of Hystrix, the downgraded component of java". If you want to learn more, you are welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.