1. 程式人生 > 程式設計 >聊聊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 set
ApplicationContext(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