当前位置:网站首页>Circuitbreaker fuse of resilience4j - circuitbreakerevent event
Circuitbreaker fuse of resilience4j - circuitbreakerevent event
2022-06-12 10:11:00 【Wang Rushuang】
List of articles
Resilience4j One of the highlights of the framework It is to realize the event driven architecture design , So I think the part of event registration and release is worth learning , I hope the following explanations can help you understand
One : CircuitBreakerEvent
CircuitBreaker A total definition of 6 Kind of Event
If you want to use events , The event user must be registered , If you want to perform an operation after the call is successful , You have to register CircuitBreakerOnSuccessEvent The consumer
circuitBreaker.getEventPublisher()
.onSuccess(event -> logger.info(" Successful call "));
If you want all events to trigger , General consumers can be registered
circuitBreaker.getEventPublisher()
.onEvent(event -> logger.info(" General events "));
Two : CircuitBreaker.EventPublisher event
Inherit Core.EventPublisher , Is the event publisher interface , It is mainly used to register events
/**
* An EventPublisher can be used to register event consumers.
* Event handler , Used to direct to EventProcessor Register to handle six events EventConsumer, And different processing strategies can be registered according to different event types ,onEvent All events trigger by default
*/
interface EventPublisher extends
io.github.resilience4j.core.EventPublisher<CircuitBreakerEvent> {
// The event consumption policy triggered when the request succeeds
EventPublisher onSuccess(EventConsumer<CircuitBreakerOnSuccessEvent> eventConsumer);
// Event consumption policy triggered when the request fails
EventPublisher onError(EventConsumer<CircuitBreakerOnErrorEvent> eventConsumer);
// The event consumption strategy triggered when the circuit breaker status changes
EventPublisher onStateTransition(
EventConsumer<CircuitBreakerOnStateTransitionEvent> eventConsumer);
// The event consumption strategy triggered by the reset of the fusing state
EventPublisher onReset(EventConsumer<CircuitBreakerOnResetEvent> eventConsumer);
// Exception in request , But it is an event consumption strategy triggered by a negligible exception
EventPublisher onIgnoredError(
EventConsumer<CircuitBreakerOnIgnoredErrorEvent> eventConsumer);
// The event consumption policy that the fuse opening request does not pass
EventPublisher onCallNotPermitted(
EventConsumer<CircuitBreakerOnCallNotPermittedEvent> eventConsumer);
}
Specific reasons CircuitBreakerStateMachine.CircuitBreakerEventProcessor Realization , That is, consumers , Also a publisher
//CircuitBreaker Event handler for CircuitBreakerEventProcessor Is the publisher of the event , At the same time, it is also the consumer of the event
private class CircuitBreakerEventProcessor extends
EventProcessor<CircuitBreakerEvent> implements EventConsumer<CircuitBreakerEvent>,
EventPublisher {
// Different event types create different consumers
@Override
public EventPublisher onSuccess(
// establish CircuitBreakerOnSuccessEvent consumer
// The event class name will be used as EventProcessor Class to store consumer collections Map Medium key,value This is the created event example
EventConsumer<CircuitBreakerOnSuccessEvent> onSuccessEventConsumer) {
// Register the consumer created above
registerConsumer(CircuitBreakerOnSuccessEvent.class.getSimpleName(),
onSuccessEventConsumer);
return this;
}
....
}
Relations are as follows :
EventConsumer: Event consumer , It's a functional interface , only one consumeEvent() Method , Define the specific behavior of consumption
// Event consumer interface ( The observer )
@FunctionalInterface
public interface EventConsumer<T> {
// Used for processing T Type of event
void consumeEvent(T event);
}
EventPublisher: Event publisher , only one onEvent() Method , It is mainly used to register general events , That is, all event types will consume this event
public interface EventPublisher<T> {
// Used to set processing T Consumers of the event
void onEvent(EventConsumer<T> onEventConsumer);
}
EventProcessor: It is mainly used to register consumers and call consumers to consume
public class EventProcessor<T> implements EventPublisher<T> {
// Used to store general event consumers
List<EventConsumer<T>> onEventConsumers = new CopyOnWriteArrayList();
// Used to store defined 6 Species specific CircuitBreakerEvent event
//key:6 Class names of events ,value: Examples of events
ConcurrentMap<String, List<EventConsumer<T>>> eventConsumerMap = new ConcurrentHashMap();
private boolean consumerRegistered;
public EventProcessor() {
}
public boolean hasConsumers() {
return this.consumerRegistered;
}
// Registration events
//key:6 Class names of events , Such as CircuitBreakerOnSuccessEvent、CircuitBreakerOnErrorEvent etc.
//value: Specific consumer examples
//CircuitBreakerEventProcessor.onSuccess() And so on 6 Event calls
public synchronized void registerConsumer(String className, EventConsumer<? extends T> eventConsumer) {
this.consumerRegistered = true;
this.eventConsumerMap.compute(className, (k, consumers) -> {
if (consumers == null) {
List consumersx = new ArrayList();
consumersx.add(eventConsumer);
return consumersx;
} else {
consumers.add(eventConsumer);
return consumers;
}
});
}
// Call the consumer consumption event , return true Disposed of ,false Untreated
public <E extends T> boolean processEvent(E event) {
boolean consumed = false;
// General consumer consumption events
if (!this.onEventConsumers.isEmpty()) {
this.onEventConsumers.forEach((onEventConsumer) -> {
onEventConsumer.consumeEvent(event);
});
consumed = true;
}
// Specific consumer consumption events
if (!this.eventConsumerMap.isEmpty()) {
//*** Select the consumers that match the type from the registered consumers
// I.e. screening key For the consumer who specifies the class name of the event , Such as CircuitBreakerOnSuccessEvent
List<EventConsumer<T>> eventConsumers = (List)this.eventConsumerMap.get(event.getClass().getSimpleName());
if (eventConsumers != null && !eventConsumers.isEmpty()) {
eventConsumers.forEach((consumer) -> {
//*** Call the consumer consumption event
consumer.consumeEvent(event);
});
consumed = true;
}
}
return consumed;
}
// Used to register common events
public synchronized void onEvent(@Nullable EventConsumer<T> onEventConsumer) {
this.consumerRegistered = true;
this.onEventConsumers.add(onEventConsumer);
}
}
3、 ... and : How to realize consumption registration
Take the successful call as an example , Go to... Immediately after registration ConcurrentMap<String, List<EventConsumer<T>>> eventConsumerMap = new ConcurrentHashMap() When registering a consumer this.registerConsumer(CircuitBreakerOnSuccessEvent.class.getSimpleName(), onSuccessEventConsumer); It specifies key by “CircuitBreakerOnSuccessEvent”, Create... Before consumption CircuitBreakerOnSuccessEvent event , When consuming, you will get the class name of the event created , Go to map Medium filter List<EventConsumer<T>> eventConsumers = this.eventConsumerMap .get(event.getClass().getSimpleName()); This creates a closed loop
Different behaviors correspond to different behavior implementations and different events , This part of code implementation thinking is worth learning
Four : Sort out the calling relationship
establish circuitBreaker after , You can use to get EventPublisher To call ——> onSuccess To register circuitBreaker.getEventPublisher().onSuccess(event -> log.error(" Consumption success ") ); ——> Execute the back-end call , After the semaphore call is successful , call ——>circuitBreaker.onSuccess()——>publishSuccessEvent()——> establish CircuitBreakerOnSuccessEvent event ——>CircuitBreakerEventProcessor.consumeEvent()——>EventProcessor.processEvent() Consumer events
边栏推荐
- 005: difference between data lake and data warehouse
- Implementation of fruit mall wholesale platform based on SSM
- CentOS 7 installing MySQL 8
- Auto. JS learning notes 8: some common and important APIs
- 1268_FreeRTOS任务上下文切换的实现
- 2021-02-21
- UE4_以现成资源探索创建背景场景的方法
- 链式哈希表
- Canal ha mode configuration
- Tp6 debugging (trace)
猜你喜欢

