Skip to content

Commit 1e85ba5

Browse files
feat:support setting rule based router failover type in service metadata. (#677)
Signed-off-by: Haotian Zhang <[email protected]>
1 parent d0c0d9d commit 1e85ba5

File tree

3 files changed

+512
-14
lines changed

3 files changed

+512
-14
lines changed

polaris-plugins/polaris-plugins-router/router-metadata/src/main/java/com/tencent/polaris/plugins/router/metadata/MetadataRouter.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.tencent.polaris.plugins.router.metadata;
1919

20+
import com.tencent.polaris.annonation.JustForTest;
2021
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
2122
import com.tencent.polaris.api.config.plugin.PluginConfigProvider;
2223
import com.tencent.polaris.api.config.verify.Verifier;
@@ -78,20 +79,6 @@ public class MetadataRouter extends AbstractServiceRouter implements PluginConfi
7879

7980
@Override
8081
public RouteResult router(RouteInfo routeInfo, ServiceInstances instances) throws PolarisException {
81-
FailOverType failOverType = config.getMetadataFailOverType();
82-
Map<String, String> svcMetadata = instances.getMetadata();
83-
if (MapUtils.isNotEmpty(svcMetadata)) {
84-
if (svcMetadata.containsKey(KEY_METADATA_FAILOVER_TYPE)) {
85-
String value = svcMetadata.get(KEY_METADATA_FAILOVER_TYPE);
86-
if (valueToFailoverType.containsKey(value)) {
87-
failOverType = valueToFailoverType.get(value);
88-
}
89-
}
90-
}
91-
MetadataFailoverType metadataFailoverType = routeInfo.getMetadataFailoverType();
92-
if (null != metadataFailoverType) {
93-
failOverType = inputToFailoverType.get(metadataFailoverType);
94-
}
9582
boolean availableInsFlag;
9683
Map<String, String> reqMetadata = getRouterMetadata(routeInfo);
9784
List<Instance> instanceList = new ArrayList<>();
@@ -113,6 +100,25 @@ public RouteResult router(RouteInfo routeInfo, ServiceInstances instances) throw
113100
if (!CollectionUtils.isEmpty(instanceList)) {
114101
return new RouteResult(instanceList, RouteResult.State.Next);
115102
}
103+
MetadataFailoverType metadataFailoverType = routeInfo.getMetadataFailoverType();
104+
FailOverType failOverType = null;
105+
if (null != metadataFailoverType) {
106+
failOverType = inputToFailoverType.get(metadataFailoverType);
107+
}
108+
if (failOverType == null) {
109+
Map<String, String> svcMetadata = instances.getMetadata();
110+
if (MapUtils.isNotEmpty(svcMetadata)) {
111+
if (svcMetadata.containsKey(KEY_METADATA_FAILOVER_TYPE)) {
112+
String value = svcMetadata.get(KEY_METADATA_FAILOVER_TYPE);
113+
if (valueToFailoverType.containsKey(value)) {
114+
failOverType = valueToFailoverType.get(value);
115+
}
116+
}
117+
}
118+
}
119+
if (failOverType == null) {
120+
failOverType = config.getMetadataFailOverType();
121+
}
116122
switch (failOverType) {
117123
case all:
118124
return new RouteResult(instances.getInstances(), RouteResult.State.Next);
@@ -164,6 +170,11 @@ public Class<? extends Verifier> getPluginConfigClazz() {
164170
return MetadataRouterConfig.class;
165171
}
166172

173+
@JustForTest
174+
void setConfig(MetadataRouterConfig config) {
175+
this.config = config;
176+
}
177+
167178
public MetadataRouterConfig getConfig() {
168179
return config;
169180
}

0 commit comments

Comments
 (0)