当前位置:网站首页>断路器HystrixCircuitBreaker
断路器HystrixCircuitBreaker
2022-06-30 09:50:00 【全栈程序员站长】
package com.netflix.hystrix;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import com.netflix.hystrix.HystrixCommandMetrics.HealthCounts;
import rx.Subscriber;
import rx.Subscription;
public interface HystrixCircuitBreaker {
boolean allowRequest();
boolean isOpen();
void markSuccess();
void markNonSuccess();
boolean attemptExecution();
class Factory {
// String is HystrixCommandKey.name() (we can't use HystrixCommandKey directly as we can't guarantee it implements hashcode/equals correctly)
private static ConcurrentHashMap<String, HystrixCircuitBreaker> circuitBreakersByCommand = new ConcurrentHashMap<String, HystrixCircuitBreaker>();
}
class HystrixCircuitBreakerImpl implements HystrixCircuitBreaker {
}
static class NoOpCircuitBreaker implements HystrixCircuitBreaker {
}
}
下面先看一下该接口的抽象方法:
allowRequest(): 每个Hystrix命令的请求都通过它判断是否被执行(已经不再使用,使用attemptExecution()方法进行判断) attemptExecution(): 每个Hystrix命令的请求都通过它判断是否被执行 isOpen(): 返回当前断路器是否打开 markSuccess(): 用来关闭断路器 markNonSuccess: 用来打开断路器
下面看一下该接口中的类:
Factory: 维护了一个Hystrix命令和HystrixCircuitBreaker的关系的集合ConcurrentHashMap<String, HystrixCircuitBreaker> circuitBreakersByCommand。其中key通过HystrixCommandKey来定义,每一个Hystrix命令都需要有一个Key来标识,同时根据这个Key可以找到对应的断路器实例。 NoOpCircuitBreaker: 一个啥都不做的断路器,它允许所有请求通过,并且断路器始终处于闭合状态 HystrixCircuitBreakerImpl:断路器的另一个实现类。
HystrixCircuitBreakerImpl介绍
在该类中定义了断路器的五个核心对象:
HystrixCommandProperties properties:断路器对应实例的属性集合对象/断路器对应HystrixCommand实例的属性对象 HystrixCommandMetrics metrics:用来让HystrixCommand记录各类度量指标的对象 AtomicReference<Status> status: 用来记录断路器的状态,默认是关闭状态 AtomicLong circuitOpened:断路器打开的时间戳,默认-1,表示断路器未打开 AtomicReference<Subscription> activeSubscription: 记录HystrixCommand
对接口的实现如下:
@Override
public boolean isOpen() {
if (properties.circuitBreakerForceOpen().get()) {
return true;
}
if (properties.circuitBreakerForceClosed().get()) {
return false;
}
return circuitOpened.get() >= 0;
}
用来判断断路器是否打开或关闭。主要步骤有:
如果断路器强制打开,返回true 如果断路器强制关闭,返回false 判断circuitOpened的值,如果大于等于0,返回true, 否则返回false
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101128.html原文链接:https://javaforall.cn
边栏推荐
- Jinbei LT6 is powerful in the year of the tiger, making waves
- June training (day 30) - topology sorting
- After recording 7000 IELTS words in 100 sentences, there are only 1043 words (including simple words such as I and you)
- The programmer was beaten.
- ArcGIS Pro scripting tool (5) - delete duplicates after sorting
- How to seize the opportunity of NFT's "chaos"?
- unable to convert expression into double array
- RobotFramework学习笔记:环境安装以及robotframework-browser插件的安装
- 今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
- Who should the newly admitted miners bow to in front of the chip machine and the graphics card machine
猜你喜欢
ArcGIS Pro scripting tool (6) -- repairing CAD layer data sources
Implementation of monitor program with assembly language
著名画家史国良《丰收时节》数字藏品上线长城数艺
Dyson design award, changing the world with sustainable design
[email protected] control a dog's running on OLED"/>
Skill combing [email protected] control a dog's running on OLED
"Kunming City coffee map" activity was launched again
Koreano essential creates a professional style
[email protected]基于51系列单片机的智能仪器教具"/>
技能梳理[email protected]基于51系列单片机的智能仪器教具
Deploy lvs-dr cluster
“昆明城市咖啡地图”再度开启,咖啡拉近城市距离
随机推荐
The famous painter shiguoliang's "harvest season" digital collection was launched on the Great Wall Digital Art
CVPR 2022 | Tsinghua & bytek & JD put forward BRT: Bridging Transformer for vision and point cloud 3D target detection
Leetcode question brushing (I) -- double pointer (go Implementation)
Foster design method
AttributeError: ‘Version‘ object has no attribute ‘major‘
“昆明城市咖啡地图”再度开启,咖啡拉近城市距离
Memorize the text and remember the words. Read the text and remember the words. Read the article and remember the words; 40 articles with 3500 words; 71 articles broke through the words in the middle
记一次实习的经历,趟坑必备(一)
“昆明城市咖啡地圖”活動再度開啟
Compétences Comb 27 @ Body sense Manipulator
ArcGIS Pro脚本工具(5)——排序后删除重复项
华南产业集团发力数字经济,城链科技发布会成功召开
Why can't you rob scientists of NFT
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
Gd32 RT thread ota/bootloader driver function
Node environment configuration
Eth is not connected to the ore pool
Who should the newly admitted miners bow to in front of the chip machine and the graphics card machine
MIT-6874-Deep Learning in the Life Sciences Week4
Jinbei LT6 is powerful in the year of the tiger, making waves