Expose way to add config filter (#10236)

* expose way to add config filter

* add @since

* fix since time
This commit is contained in:
chenyiqin 2023-04-06 15:21:21 +08:00 committed by GitHub
parent 06a2ea7004
commit 34cdb0890f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 2 deletions

View File

@ -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;
@ -144,6 +145,15 @@ public interface ConfigService {
*/
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.
*

View File

@ -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;
@ -248,6 +249,11 @@ public class NacosConfigService implements ConfigService {
}
}
@Override
public void addConfigFilter(IConfigFilter configFilter) {
configFilterChainManager.addFilter(configFilter);
}
@Override
public void shutDown() throws NacosException {
worker.shutdown();

View File

@ -36,10 +36,12 @@ public class ConfigFilterChainManager implements IConfigFilterChain {
private final List<IConfigFilter> filters = new ArrayList<>();
private final Properties initProperty;
public ConfigFilterChainManager(Properties properties) {
this.initProperty = properties;
ServiceLoader<IConfigFilter> 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()) {