1. 程式人生 > 程式設計 >聊聊dubbo的ConfigChangeEvent

聊聊dubbo的ConfigChangeEvent

本文主要研究一下dubbo的ConfigChangeEvent

ConfigChangeEvent

dubbo-2.7.3/dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigChangeEvent.java

public class ConfigChangeEvent {
    private final String key;

    private final String value;
    private final ConfigChangeType changeType;

    public ConfigChangeEvent(String key,String value) {
        this(key,value,ConfigChangeType.MODIFIED);
    }

    public ConfigChangeEvent(String key,String value,ConfigChangeType changeType) {
        this.key = key;
        this.value = value;
        this.changeType = changeType;
    }

    public String getKey
() { return key; } public String getValue() { return value; } public ConfigChangeType getChangeType() { return changeType; } @Override public String toString() { return "ConfigChangeEvent{" + "key='" + key + '\'' + ",value='
" + value + '\'' + ",changeType=" + changeType + '}'; } } 複製程式碼
  • ConfigChangeEvent定義了key、value、changeType三個屬性

ConfigChangeType

dubbo-2.7.3/dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigChangeType.java

public enum ConfigChangeType {
    /**
     * A config is created.
     */
    ADDED,/**
     * A config is updated.
     */
    MODIFIED,/**
     * A config is deleted.
     */
    DELETED
}
複製程式碼
  • ConfigChangeType定義了ADDED、MODIFIED、DELETED三種型別

ConfigurationListener

dubbo-2.7.3/dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigurationListener.java

public interface ConfigurationListener {

    /**
     * Listener call back method. Listener gets notified by this method once there's any change happens on the config
     * the listener listens on.
     *
     * @param event config change event
     */
    void process(ConfigChangeEvent event);
}
複製程式碼
  • ConfigurationListener定義了process方法來處理ConfigChangeEvent

AbstractConfiguratorListener

dubbo-2.7.3/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/AbstractConfiguratorListener.java

public abstract class AbstractConfiguratorListener implements ConfigurationListener {
    private static final Logger logger = LoggerFactory.getLogger(AbstractConfiguratorListener.class);

    protected List<Configurator> configurators = Collections.emptyList();


    protected final void initWith(String key) {
        DynamicConfiguration dynamicConfiguration = DynamicConfiguration.getDynamicConfiguration();
        dynamicConfiguration.addListener(key,this);
        String rawConfig = dynamicConfiguration.getRule(key,DynamicConfiguration.DEFAULT_GROUP);
        if (!StringUtils.isEmpty(rawConfig)) {
            genConfiguratorsFromRawRule(rawConfig);
        }
    }

    @Override
    public void process(ConfigChangeEvent event) {
        if (logger.isInfoEnabled()) {
            logger.info("Notification of overriding rule,change type is: " + event.getChangeType() +
                    ",raw config content is:\n " + event.getValue());
        }

        if (event.getChangeType().equals(ConfigChangeType.DELETED)) {
            configurators.clear();
        } else {
            if (!genConfiguratorsFromRawRule(event.getValue())) {
                return;
            }
        }

        notifyOverrides();
    }

    private boolean genConfiguratorsFromRawRule(String rawConfig) {
        boolean parseSuccess = true;
        try {
            // parseConfigurators will recognize app/service config automatically.
            configurators = Configurator.toConfigurators(ConfigParser.parseConfigurators(rawConfig))
                    .orElse(configurators);
        } catch (Exception e) {
            logger.error("Failed to parse raw dynamic config and it will not take effect,the raw config is: " +
                    rawConfig,e);
            parseSuccess = false;
        }
        return parseSuccess;
    }

    protected abstract void notifyOverrides();

    public List<Configurator> getConfigurators() {
        return configurators;
    }

    public void setConfigurators(List<Configurator> configurators) {
        this.configurators = configurators;
    }
}
複製程式碼
  • AbstractConfiguratorListener宣告實現了ConfigurationListener介面,其process在event的changeType是ConfigChangeType.DELETED時會清空configurators;最後執行的notifyOverrides方法留給子類實現

小結

ConfigChangeEvent定義了key、value、changeType三個屬性;ConfigChangeType定義了ADDED、MODIFIED、DELETED三種型別;ConfigurationListener定義了process方法來處理ConfigChangeEvent

doc