Skip to content

Commit

Permalink
support default filter config
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianjun.ljj committed May 16, 2024
1 parent 21acf28 commit f5f409f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
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,7 @@ public abstract class AbstractInterfaceConfig<T, S extends AbstractInterfaceConf
/**
* 过滤器配置别名,多个用逗号隔开
*/
protected List<String> filter;
protected List<String> filter = getListValue(DEFAULT_FILTERS);

/**
* 注册中心配置,可配置多个
Expand Down Expand Up @@ -361,6 +363,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
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.config;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
* @author Even
* @date 2024/5/16 14:07
*/
public class ConsumerConfigTest {

@Test
public void testAddFilter() {
ConsumerConfig consumerConfig = new ConsumerConfig();
List<String> filters = new ArrayList<>();
filters.add("testFilter1");
filters.add("testFilter2");
consumerConfig.addFilter(filters);
Assert.assertEquals(4, consumerConfig.getFilter().size());
consumerConfig.setFilter(filters);
Assert.assertEquals(2, consumerConfig.getFilter().size());
}
}
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
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

0 comments on commit f5f409f

Please sign in to comment.