专栏首页码匠的流水账聊聊dubbo spring boot的DubboShutdownMetadata

聊聊dubbo spring boot的DubboShutdownMetadata

本文主要研究一下dubbo spring boot的DubboShutdownMetadata

AbstractDubboMetadata

dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/actuator/src/main/java/org/apache/dubbo/spring/boot/actuate/endpoint/metadata/AbstractDubboMetadata.java

public abstract class AbstractDubboMetadata implements ApplicationContextAware, EnvironmentAware {

    protected ApplicationContext applicationContext;

    protected ConfigurableEnvironment environment;

    private static boolean isSimpleType(Class<?> type) {
        return isPrimitiveOrWrapper(type)
                || type == String.class
                || type == BigDecimal.class
                || type == BigInteger.class
                || type == Date.class
                || type == URL.class
                || type == Class.class
                ;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void setEnvironment(Environment environment) {
        if (environment instanceof ConfigurableEnvironment) {
            this.environment = (ConfigurableEnvironment) environment;
        }
    }

    protected Map<String, Object> resolveBeanMetadata(final Object bean) {

        final Map<String, Object> beanMetadata = new LinkedHashMap<>();

        try {

            BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {

                Method readMethod = propertyDescriptor.getReadMethod();

                if (readMethod != null && isSimpleType(propertyDescriptor.getPropertyType())) {

                    String name = Introspector.decapitalize(propertyDescriptor.getName());
                    Object value = readMethod.invoke(bean);

                    beanMetadata.put(name, value);
                }

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return beanMetadata;

    }

    protected Map<String, ServiceBean> getServiceBeansMap() {
        return beansOfTypeIncludingAncestors(applicationContext, ServiceBean.class);
    }

    protected ReferenceAnnotationBeanPostProcessor getReferenceAnnotationBeanPostProcessor() {
        return applicationContext.getBean(BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class);
    }

    protected Map<String, ProtocolConfig> getProtocolConfigsBeanMap() {
        return beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class);
    }


}
  • AbstractDubboMetadata声明实现了ApplicationContextAware及EnvironmentAware接口;提供了getServiceBeansMap、getReferenceAnnotationBeanPostProcessor、getProtocolConfigsBeanMap实现类使用

DubboShutdownMetadata

dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/actuator/src/main/java/org/apache/dubbo/spring/boot/actuate/endpoint/metadata/DubboShutdownMetadata.java

@Component
public class DubboShutdownMetadata extends AbstractDubboMetadata {


    public Map<String, Object> shutdown() throws Exception {

        Map<String, Object> shutdownCountData = new LinkedHashMap<>();

        // registries
        int registriesCount = getRegistries().size();

        // protocols
        int protocolsCount = getProtocolConfigsBeanMap().size();

        shutdownCountData.put("registries", registriesCount);
        shutdownCountData.put("protocols", protocolsCount);

        // Service Beans
        Map<String, ServiceBean> serviceBeansMap = getServiceBeansMap();
        if (!serviceBeansMap.isEmpty()) {
            for (ServiceBean serviceBean : serviceBeansMap.values()) {
                serviceBean.destroy();
            }
        }
        shutdownCountData.put("services", serviceBeansMap.size());

        // Reference Beans
        ReferenceAnnotationBeanPostProcessor beanPostProcessor = getReferenceAnnotationBeanPostProcessor();

        int referencesCount = beanPostProcessor.getReferenceBeans().size();

        beanPostProcessor.destroy();

        shutdownCountData.put("references", referencesCount);

        // Set Result to complete
        Map<String, Object> shutdownData = new TreeMap<>();
        shutdownData.put("shutdown.count", shutdownCountData);


        return shutdownData;
    }

}
  • DubboShutdownMetadata继承了AbstractDubboMetadata,它提供了shutdown方法,该方法会遍历getServiceBeansMap,挨个执行serviceBean.destroy()方法,最后执行ReferenceAnnotationBeanPostProcessor.destroy方法

ReferenceAnnotationBeanPostProcessor

dubbo-2.7.3-sources.jar!/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java

public class ReferenceAnnotationBeanPostProcessor extends AnnotationInjectedBeanPostProcessor implements
        ApplicationContextAware, ApplicationListener {

    /**
     * The bean name of {@link ReferenceAnnotationBeanPostProcessor}
     */
    public static final String BEAN_NAME = "referenceAnnotationBeanPostProcessor";

    /**
     * Cache size
     */
    private static final int CACHE_SIZE = Integer.getInteger(BEAN_NAME + ".cache.size", 32);

    private final ConcurrentMap<String, ReferenceBean<?>> referenceBeanCache =
            new ConcurrentHashMap<>(CACHE_SIZE);

    private final ConcurrentHashMap<String, ReferenceBeanInvocationHandler> localReferenceBeanInvocationHandlerCache =
            new ConcurrentHashMap<>(CACHE_SIZE);

    private final ConcurrentMap<InjectionMetadata.InjectedElement, ReferenceBean<?>> injectedFieldReferenceBeanCache =
            new ConcurrentHashMap<>(CACHE_SIZE);

    private final ConcurrentMap<InjectionMetadata.InjectedElement, ReferenceBean<?>> injectedMethodReferenceBeanCache =
            new ConcurrentHashMap<>(CACHE_SIZE);

    private ApplicationContext applicationContext;

    /**
     * To support the legacy annotation that is @com.alibaba.dubbo.config.annotation.Reference since 2.7.3
     */
    public ReferenceAnnotationBeanPostProcessor() {
        super(Reference.class, com.alibaba.dubbo.config.annotation.Reference.class);
    }

    //......

    @Override
    public void destroy() throws Exception {
        super.destroy();
        this.referenceBeanCache.clear();
        this.localReferenceBeanInvocationHandlerCache.clear();
        this.injectedFieldReferenceBeanCache.clear();
        this.injectedMethodReferenceBeanCache.clear();
    }

}
  • ReferenceAnnotationBeanPostProcessor继承了AnnotationInjectedBeanPostProcessor,实现了ApplicationContextAware, ApplicationListener接口;destroy方法会执行AnnotationInjectedBeanPostProcessor.destroy,然后清空referenceBeanCache、localReferenceBeanInvocationHandlerCache、injectedFieldReferenceBeanCache、injectedMethodReferenceBeanCache

小结

DubboShutdownMetadata继承了AbstractDubboMetadata,它提供了shutdown方法,该方法会遍历getServiceBeansMap,挨个执行serviceBean.destroy()方法,最后执行ReferenceAnnotationBeanPostProcessor.destroy方法

doc

  • DubboShutdownMetadata

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-08-15

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

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊dubbo spring boot的DubboShutdownMetadata

    本文主要研究一下dubbo spring boot的DubboShutdownMetadata

    codecraft
  • 聊聊dubbo的CommandExecutor

    dubbo-2.7.2/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/Co...

    codecraft
  • 聊聊nacos address的deleteCluster

    nacos-1.1.3/address/src/main/java/com/alibaba/nacos/address/controller/AddressSe...

    codecraft
  • 聊聊dubbo spring boot的DubboShutdownMetadata

    本文主要研究一下dubbo spring boot的DubboShutdownMetadata

    codecraft
  • Oracle 20c 新特性:数据泵 expdp / impdp 的EXCLUDE和CHECKSUM增强

    墨墨导读:随着 Oracle 的数据泵技术不断增强,这个逻辑数据备份和迁移工具,已经成为了 Oracle 数据库用户的首选工具之一,在 20c 中, expdp...

    数据和云
  • 深入浅出Retrofit2.x(二)

    大家在和服务交互的时候,大多情况下都应该使用的是 GET、POST 请求吧,下面我们就对 POST 各种数据类型及如何定义接口进行说明。

    IT大飞说
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解

    (1) 在SAP CRM里创建一个Lead后,会观察到有一个Opportunity自动生成,这是通过什么后台逻辑实现的呢?

    Jerry Wang
  • java 用httpclient访问https时经常返回403的原因

    纠结了一天的问题终于落下了帷幕!先听一首歌吧 今天使用了一些httpclient包进行https网页数据的访问,但是一直返回403的问题,一开始以为网站做了限制...

    企鹅号小编
  • 卓越软件工程--《微软360度》读后感

    卓越软件工程 --《微软360度》读后感 卓越软件工程 这一段时间,读了《微软360度—成功与成长》这本书,感觉微软之所以取得巨大成功,与微软重视人才,重视知...

    用户1177503
  • 移动Web 开发中的 Off Canvas 导航

    Jeff 最近发布的DeveMobile 与EaseMobile 主题在导航栏上采用的是在一些native app 中常见的Off Canvas 导航。而关于 ...

    Jeff

扫码关注云+社区

领取腾讯云代金券