Shen Min, CIO of science and technology innovator Digital China Group: the best practice model is failing, and open source accelerates Distributed Innovation

基于SSM实现水果商城批发平台

Auto. JS learning notes 7: JS file calls functions and variables in another JS file to solve various problems of call failure

Halcon combined with C # to detect surface defects -- affine transformation (III)

哈希表的理论讲解
![[CEGUI] log system](/img/5d/c8f76194b1ae2a62d3f5a1e84a5603.jpg)
[CEGUI] log system

MySQL III Configuration file & log file

In 2026, the capacity of China's software defined storage market will be close to US $4.51 billion

MYSQL的最左匹配原则的原理讲解

SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
随机推荐
Essentials reading notes
JVM (V) Virtual machine class loading (parental delegation mechanism)
Research on autojs wechat: the control IP in wechat on different versions of wechat or simulators is different.
C language recursive folder code
Quickly build oncyber io
First NFT platform in dfinity Ecology: impossible thoughts
np. Meshgrid() function and coordinate position generation in 3D space and numpy Introduction to repeat() function
Implementation of fruit mall wholesale platform based on SSM
原始套接字使用
[CEGUI] font loading optimization
How CEPH improves storage performance and storage stability
Chained hash table
总有一根阴线(上影线)会阻止多军前进的脚步,总有一个阳线(下影线)会阻挡空军肆虐的轰炸
Explication du principe d'appariement le plus à gauche de MySQL
MySQL III Configuration file & log file
MySQL索引常见问题
7-5 zhe zhe playing games
MySQL index FAQs
2021-02-21
2021-09-15