聊聊spring cloud的HystrixCircuitBreakerConfiguration

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

HystrixCircuitBreakerConfiguration

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

@Configuration
public class HystrixCircuitBreakerConfiguration {

    @Bean
    public HystrixCommandAspect hystrixCommandAspect() {
        return new HystrixCommandAspect();
    }

    @Bean
    public HystrixShutdownHook hystrixShutdownHook() {
        return new HystrixShutdownHook();
    }

    @Bean
    public HasFeatures hystrixFeature() {
        return HasFeatures.namedFeatures(new NamedFeature("Hystrix", HystrixCommandAspect.class));
    }

    //FIXME: 2.0.0
    /*@Configuration
    @ConditionalOnProperty(value = "hystrix.metrics.enabled", matchIfMissing = true)
    @ConditionalOnClass({ HystrixMetricsPoller.class, GaugeService.class })
    @EnableConfigurationProperties(HystrixMetricsProperties.class)
    protected static class HystrixMetricsPollerConfiguration implements SmartLifecycle {

        private static Log logger = LogFactory
                .getLog(HystrixMetricsPollerConfiguration.class);

        @Autowired(required = false)
        private GaugeService gauges;

        @Autowired
        private HystrixMetricsProperties metricsProperties;

        private ObjectMapper mapper = new ObjectMapper();

        private HystrixMetricsPoller poller;

        private Set<String> reserved = new HashSet<String>(Arrays.asList("group", "name",
                "type", "currentTime"));

        @Override
        public void start() {
            if (this.gauges == null) {
                return;
            }
            MetricsAsJsonPollerListener listener = new MetricsAsJsonPollerListener() {
                @Override
                public void handleJsonMetric(String json) {
                    try {
                        @SuppressWarnings("unchecked")
                        Map<String, Object> map = HystrixMetricsPollerConfiguration.this.mapper
                                .readValue(json, Map.class);
                        if (map != null && map.containsKey("type")) {
                            addMetrics(map, "hystrix.");
                        }
                    }
                    catch (IOException ex) {
                        // ignore
                    }
                }

            };
            this.poller = new HystrixMetricsPoller(listener,
                    metricsProperties.getPollingIntervalMs());
            // start polling and it will write directly to the listener
            this.poller.start();
            logger.info("Starting poller");
        }

        private void addMetrics(Map<String, Object> map, String root) {
            StringBuilder prefixBuilder = new StringBuilder(root);
            if (map.containsKey("type")) {
                prefixBuilder.append((String) map.get("type"));
                if (map.containsKey("group")) {
                    prefixBuilder.append(".").append(map.get("group"));
                }
                prefixBuilder.append(".").append(map.get("name"));
            }
            String prefix = prefixBuilder.toString();
            for (String key : map.keySet()) {
                Object value = map.get(key);
                if (!this.reserved.contains(key)) {
                    if (value instanceof Number) {
                        String name = prefix + "." + key;
                        this.gauges.submit(name, ((Number) value).doubleValue());
                    }
                    else if (value instanceof Map) {
                        @SuppressWarnings("unchecked")
                        Map<String, Object> sub = (Map<String, Object>) value;
                        addMetrics(sub, prefix);
                    }
                }
            }
        }

        @Override
        public void stop() {
            if (this.poller != null) {
                this.poller.shutdown();
            }
        }

        @Override
        public boolean isRunning() {
            return this.poller != null ? this.poller.isRunning() : false;
        }

        @Override
        public int getPhase() {
            return Ordered.LOWEST_PRECEDENCE;
        }

        @Override
        public boolean isAutoStartup() {
            return true;
        }

        @Override
        public void stop(Runnable callback) {
            if (this.poller != null) {
                this.poller.shutdown();
            }
            callback.run();
        }

    }*/

    /**
     * {@link DisposableBean} that makes sure that Hystrix internal state is cleared when
     * {@link ApplicationContext} shuts down.
     */
    private class HystrixShutdownHook implements DisposableBean {

        @Override
        public void destroy() throws Exception {
            // Just call Hystrix to reset thread pool etc.
            Hystrix.reset();
        }

    }

}

这里主要是创建了HystrixCommandAspect以及HystrixShutdownHook

HystrixCommandAspect

hystrix-javanica-1.5.12-sources.jar!/com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect.java

/**
 * AspectJ aspect to process methods which annotated with {@link HystrixCommand} annotation.
 */
@Aspect
public class HystrixCommandAspect {

    private static final Map<HystrixPointcutType, MetaHolderFactory> META_HOLDER_FACTORY_MAP;

    static {
        META_HOLDER_FACTORY_MAP = ImmutableMap.<HystrixPointcutType, MetaHolderFactory>builder()
                .put(HystrixPointcutType.COMMAND, new CommandMetaHolderFactory())
                .put(HystrixPointcutType.COLLAPSER, new CollapserMetaHolderFactory())
                .build();
    }

