diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/client/RouterChain.java b/core/api/src/main/java/com/alipay/sofa/rpc/client/RouterChain.java index 41eeb5a69..e841d99ab 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/client/RouterChain.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/client/RouterChain.java @@ -143,7 +143,7 @@ public static RouterChain buildConsumerChain(ConsumerBootstrap consumerBootstrap List> extensionRouters = new ArrayList>(); List routerAliases = consumerConfig.getRouter(); if (CommonUtils.isNotEmpty(routerAliases)) { - for (String routerAlias : routerAliases) { + routerAliases.stream().distinct().forEach(routerAlias -> { if (startsWithExcludePrefix(routerAlias)) { // 排除用的特殊字符 excludes.add(routerAlias.substring(1)); } else { @@ -152,7 +152,7 @@ public static RouterChain buildConsumerChain(ConsumerBootstrap consumerBootstrap extensionRouters.add(extensionRouter); } } - } + }); } // 解析自动加载的router if (!excludes.contains(StringUtils.ALL) && !excludes.contains(StringUtils.DEFAULT)) { // 配了-*和-default表示不加载内置 @@ -162,7 +162,6 @@ public static RouterChain buildConsumerChain(ConsumerBootstrap consumerBootstrap } } } - excludes = null; // 不需要了 // 按order从小到大排序 if (extensionRouters.size() > 1) { extensionRouters.sort(Comparator.comparingInt(ExtensionClass::getOrder)); diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java b/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java index ed8ade316..46c43206c 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java @@ -380,6 +380,13 @@ public class RpcOptions { */ public static final String CONSUMER_REJECTED_EXECUTION_POLICY = "consumer.rejected.execution.policy"; + /** + * 需要解析的 routers + * + * @since 5.13.0 + */ + public static final String CONSUMER_ROUTERS = "consumer.routers"; + /** * 默认回调线程池最小 */ diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/config/ConsumerConfig.java b/core/api/src/main/java/com/alipay/sofa/rpc/config/ConsumerConfig.java index 1b72bc901..031a9316a 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/config/ConsumerConfig.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/config/ConsumerConfig.java @@ -33,10 +33,12 @@ import java.io.Serializable; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.List; import static com.alipay.sofa.rpc.common.RpcConfigs.getBooleanValue; import static com.alipay.sofa.rpc.common.RpcConfigs.getIntValue; +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.CONSUMER_REJECTED_EXECUTION_POLICY; import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_ADDRESS_HOLDER; @@ -56,6 +58,7 @@ import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_RECONNECT_PERIOD; import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_REPEATED_REFERENCE_LIMIT; import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_RETRIES; +import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_ROUTERS; import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_STICKY; import static com.alipay.sofa.rpc.common.RpcOptions.DEFAULT_PROTOCOL; @@ -173,7 +176,8 @@ public class ConsumerConfig extends AbstractInterfaceConfig router; + protected List router = new ArrayList( + getListValue(CONSUMER_ROUTERS)); /** * 路由规则引用,多个用英文逗号隔开。List @@ -682,6 +686,18 @@ public ConsumerConfig setRouter(List router) { return this; } + /** + * Add router. + * + * @param router the add router + */ + public void addRouter(List router) { + if (this.router == null) { + this.router = new ArrayList<>(); + } + this.router.addAll(router); + } + /** * Gets routerRef. * diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/filter/FilterChain.java b/core/api/src/main/java/com/alipay/sofa/rpc/filter/FilterChain.java index 9b0e97c20..e8488751c 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/filter/FilterChain.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/filter/FilterChain.java @@ -193,7 +193,7 @@ private static List selectActualFilters(AbstractInterfaceConfig config, List> extensionFilters = new LinkedList>(); List filterAliases = config.getFilter(); // if (CommonUtils.isNotEmpty(filterAliases)) { - for (String filterAlias : filterAliases) { + filterAliases.stream().distinct().forEach(filterAlias -> { if (startsWithExcludePrefix(filterAlias)) { // 排除用的特殊字符 excludes.add(filterAlias.substring(1)); } else { @@ -202,7 +202,7 @@ private static List selectActualFilters(AbstractInterfaceConfig config, extensionFilters.add(filter); } } - } + }); } // 解析自动加载的过滤器 if (!excludes.contains(StringUtils.ALL) && !excludes.contains(StringUtils.DEFAULT)) { // 配了-*和-default表示不加载内置 diff --git a/core/api/src/test/java/com/alipay/sofa/rpc/client/RouterChainTest.java b/core/api/src/test/java/com/alipay/sofa/rpc/client/RouterChainTest.java index 17e436777..b762e4d23 100644 --- a/core/api/src/test/java/com/alipay/sofa/rpc/client/RouterChainTest.java +++ b/core/api/src/test/java/com/alipay/sofa/rpc/client/RouterChainTest.java @@ -48,7 +48,6 @@ public void buildProviderChain() { ConsumerConfig config = new ConsumerConfig(); config.setBootstrap("test"); ArrayList list = new ArrayList(); - config.setRouter(Arrays.asList("testChainRouter0", "-testChainRouter8", "notExistChainRouter")); list.add(new TestChainRouter1()); list.add(new TestChainRouter2()); list.add(new TestChainRouter3()); diff --git a/core/api/src/test/java/com/alipay/sofa/rpc/std/config/ConsumerConfigTest.java b/core/api/src/test/java/com/alipay/sofa/rpc/std/config/ConsumerConfigTest.java index fabb40a41..43d5daa0d 100644 --- a/core/api/src/test/java/com/alipay/sofa/rpc/std/config/ConsumerConfigTest.java +++ b/core/api/src/test/java/com/alipay/sofa/rpc/std/config/ConsumerConfigTest.java @@ -24,9 +24,13 @@ import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException; import com.alipay.sofa.rpc.std.sample.SampleService; import com.alipay.sofa.rpc.std.sample.SampleServiceImpl; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; + /** * @author zhaowang * @version : ConsumerConfigTest.java, v 0.1 2022年01月28日 2:33 下午 zhaowang @@ -59,7 +63,7 @@ public void testDefaultValue() { assertEquals(30000, config.getHeartbeatPeriod()); assertEquals(10000, config.getReconnectPeriod()); assertEquals("DISCARD", config.getRejectedExecutionPolicy()); - assertEquals(null, config.getRouter()); + assertNotNull(config.getRouter()); assertEquals(null, config.getRouterRef()); assertEquals(null, config.getOnReturn()); assertEquals(null, config.getOnConnect()); @@ -119,6 +123,20 @@ public void testGetInterfaceId() { assertEquals("serviceName", config.getInterfaceId()); } + @Test + public void testRouter() { + List router = config.getRouter(); + List addRouter = new ArrayList<>(); + addRouter.add("testRouter"); + config.addRouter(addRouter); + assertSame(router, config.getRouter()); + Assert.assertTrue(router.contains("testRouter")); + + config.setRouter(addRouter); + assertNotSame(router, config.getRouter()); + assertSame(addRouter, config.getRouter()); + } + public interface InnerInterface { } diff --git a/core/api/src/test/resources/sofa-rpc/rpc-config.json b/core/api/src/test/resources/sofa-rpc/rpc-config.json index 6eee71f61..fa1fc161b 100644 --- a/core/api/src/test/resources/sofa-rpc/rpc-config.json +++ b/core/api/src/test/resources/sofa-rpc/rpc-config.json @@ -1,5 +1,6 @@ { "rpc.config.order": 999, "logger.impl": "com.alipay.sofa.rpc.log.SystemLogger", - "default.filters" :["testChainFilter0", "-testChainFilter8"] + "default.filters" : ["testChainFilter0", "-testChainFilter8", "testChainFilter0"], + "consumer.routers" : ["testChainRouter0", "-testChainRouter8", "notExistChainRouter", "testChainRouter0"] } \ No newline at end of file diff --git a/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json b/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json index 50895ffae..796594c8f 100644 --- a/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json +++ b/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json @@ -213,6 +213,7 @@ PS:大家也看到了,本JSON文档是支持注释的,而标准JSON是不支 "consumer.connect.create.when.absent": true, // 默认回调线程池满时的拒绝策略,可用值:DISCARD, CALLER_RUNS, CALLER_HANDLE_EXCEPTION "consumer.rejected.execution.policy": "DISCARD", + "consumer.routers": [], /*-------------Consumer相关配置结束-------------*/