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 consumer routers config #1416

Merged
merged 2 commits into from
May 17, 2024
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 @@ -143,7 +143,7 @@ public static RouterChain buildConsumerChain(ConsumerBootstrap consumerBootstrap
List<ExtensionClass<Router>> extensionRouters = new ArrayList<ExtensionClass<Router>>();
List<String> 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 {
Expand All @@ -152,7 +152,7 @@ public static RouterChain buildConsumerChain(ConsumerBootstrap consumerBootstrap
extensionRouters.add(extensionRouter);
}
}
}
});
}
// 解析自动加载的router
if (!excludes.contains(StringUtils.ALL) && !excludes.contains(StringUtils.DEFAULT)) { // 配了-*和-default表示不加载内置
Expand All @@ -162,7 +162,6 @@ public static RouterChain buildConsumerChain(ConsumerBootstrap consumerBootstrap
}
}
}
excludes = null; // 不需要了
// 按order从小到大排序
if (extensionRouters.size() > 1) {
extensionRouters.sort(Comparator.comparingInt(ExtensionClass::getOrder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
* 默认回调线程池最小
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -173,7 +176,8 @@ public class ConsumerConfig<T> extends AbstractInterfaceConfig<T, ConsumerConfig
/**
* 路由配置别名
*/
protected List<String> router;
protected List<String> router = new ArrayList<String>(
getListValue(CONSUMER_ROUTERS));

/**
* 路由规则引用,多个用英文逗号隔开。List<Router>
Expand Down Expand Up @@ -682,6 +686,18 @@ public ConsumerConfig<T> setRouter(List<String> router) {
return this;
}

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

/**
* Gets routerRef.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private static List<Filter> selectActualFilters(AbstractInterfaceConfig config,
List<ExtensionClass<Filter>> extensionFilters = new LinkedList<ExtensionClass<Filter>>();
List<String> 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 {
Expand All @@ -202,7 +202,7 @@ private static List<Filter> selectActualFilters(AbstractInterfaceConfig config,
extensionFilters.add(filter);
}
}
}
});
}
// 解析自动加载的过滤器
if (!excludes.contains(StringUtils.ALL) && !excludes.contains(StringUtils.DEFAULT)) { // 配了-*和-default表示不加载内置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public void buildProviderChain() {
ConsumerConfig config = new ConsumerConfig();
config.setBootstrap("test");
ArrayList<Router> list = new ArrayList<Router>();
config.setRouter(Arrays.asList("testChainRouter0", "-testChainRouter8", "notExistChainRouter"));
list.add(new TestChainRouter1());
list.add(new TestChainRouter2());
list.add(new TestChainRouter3());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -119,6 +123,20 @@ public void testGetInterfaceId() {
assertEquals("serviceName", config.getInterfaceId());
}

@Test
public void testRouter() {
List<String> router = config.getRouter();
List<String> 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 {
}

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,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"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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相关配置结束-------------*/


Expand Down
Loading