Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support default filter config #1415

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public class RpcOptions {
* 默认Tracer实现
*/
public static final String DEFAULT_TRACER = "default.tracer";
/**
* 默认filter实现
*/
public static final String DEFAULT_FILTERS = "default.filters";

/**
* 注册中心发现服务(保存注册中心地址的服务)的地址
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import java.util.concurrent.ConcurrentHashMap;

import static com.alipay.sofa.rpc.common.RpcConfigs.getBooleanValue;
import static com.alipay.sofa.rpc.common.RpcConfigs.getListValue;
import static com.alipay.sofa.rpc.common.RpcConfigs.getStringValue;
import static com.alipay.sofa.rpc.common.RpcOptions.DEFAULT_FILTERS;
import static com.alipay.sofa.rpc.common.RpcOptions.DEFAULT_GROUP;
import static com.alipay.sofa.rpc.common.RpcOptions.DEFAULT_PROXY;
import static com.alipay.sofa.rpc.common.RpcOptions.DEFAULT_SERIALIZATION;
Expand Down Expand Up @@ -106,7 +108,8 @@ public abstract class AbstractInterfaceConfig<T, S extends AbstractInterfaceConf
/**
* 过滤器配置别名,多个用逗号隔开
*/
protected List<String> filter;
protected List<String> filter = new ArrayList<String>(
getListValue(DEFAULT_FILTERS));

/**
* 注册中心配置,可配置多个
Expand Down Expand Up @@ -361,6 +364,18 @@ public S setFilter(List<String> filter) {
return castThis();
}

/**
* add filter
*
* @param filter the add filter
*/
public void addFilter(List<String> filter) {
if(this.filter == null) {
filter = new ArrayList<>();
}
this.filter.addAll(filter);
}

/**
* Gets registry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

/**
*
Expand All @@ -38,12 +37,10 @@ public class FilterChainTest {
public void buildProviderChain() {

ProviderConfig providerConfig = new ProviderConfig();
providerConfig.setFilter(Arrays.asList("testChainFilter0", "-testChainFilter8"));
providerConfig.setInterfaceId(Serializer.class.getName());

ConsumerConfig consumerConfig = new ConsumerConfig();
ArrayList<Filter> list = new ArrayList<Filter>();
consumerConfig.setFilter(Arrays.asList("testChainFilter0", "-testChainFilter8"));
list.add(new TestChainFilter1());
list.add(new TestChainFilter2());
list.add(new TestChainFilter3());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testDefaultValue() {
assertEquals(null, defaultConfig.getInterfaceId());
assertEquals("", defaultConfig.getUniqueId());
assertEquals(null, defaultConfig.getFilterRef());
assertEquals(null, defaultConfig.getFilter());
assertNotNull(defaultConfig.getFilter());
assertEquals(null, defaultConfig.getRegistry());
assertEquals(null, defaultConfig.getMethods());
assertEquals("hessian2", defaultConfig.getSerialization());
Expand Down Expand Up @@ -122,9 +122,18 @@ public void testSetGet() {
config.setFilterRef(filterRefList);
assertSame(filterRefList, config.getFilterRef());

List<String> defaultFilter = config.getFilter();
List<String> filterList = new ArrayList<>();
filterList.add("testFilter");
config.addFilter(filterList);
Assert.assertTrue(defaultFilter.contains("testChainFilter0"));
Assert.assertTrue(defaultFilter.contains("-testChainFilter8"));
Assert.assertTrue(defaultFilter.contains("testFilter"));
assertSame(defaultFilter, config.getFilter());

config.setFilter(filterList);
assertSame(filterList, config.getFilter());
assertNotSame(defaultFilter, config.getFilter());

List<RegistryConfig> registryConfigs = new ArrayList<>();
config.setRegistry(registryConfigs);
Expand Down
3 changes: 2 additions & 1 deletion core/api/src/test/resources/sofa-rpc/rpc-config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"rpc.config.order": 999,
"logger.impl": "com.alipay.sofa.rpc.log.SystemLogger"
"logger.impl": "com.alipay.sofa.rpc.log.SystemLogger",
"default.filters" :["testChainFilter0", "-testChainFilter8"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ PS:大家也看到了,本JSON文档是支持注释的,而标准JSON是不支
"default.transport": "netty4",
// 默认tracer实现
"default.tracer": "",
// 默认filter实现
"default.filters": [],
/*-------------默认配置值结束-------------*/


Expand Down
Loading