当前位置:网站首页>Circuitbreaker fuse of resilience4j -- Measurement of circuitbreakermetrics index
Circuitbreaker fuse of resilience4j -- Measurement of circuitbreakermetrics index
2022-06-12 10:10:00 【Wang Rushuang】
List of articles
Mentioned in the last article ,onError() or onSuccess() I will calculate the error rate and other indicators , Specifically by CircuitBreakerMetrics Realization 
One :CircuitBreaker.Metrics Indicator measurement interface
This interface defines a series of methods to obtain metrics , These include the following :
// Metrics interface , Used to observe various indexes of fuse
interface Metrics {
// Returns the failure rate percentage , If the number of calls is less than MINIMUM_NUMBER_OF_CALLS, return -1
float getFailureRate();
// Returns the percentage of slow calls , If the number of calls is less than MINIMUM_NUMBER_OF_CALLS, return -1
float getSlowCallRate();
// Total slow call requests
int getNumberOfSlowCalls();
// Total number of successful slow calls
int getNumberOfSlowSuccessfulCalls();
// Total slow call failures
int getNumberOfSlowFailedCalls();
// The maximum number of calls to the ring buffer
int getNumberOfBufferedCalls();
// Total number of failed calls
int getNumberOfFailedCalls();
// In the open state , Returns the number of currently disallowed request calls , In the closed and semi open states , Always return to 0
long getNumberOfNotPermittedCalls();
// Total number of successful calls
int getNumberOfSuccessfulCalls();
}
Two :CircuitBreakerMetrics class
yes CircuitBreaker.Metrics Implementation class of interface , The specific content is to realize CircuitBreaker.Metrics Method of definition
// Slow call rate
private float getSlowCallRate(Snapshot snapshot) {
int bufferedCalls = snapshot.getTotalNumberOfCalls();
if (bufferedCalls == 0 || bufferedCalls < minimumNumberOfCalls) {
return -1.0f;
}
return snapshot.getSlowCallRate();
}
// Failure rate
private float getFailureRate(Snapshot snapshot) {
// Total number of calls
int bufferedCalls = snapshot.getTotalNumberOfCalls();
if (bufferedCalls == 0 || bufferedCalls < minimumNumberOfCalls) {
//ringBitSet The number of middle is not up to ringBufferSize size , return -1.0f, Failure rate is not calculated
return -1.0f;
}
// Return failure rate :totalNumberOfFailedCalls * 100.0f / totalNumberOfCalls
return snapshot.getFailureRate();
}
onError() and onSuccess() The method is the state machine mentioned in the previous article CircuitBreakerStateMachine In the state class of onError() and onSuccess() Method called , Check if the error rate threshold has been reached checkIfThresholdsExceeded() Then call the state transition method of the state machine stateTransition()
/**
* Records a successful call and checks if the thresholds are exceeded.
* Check whether it is a slow call when the call is successful , If it is a slow call , Publish the slow call event first , Then count the slow call rate
* @return the result of the check
*/
public Result onSuccess(long duration, TimeUnit durationUnit) {
Snapshot snapshot;
if (durationUnit.toNanos(duration) > slowCallDurationThresholdInNanos) {
snapshot = metrics.record(duration, durationUnit, Outcome.SLOW_SUCCESS);
} else {
snapshot = metrics.record(duration, durationUnit, Outcome.SUCCESS);
}
return checkIfThresholdsExceeded(snapshot);
}
/**
* This method is called when it fails , Slow release / Error events , And check whether the threshold is reached
* 1. Record the request first , That is, the total number of requests , Error request times, etc +1
* 2. Calculate the failure rate / Slow call rate , Whether the threshold is reached
*/
public Result onError(long duration, TimeUnit durationUnit) {
Snapshot snapshot;
if (durationUnit.toNanos(duration) > slowCallDurationThresholdInNanos) {
// Slow call , call Core.Metrics Record the number of requests
snapshot = metrics.record(duration, durationUnit, Outcome.SLOW_ERROR);
} else {
// Error call , call Core.Metrics Record the number of requests
snapshot = metrics.record(duration, durationUnit, Outcome.ERROR);
}
return checkIfThresholdsExceeded(snapshot);
}
// Check the error rate / Whether the slow call rate reaches the threshold
private Result checkIfThresholdsExceeded(Snapshot snapshot) {
// Get the failure rate
float failureRateInPercentage = getFailureRate(snapshot);
if (failureRateInPercentage == -1) {
// Less than buffer size , The check threshold has not been reached
return Result.BELOW_MINIMUM_CALLS_THRESHOLD;
}
if (failureRateInPercentage >= failureRateThreshold) {
// Threshold exceeded
return Result.ABOVE_THRESHOLDS;
}
// Slow call rate
float slowCallsInPercentage = getSlowCallRate(snapshot);
if (slowCallsInPercentage >= slowCallRateThreshold) {
return Result.ABOVE_THRESHOLDS;
}
return Result.BELOW_THRESHOLDS;
}
It also defines enumeration values to check whether the threshold is reached :
enum Result {
BELOW_THRESHOLDS,
ABOVE_THRESHOLDS,
BELOW_MINIMUM_CALLS_THRESHOLD
}
summary
CircuitBreakerMetrics It is mainly used to calculate various indicators of measurement
边栏推荐
- 远程桌面不能复制粘贴解决办法
- 在线电路仿真以及开源电子硬件设计介绍
- MYSQL的最左匹配原則的原理講解
- In 2026, the capacity of China's software defined storage market will be close to US $4.51 billion
- np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍
- 7-4 network red dot punch in strategy (DFS)
- Auto. JS learning note 9: basic methods such as using the script engine, starting the script file with the specified path, and closing
- Web3.0与数字时尚,该如何落地?
- Chained hash table
- 奇葩错误 -- 轮廓检测检测到边框、膨胀腐蚀开闭运算效果颠倒
猜你喜欢

在线电路仿真以及开源电子硬件设计介绍

链式哈希表

Auto. JS learning note 4: after autojs is packaged, most Huawei and other big brand mobile phones cannot be installed? This problem can be solved by using the simulator to remotely sign and package in

机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】

Crazy temporary products: super low price, big scuffle and new hope

MySQL索引常见问题

How high can C language reach by self-study alone?

JVM (IV) Class file structure (complete parsing of bytecode attached)
SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000

《真北》读书笔记
随机推荐
Implementation of fruit mall wholesale platform based on SSM
Canal ha mode configuration
【系统分析师之路】第十八章 复盘系统安全分析与设计
原始套接字使用
tp6调试(trace)
[chromium] location information kernel debugging
Introduction to on-line circuit simulation and open source electronic hardware design
Explication du principe d'appariement le plus à gauche de MySQL
UE4_以现成资源探索创建背景场景的方法
Auto. JS debugging: use the network mode of lightning simulator for debugging
[CEGUI] font loading optimization
5 most common CEPH failure scenarios
There is always a negative line (upper shadow line) that will stop the advance of many armies, and there is always a positive line (lower shadow line) that will stop the rampant bombing of the air for
MySQL VI Database lock
How CEPH improves storage performance and storage stability
True north reading notes
MYSQL的最左匹配原则的原理讲解
Spark complex structure data retrieval method
Web3.0与数字时尚,该如何落地?
7-13 地下迷宫探索(邻接表)