博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊spring cloud的HystrixAutoConfiguration
阅读量:6612 次
发布时间:2019-06-24

本文共 7372 字,大约阅读时间需要 24 分钟。

本文主要研究一下spring cloud的HystrixAutoConfiguration

HystrixAutoConfiguration

spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/cloud/netflix/hystrix/HystrixAutoConfiguration.java

/** * Auto configuration for Hystrix. * * @author Christian Dupuis * @author Dave Syer */@Configuration@ConditionalOnClass({ Hystrix.class, HealthIndicator.class })@AutoConfigureAfter({ HealthIndicatorAutoConfiguration.class })public class HystrixAutoConfiguration {    @Bean    @ConditionalOnEnabledHealthIndicator("hystrix")    public HystrixHealthIndicator hystrixHealthIndicator() {        return new HystrixHealthIndicator();    }    @Bean    @ConditionalOnProperty(value = "management.metrics.hystrix.enabled", matchIfMissing = true)    public HystrixMetricsBinder hystrixMetricsBinder() {        return new HystrixMetricsBinder();    }    /**     * See original {@link org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpointAutoConfiguration}     */    @Configuration    @ConditionalOnWebApplication(type = SERVLET)    @ConditionalOnBean(HystrixCommandAspect.class) // only install the stream if enabled    @ConditionalOnClass({ HystrixMetricsStreamServlet.class })    @EnableConfigurationProperties(HystrixProperties.class)    protected static class HystrixServletAutoConfiguration {        @Bean        @ConditionalOnEnabledEndpoint        public HystrixStreamEndpoint hystrixStreamEndpoint(HystrixProperties properties) {            return new HystrixStreamEndpoint(properties.getConfig());        }        @Bean        public HasFeatures hystrixStreamFeature() {            return HasFeatures.namedFeature("Hystrix Stream Servlet", HystrixMetricsStreamServlet.class);        }    }    @Configuration    @ConditionalOnWebApplication(type = REACTIVE)    @ConditionalOnBean(HystrixCommandAspect.class) // only install the stream if enabled    @ConditionalOnClass({ DispatcherHandler.class })    @EnableConfigurationProperties(HystrixProperties.class)    protected static class HystrixWebfluxManagementContextConfiguration {        @Bean        @ConditionalOnEnabledEndpoint        public HystrixWebfluxEndpoint hystrixWebfluxController() {            Observable
serializedDashboardData = HystrixDashboardStream.getInstance().observe() .concatMap(dashboardData -> Observable.from(SerialHystrixDashboardData.toMultipleJsonStrings(dashboardData))); Publisher
publisher = RxReactiveStreams.toPublisher(serializedDashboardData); return new HystrixWebfluxEndpoint(publisher); } @Bean public HasFeatures hystrixStreamFeature() { return HasFeatures.namedFeature("Hystrix Stream Webflux", HystrixWebfluxEndpoint.class); } }}
这里有几个bean,一个是HystrixHealthIndicator,还有HystrixMetricsBinder。另外关于endpoint,区分了servlet及webflux版本。

HystrixHealthIndicator

spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/cloud/netflix/hystrix/HystrixHealthIndicator.java

