插件
可以通过实现插件来改变Hystrix的行为。可以通过HystrixPlugins来注册自定义插件,这些插件会被应用到HystrixCommand,HystrixObservableCommand和HystrixCollapser。
插件类型
- 事件通知
在HystrixCommand和HystrixObservableCommand执行过程中会触发一些时间,实现HystrixEventNotifier可以监听这些事件进行一些告警和数据收集。
- 发布metrics
通过实现HystrixMetricsPublisher可以获取所有实例的metrics信息,这样我们就可以获取和存储这些metrics信息,以便后续处理。默认的实现不会发布这些metrics信息。
- 配置策略
通过实现HystrixPropertiesStrategy可以改变配置的默认实现。系统默认使用Archaius.
- 并发策略
Hystrix实现ThreadLocal,Callable,Runnable,ThreadPoolExecutor和BlockingQueue来实现线程隔离和请求作用域。
Hystrix默认已经实现了这些功能,也提供了插件让用户通过实现HystrixConcurrencyStrategy可以实现自定义的组件:
getThreadPool和getBlockingQueue方法可以让你实现自定义的线程池和队列。
wrapCallable方法可以让你对Callable进行处理,
getRequestVarible方法实现一个请求作用域。
- HystrixCommandExecutionHook
通过实现HystrixCommandExecutionHook可以在HystrixCommand或HystrixObservableCommand执行期间的响应阶段进行回调。
HystrixCommandExecutionHook method | when Hystrix calls the method |
---|---|
onStart |
before the HystrixInvokable begins executing |
onEmit |
whenever the HystrixInvokable emits a value |
onError |
if the HystrixInvokable fails with an exception |
onSuccess |
if the HystrixInvokable completes successfully |
onThreadStart |
at the start of thread execution if the HystrixInvokable is a HystrixCommand executed using the THREAD ExecutionIsolationStrategy |
onThreadComplete |
at the completion of thread execution if the HystrixInvokable is a HystrixCommand executed using the THREAD ExecutionIsolationStrategy |
onExecutionStart |
when the user-defined execution method in the HystrixInvokable begins |
onExecutionEmit |
whenever the user-defined execution method in the HystrixInvokable emits a value |
onExecutionError |
when the user-defined execution method in the HystrixInvokable fails with an exception |
onExecutionSuccess |
when the user-defined execution method in the HystrixInvokable completes successfully |
onFallbackStart |
if the HystrixInvokable attempts to call the fallback method |
onFallbackEmit |
whenever the fallback method in the HystrixInvokable emits a value |
onFallbackError |
if the fallback method in the HystrixInvokable fails with an exception or does not exist when a call attempt is made |
onFallbackSuccess |
if the fallback method in the HystrixInvokable completes successfully |
onCacheHit |
if the response to the HystrixInvokable is found in the HystrixRequestCache |
怎么使用