Preface
The Dromara community adds another member project!
Today I would like to introduce you to Akali.
It is lightweight and compact, comes and goes without a trace, and has less than 500 lines of code, but it can solve the main problems in high-traffic scenarios: hotspot processing and degradation processing.
introduce
Akali is a lightweight localized hotspot detection/downgrade framework that is suitable for large traffic scenarios and can easily solve scenarios such as concurrent queries with ultra-high traffic in the business. And it is extremely simple to access and use, you can access and use it in 10 seconds!
The concept of the Akali framework is to be compact, practical, and capable of fighting team battles with full health. It will leave the field with full health, and everything it goes will be nothingness.
Gitee:https://gitee.com/dromara/Akali
Github:https://github.com/bryan31/Akali
Official website: https://akali.yomahub.com/
use
Introduce dependencies:
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>akali</artifactId>
<version>1.0.1</version>
</dependency>
Hotspot any method
Just add @AkaliHot
this annotation, and any method can obtain hotspot detection, and use hotspot data to return during the hotspot period. After the hotspot passes, the original business logic will be automatically called.
Example: For example, if there is a product query business, pass in SkuCode and return product information. When a product is on sale, the number of visits will increase, but for the same SkuCode, the SkuInfo returned within a short time window is consistent. Our goal is that when a product SKU is queried in large numbers, the framework This product SKU can be mentioned as hot data in a short period of time, and the pressure on downstream business can be reduced by caching and returning it. When the hotspot value passes, the framework can automatically remove the hotspot value so that it can be queried in the original way.
Its essence is equivalent to monitoring hot spots in real time and caching the hot spot data for a short period of time.
The following example represents: when the same skuCode is called more than 50 times within 5 seconds, the value of this skuCode will be automatically raised as a hotspot and returned directly with the last return value. When the call is less than 50 times in 5 seconds, the framework will automatically remove this hotspot. Make it normally call your original code to perform logical calculations and return. It's all automatic.
@AkaliHot(grade = FlowGradeEnum.FLOW_GRADE_QPS, count = 50, duration = 5)
public SkuInfo getSkuInfo(String skuCode){
//do your biz and return sku info
}
In addition grade
to QPS
the dimensional statistics, the parameters also use Thread
the number as the dimensional statistics. for example:
@AkaliHot(grade = FlowGradeEnum.FLOW_GRADE_THREAD, count = 50, duration = 5)
public SkuInfo getSkuInfo(String skuCode){
//do your biz and return sku info
}
This means that if a certain skuCode has more than 50 threads running within 5 seconds, it will be mentioned as a hotspot and the hotspot data will be returned directly.
Students who are familiar with open source projects must have thought of JD.com's framework when they saw this. hotkey
Unlike Akali
JD.com hotkey
, it runs completely locally and does not rely on the server, and the access is hotkey
much more convenient than JD.com. Performance is exactly equivalent hotkey
.
Downgrade any method
Just add @AkaliFallback
annotations. Any method can be used to obtain downgrade functionality.
For example: A certain method needs to call an external interface, but the performance of the external interface is poor and time-consuming. When concurrency is high, the thread pool will be full, and the thread pool queue will gradually accumulate, causing timeouts or discarding. In severe cases, the entire system will be brought down.
At this time, we only need to add @AkaliFallback
annotations to this method to solve the problem.
@AkaliFallback(grade = FlowGradeEnum.FLOW_GRADE_THREAD, count = 100)
public String sayHi(String name){
return "hi,"+name;
}
public String sayHiFallback(String name){
return "fallback str";
}
The above annotation indicates that when the number of threads running at the same time in this method exceeds 100, the downgrade is triggered. The downgrade will automatically call the 原方法名+Fallback
method name (and the parameters must be consistent). When the downgrade is triggered, it will return directly fallback str
. When the number of threads is less than 100, the framework It will also automatically remove the downgrade and output hi,xxxx
.
If there is no fallback method defined in your class, an error will be reported when downgrading is triggered. Of course, you can throw an error in the downgrade method to let the upstream system know that your method has reached the bottleneck.
Precautions
Akali only targets Springboot and Spring environment, and all classes marked with @AkaliHot
or @AkaliFallback
must be registered in the spring context.
Akali will automatically scan all annotated classes in springboot. You do not need to do any configuration. In spring, you need to configure:
<bean class="com.yomahub.akali.strategy.FallbackStrategy"/>
<bean class="com.yomahub.akali.strategy.MethodHotspotStrategy"/>
<bean class="com.yomahub.akali.spring.AkaliScanner"/>
at last
If you are interested, please give Akali a little star on Gitee.