/** * A {@link HealthIndicator} implementation for Hystrix circuit breakers. * 

* This default implementation will not change the system state (e.g. OK) but * includes all open circuits by name. * * @author Christian Dupuis */public class HystrixHealthIndicator extends AbstractHealthIndicator { private static final Status CIRCUIT_OPEN = new Status("CIRCUIT_OPEN"); @Override protected void doHealthCheck(Builder builder) throws Exception { List

openCircuitBreakers = new ArrayList<>(); // Collect all open circuit breakers from Hystrix for (HystrixCommandMetrics metrics : HystrixCommandMetrics.getInstances()) { HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory .getInstance(metrics.getCommandKey()); if (circuitBreaker != null && circuitBreaker.isOpen()) { openCircuitBreakers.add(metrics.getCommandGroup().name() + "::" + metrics.getCommandKey().name()); } } // If there is at least one open circuit report OUT_OF_SERVICE adding the command // group // and key name if (!openCircuitBreakers.isEmpty()) { builder.status(CIRCUIT_OPEN).withDetail("openCircuitBreakers", openCircuitBreakers); } else { builder.up(); } }}

检查断路器的状态

HystrixMetricsBinder

micrometer-core-1.0.5-sources.jar!/io/micrometer/core/instrument/binder/hystrix/HystrixMetricsBinder.java

@NonNullApi@NonNullFieldspublic class HystrixMetricsBinder implements MeterBinder {    @Override    public void bindTo(MeterRegistry registry) {        // Keeps references of existing Hystrix plugins.        HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();        HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();        HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();        HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();        HystrixPlugins.reset();        // Registers existing plugins except the new MicroMeter Strategy plugin.        HystrixPlugins.getInstance().registerMetricsPublisher(new MicrometerMetricsPublisher(registry));        HystrixPlugins.getInstance().registerConcurrencyStrategy(concurrencyStrategy);        HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);        HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);        HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);    }}
这里导出metrics到micrometer

HystrixStreamEndpoint

spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java

/** * {@link org.springframework.boot.actuate.endpoint.annotation.Endpoint} to expose a Jolokia {@link HystrixMetricsStreamServlet}. * * @author Phillip Webb * @since 2.0.0 */@ServletEndpoint(id = "hystrix.stream")public class HystrixStreamEndpoint implements Supplier
{ private final Map
initParameters; public HystrixStreamEndpoint(Map
initParameters) { this.initParameters = initParameters; } @Override public EndpointServlet get() { return new EndpointServlet(HystrixMetricsStreamServlet.class) .withInitParameters(this.initParameters); }}
这个是基于servlet的

HystrixWebfluxEndpoint

spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/cloud/netflix/hystrix/HystrixWebfluxEndpoint.java

@RestControllerEndpoint(id = "hystrix.stream")public class HystrixWebfluxEndpoint {    private final Flux
stream; public HystrixWebfluxEndpoint(Publisher
dashboardData) { stream = Flux.interval(Duration.ofMillis(500)).map(aLong -> "{\"type\":\"ping\"}") .mergeWith(dashboardData).share(); } // path needs to be empty, so it registers correct as /actuator/hystrix.stream @GetMapping(path = "", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux
hystrixStream() { return stream; }}
这个是基于webflux的

小结

HystrixAutoConfiguration主要是配置了HystrixHealthIndicator、HystrixMetricsBinder以及HystrixEndpoint(分servlet与webflux版本)。

doc

转载地址:http://cgaso.baihongyu.com/

你可能感兴趣的文章
关于nginx信号控制的“故障”案例
查看>>
脚本安装denyhosts
查看>>
Struts2中javascrpit实现form的提交二
查看>>
Linux下常用的磁盘管理技术LVM
查看>>
Silverlight杂记之HTTP通信WebClient介绍
查看>>
动态路由协议之RIP配置详解
查看>>
(apache+tomcat集群+memcached番外篇)单台tomcat的session信息的2种持久化方式
查看>>
nginx1.9+做TCP代理(端口转发)
查看>>
Windows 2012 计划任务发送邮件
查看>>
Samba服务介绍及配置
查看>>
Android系统的开机画面显示过程分析(5)
查看>>
DataGrid Web Control 连载之四
查看>>
在Windows Server 2008上实现带宽控制---QoS
查看>>
Web Client Software Factory系列(1):初识与预备知识
查看>>
HTML元素的默认CSS设置介绍
查看>>
CSS-图片不变形设置
查看>>
Git异常:fatal: could not create work tree dir 'XXX': No such file or directory
查看>>
JavaScript:综合案例-表单验证
查看>>
GNU make manual 翻译(八十二)
查看>>
python批量下载图片的三种方法
查看>>