    @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)")

    public void hystrixCommandAnnotationPointcut() {
    }

    @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)")
    public void hystrixCollapserAnnotationPointcut() {
    }

    @Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()")
    public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable {
        Method method = getMethodFromTarget(joinPoint);
        Validate.notNull(method, "failed to get method from joinPoint: %s", joinPoint);
        if (method.isAnnotationPresent(HystrixCommand.class) && method.isAnnotationPresent(HystrixCollapser.class)) {
            throw new IllegalStateException("method cannot be annotated with HystrixCommand and HystrixCollapser " +
                    "annotations at the same time");
        }
        MetaHolderFactory metaHolderFactory = META_HOLDER_FACTORY_MAP.get(HystrixPointcutType.of(method));
        MetaHolder metaHolder = metaHolderFactory.create(joinPoint);
        HystrixInvokable invokable = HystrixCommandFactory.getInstance().create(metaHolder);
        ExecutionType executionType = metaHolder.isCollapserAnnotationPresent() ?
                metaHolder.getCollapserExecutionType() : metaHolder.getExecutionType();

        Object result;
        try {
            if (!metaHolder.isObservable()) {
                result = CommandExecutor.execute(invokable, executionType, metaHolder);
            } else {
                result = executeObservable(invokable, executionType, metaHolder);
            }
        } catch (HystrixBadRequestException e) {
            throw e.getCause() != null ? e.getCause() : e;
        } catch (HystrixRuntimeException e) {
            throw hystrixRuntimeExceptionToThrowable(metaHolder, e);
        }
        return result;
    }
    //......
}

支持@HystrixCommand及@HystrixCollapser注解

HystrixShutdownHook

    /**
     * {@link DisposableBean} that makes sure that Hystrix internal state is cleared when
     * {@link ApplicationContext} shuts down.
     */
    private class HystrixShutdownHook implements DisposableBean {

        @Override
        public void destroy() throws Exception {
            // Just call Hystrix to reset thread pool etc.
            Hystrix.reset();
        }

    }

主要是调用了reset方法

hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/Hystrix.java

    /**
     * Reset state and release resources in use (such as thread-pools).
     * <p>
     * NOTE: This can result in race conditions if HystrixCommands are concurrently being executed.
     * </p>
     */
    public static void reset() {
        // shutdown thread-pools
        HystrixThreadPool.Factory.shutdown();
        _reset();
    }

    /**
     * Reset state and release resources in use (such as threadpools) and wait for completion.
     * <p>
     * NOTE: This can result in race conditions if HystrixCommands are concurrently being executed.
     * </p>
     * 
     * @param time
     *            time to wait for thread-pools to shutdown
     * @param unit
     *            {@link TimeUnit} for <pre>time</pre> to wait for thread-pools to shutdown
     */
    public static void reset(long time, TimeUnit unit) {
        // shutdown thread-pools
        HystrixThreadPool.Factory.shutdown(time, unit);
        _reset();
    }

    /**
     * Reset logic that doesn't have time/TimeUnit arguments.
     */
    private static void _reset() {
        // clear metrics
        HystrixCommandMetrics.reset();
        HystrixThreadPoolMetrics.reset();
        HystrixCollapserMetrics.reset();
        // clear collapsers
        HystrixCollapser.reset();
        // clear circuit breakers
        HystrixCircuitBreaker.Factory.reset();
        HystrixPlugins.reset();
        HystrixPropertiesFactory.reset();
        currentCommand.set(new ConcurrentStack<HystrixCommandKey>());
    }

小结

HystrixCircuitBreakerConfiguration主要是注入了HystrixCommandAspect以及HystrixShutdownHook。前者用于支持支持@HystrixCommand及@HystrixCollapser注解,后者用于shudown的时候进行一些清理工作。

doc

  • 13. Circuit Breaker: Hystrix Clients

原文发布于微信公众号 - 码匠的流水账(geek_luandun)

原文发表时间:2018-06-24

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏移动开发的那些事儿

startActivity的Hook之路

由上可知,最终是通过ActivityThread里面的mInstrumentation对象来执行execStartActivity,而ActivityThrea...

15420
来自专栏ml

poj-----(2828)Buy Tickets(线段树单点更新)

Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 1...

391120
来自专栏算法修养

ZOJ 3661 Palindromic Substring(回文树)

Palindromic Substring ---- Time Limit: 10 Seconds      Memory Limit: 65536 KB --...

29960
来自专栏扎心了老铁

Hadoop通过HCatalog编写Mapreduce任务访问hive库中schema数据

1、dirver package com.kangaroo.hadoop.drive; import java.util.Map; import java.u...

74550
来自专栏Java面试笔试题

阐述Spring框架中Bean的生命周期?

① Spring IoC容器找到关于Bean的定义并实例化该Bean。 ② Spring IoC容器对Bean进行依赖注入。 ③ 如果Bean实现了Bean...

16630
来自专栏码匠的流水账

springboot2上报metrics到statsd

micrometer-registry-statsd-1.0.1-sources.jar!/io/micrometer/statsd/StatsdFlavor....

31210
来自专栏算法修养

UESTC 491 Tricks in Bits

Tricks in Bits Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format...

34540
来自专栏小樱的经验随笔

HDU 5882 Balanced Game

Balanced Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/3276...

35890
来自专栏码匠的流水账

聊聊resilience4j的CircuitBreakerStateMachine

本文主要研究一下resilience4j的CircuitBreakerStateMachine

13620
来自专栏androidBlog

Android Hook Activity 的几种姿势

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gdutxiaoxu/article/de...

33610

扫码关注云+社区

领取腾讯云代金券