问题
For unit testing I would like to be able to subscribe to Hystrix events, particularly should there be an event when a circuit breaker opens or closes. I looked around for examples and it appears that the work around is to tap into the metrics stream and monitor the circuit breaker flags.
Since Hystrix is build on RxJava I thought there should be a subscription interface for events somewhere. Is there an easy way to subscribe to these type of events in Hystrix?
Thank you!
回答1:
You need to write Custom event notifier and registered it in HystrixPlugins. have look on below code.
public class CircuitBreakerHystrixEventNotifier extends HystrixEventNotifier{
public CircuitBreakerHystrixEventNotifier(){
}
public void markEvent(HystrixEventType eventType, HystrixCommandKey key) {
//here write code based on eventTypes.
}
}
You need to registered this CircuitBreakerHystrixEventNotifier in hystrix, see below
HystrixPlugins.getInstance().registerEventNotifier(getCircuitBreakerHystrixEventNotifier());
来源:https://stackoverflow.com/questions/39957791/is-it-possible-to-subscribe-to-the-circuitbreaker-opening-event-in-hystrix