diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java index 6dd1f6bae..3adab5aa4 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.api.config; +import com.alibaba.nacos.api.config.filter.IConfigFilter; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; @@ -143,6 +144,15 @@ public interface ConfigService { * @return whether health */ String getServerStatus(); + + /** + * add config filter. + * It is recommended to use {@link com.alibaba.nacos.api.config.filter.AbstractConfigFilter} to expand the filter. + * + * @param configFilter filter + * @since 2.3.0 + */ + void addConfigFilter(IConfigFilter configFilter); /** * Shutdown the resource service. diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index f8a351d39..a8e50c000 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigType; +import com.alibaba.nacos.api.config.filter.IConfigFilter; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; @@ -247,7 +248,12 @@ public class NacosConfigService implements ConfigService { return DOWN; } } - + + @Override + public void addConfigFilter(IConfigFilter configFilter) { + configFilterChainManager.addFilter(configFilter); + } + @Override public void shutDown() throws NacosException { worker.shutdown(); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java index 36acb3ad3..6cfd7eddb 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java @@ -35,11 +35,13 @@ import java.util.ServiceLoader; public class ConfigFilterChainManager implements IConfigFilterChain { private final List filters = new ArrayList<>(); + + private final Properties initProperty; public ConfigFilterChainManager(Properties properties) { + this.initProperty = properties; ServiceLoader configFilters = ServiceLoader.load(IConfigFilter.class); for (IConfigFilter configFilter : configFilters) { - configFilter.init(properties); addFilter(configFilter); } } @@ -51,6 +53,8 @@ public class ConfigFilterChainManager implements IConfigFilterChain { * @return this */ public synchronized ConfigFilterChainManager addFilter(IConfigFilter filter) { + // init + filter.init(this.initProperty); // ordered by order value int i = 0; while (i < this.filters.size()) {