Skip to content

Commit a2a45ed

Browse files
feat:support TSF router. (#534)
* feat:support TSF router. * feat:support TSF router.
1 parent eefcde0 commit a2a45ed

File tree

47 files changed

+2318
-446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2318
-446
lines changed

polaris-common/polaris-client/src/main/java/com/tencent/polaris/client/flow/BaseFlow.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,21 @@
3333
import com.tencent.polaris.api.plugin.route.RouteInfo;
3434
import com.tencent.polaris.api.plugin.route.RouteResult;
3535
import com.tencent.polaris.api.plugin.route.ServiceRouter;
36-
import com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider;
37-
import com.tencent.polaris.api.pojo.Instance;
38-
import com.tencent.polaris.api.pojo.ServiceEventKey;
36+
import com.tencent.polaris.api.pojo.*;
3937
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
40-
import com.tencent.polaris.api.pojo.ServiceEventKeysProvider;
41-
import com.tencent.polaris.api.pojo.ServiceInfo;
42-
import com.tencent.polaris.api.pojo.ServiceInstances;
43-
import com.tencent.polaris.api.pojo.ServiceInstancesWrap;
44-
import com.tencent.polaris.api.pojo.ServiceKey;
45-
import com.tencent.polaris.api.pojo.ServiceRule;
46-
import com.tencent.polaris.api.pojo.Services;
4738
import com.tencent.polaris.api.rpc.Criteria;
4839
import com.tencent.polaris.api.rpc.RequestBaseEntity;
4940
import com.tencent.polaris.api.utils.CollectionUtils;
5041
import com.tencent.polaris.client.util.Utils;
5142
import com.tencent.polaris.logging.LoggerFactory;
52-
import java.util.HashMap;
53-
import java.util.List;
54-
import java.util.Map;
43+
import org.slf4j.Logger;
44+
45+
import java.util.*;
5546
import java.util.concurrent.ExecutionException;
5647
import java.util.concurrent.TimeUnit;
5748
import java.util.concurrent.TimeoutException;
58-
import org.slf4j.Logger;
49+
50+
import static com.tencent.polaris.api.plugin.route.RouterConstants.ROUTER_FAULT_TOLERANCE_ENABLE;
5951

6052
/**
6153
* 同步调用流程
@@ -71,16 +63,16 @@ public class BaseFlow {
7163
/**
7264
* 通用获取单个服务实例的方法,用于SDK内部调用
7365
*
74-
* @param extensions 插件上下文
75-
* @param serviceKey 服务信息
66+
* @param extensions 插件上下文
67+
* @param serviceKey 服务信息
7668
* @param coreRouterNames 核心路由插件链
77-
* @param lbPolicy 负载均衡策略
78-
* @param protocol 协议信息
79-
* @param hashKey 一致性hash的key
69+
* @param lbPolicy 负载均衡策略
70+
* @param protocol 协议信息
71+
* @param hashKey 一致性hash的key
8072
* @return 过滤后的实例
8173
*/
8274
public static Instance commonGetOneInstance(Extensions extensions, ServiceKey serviceKey,
83-
List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
75+
List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
8476
ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
8577
LOG.debug("[ConnectionManager]start to discover service {}", svcEventKey);
8678
DefaultServiceEventKeysProvider provider = new DefaultServiceEventKeysProvider();
@@ -123,14 +115,14 @@ public static Instance commonGetOneInstance(Extensions extensions, ServiceKey se
123115
/**
124116
* 处理服务路由
125117
*
126-
* @param routeInfo 路由信息
127-
* @param dstInstances 目标实例列表
118+
* @param routeInfo 路由信息
119+
* @param dstInstances 目标实例列表
128120
* @param routerChainGroup 插件链
129121
* @return 过滤后的实例
130122
* @throws PolarisException 异常
131123
*/
132124
public static ServiceInstances processServiceRouters(RouteInfo routeInfo, ServiceInstances dstInstances,
133-
RouterChainGroup routerChainGroup) throws PolarisException {
125+
RouterChainGroup routerChainGroup) throws PolarisException {
134126
if (null == dstInstances || CollectionUtils.isEmpty(dstInstances.getInstances())) {
135127
return dstInstances;
136128
}
@@ -141,10 +133,19 @@ public static ServiceInstances processServiceRouters(RouteInfo routeInfo, Servic
141133
if (processRouterChain(routerChainGroup.getBeforeRouters(), routeInfo, serviceInstancesWrap)) {
142134
processed = true;
143135
}
136+
Map<String, String> destSvcMetadata = Optional.ofNullable(serviceInstancesWrap.getMetadata()).orElse(Collections.emptyMap());
137+
List<Instance> faultToleranceServiceInstances = new ArrayList<>();
138+
if (Boolean.parseBoolean(destSvcMetadata.get(ROUTER_FAULT_TOLERANCE_ENABLE))) {
139+
faultToleranceServiceInstances = new ArrayList<>(dstInstances.getInstances());
140+
}
144141
//再走业务路由
145142
if (processRouterChain(routerChainGroup.getCoreRouters(), routeInfo, serviceInstancesWrap)) {
146143
processed = true;
147144
}
145+
if (CollectionUtils.isEmpty(serviceInstancesWrap.getInstances())
146+
&& Boolean.parseBoolean(destSvcMetadata.get(ROUTER_FAULT_TOLERANCE_ENABLE))) {
147+
serviceInstancesWrap.setInstances(faultToleranceServiceInstances);
148+
}
148149
//最后走后置路由
149150
if (processRouterChain(routerChainGroup.getAfterRouters(), routeInfo, serviceInstancesWrap)) {
150151
processed = true;
@@ -156,7 +157,7 @@ public static ServiceInstances processServiceRouters(RouteInfo routeInfo, Servic
156157
}
157158

158159
private static boolean processRouterChain(List<ServiceRouter> routers,
159-
RouteInfo routeInfo, ServiceInstancesWrap serviceInstances) throws PolarisException {
160+
RouteInfo routeInfo, ServiceInstancesWrap serviceInstances) throws PolarisException {
160161
if (CollectionUtils.isEmpty(routers)) {
161162
return false;
162163
}
@@ -190,15 +191,15 @@ private static boolean processRouterChain(List<ServiceRouter> routers,
190191
/**
191192
* 同步拉取资源数据
192193
*
193-
* @param extensions 插件集合
194+
* @param extensions 插件集合
194195
* @param internalRequest 是否内部请求
195-
* @param paramProvider 参数提供器
196-
* @param controlParam 控制参数
196+
* @param paramProvider 参数提供器
197+
* @param controlParam 控制参数
197198
* @return 多资源应答
198199
* @throws PolarisException 获取异常
199200
*/
200201
public static ResourcesResponse syncGetResources(Extensions extensions, boolean internalRequest,
201-
ServiceEventKeysProvider paramProvider, FlowControlParam controlParam)
202+
ServiceEventKeysProvider paramProvider, FlowControlParam controlParam)
202203
throws PolarisException {
203204

204205
if (CollectionUtils.isEmpty(paramProvider.getSvcEventKeys()) && null == paramProvider.getSvcEventKey()) {
@@ -245,7 +246,7 @@ public static ResourcesResponse syncGetResources(Extensions extensions, boolean
245246
}
246247

247248
private static boolean readResourcesFromLocalCache(ServiceEventKeysProvider paramProvider,
248-
Extensions extensions, ResourcesResponse resourcesResponse) {
249+
Extensions extensions, ResourcesResponse resourcesResponse) {
249250
LocalRegistry localRegistry = extensions.getLocalRegistry();
250251
if (null != paramProvider.getSvcEventKey()) {
251252
if (loadLocalResources(paramProvider.getSvcEventKey(), resourcesResponse, localRegistry)) {
@@ -263,7 +264,7 @@ private static boolean readResourcesFromLocalCache(ServiceEventKeysProvider para
263264
}
264265

265266
private static boolean loadLocalResources(ServiceEventKey svcEventKey, ResourcesResponse resourcesResponse,
266-
LocalRegistry localRegistry) {
267+
LocalRegistry localRegistry) {
267268
ResourceFilter filter = new ResourceFilter(svcEventKey, false, true);
268269
if (svcEventKey.getEventType() == EventType.INSTANCE) {
269270
ServiceInstances instances = localRegistry.getInstances(filter);
@@ -293,7 +294,7 @@ private static boolean loadLocalResources(ServiceEventKey svcEventKey, Resources
293294
}
294295

295296
public static Instance processLoadBalance(LoadBalancer loadBalancer, Criteria criteria,
296-
ServiceInstances dstInstances) throws PolarisException {
297+
ServiceInstances dstInstances) throws PolarisException {
297298
Instance instance = loadBalancer.chooseInstance(criteria, dstInstances);
298299
if (null == instance) {
299300
throw new PolarisException(ErrorCode.INSTANCE_NOT_FOUND,
@@ -306,12 +307,12 @@ public static Instance processLoadBalance(LoadBalancer loadBalancer, Criteria cr
306307
/**
307308
* 构建流程控制参数
308309
*
309-
* @param entity 请求对象
310-
* @param config 配置对象
310+
* @param entity 请求对象
311+
* @param config 配置对象
311312
* @param controlParam 控制参数
312313
*/
313314
public static void buildFlowControlParam(RequestBaseEntity entity, Configuration config,
314-
FlowControlParam controlParam) {
315+
FlowControlParam controlParam) {
315316
long timeoutMs = entity.getTimeoutMs();
316317
if (timeoutMs == 0) {
317318
timeoutMs = config.getGlobal().getAPI().getTimeout();

polaris-common/polaris-config-default/src/main/resources/conf/default-config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ consumer:
142142
#描述: 缓存插件名
143143
type: inmemory
144144
#描述: 是否启用服务数据缓存
145-
serviceExpireEnable: true
145+
serviceExpireEnable: false
146146
#描述: 服务过期淘汰时间
147147
serviceExpireTime: 24h
148148
#描述: 服务定期同步刷新周期
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.metadata.core.constant;
19+
20+
/**
21+
* Metadata constant for Polaris.
22+
*
23+
* @author Haotian Zhang
24+
*/
25+
public class MetadataConstants {
26+
27+
/**
28+
* local service namespace.
29+
*/
30+
public static final String LOCAL_NAMESPACE = "LOCAL_NAMESPACE";
31+
32+
/**
33+
* local service name.
34+
*/
35+
public static final String LOCAL_SERVICE = "LOCAL_SERVICE";
36+
37+
/**
38+
* local host ip.
39+
*/
40+
public static final String LOCAL_IP = "LOCAL_IP";
41+
42+
/**
43+
* local host port.
44+
*/
45+
public static final String LOCAL_PORT = "LOCAL_PORT";
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.metadata.core.constant;
19+
20+
/**
21+
* Metadata constant for TSF.
22+
*
23+
* @author Haotian Zhang
24+
*/
25+
public final class TsfMetadataConstants {
26+
/**
27+
* tsf application id.
28+
*/
29+
public static String TSF_APPLICATION_ID = "TSF_APPLICATION_ID";
30+
31+
/**
32+
* tsf program version.
33+
*/
34+
public static String TSF_PROG_VERSION = "TSF_PROG_VERSION";
35+
36+
/**
37+
* tsf group id.
38+
*/
39+
public static String TSF_GROUP_ID = "TSF_GROUP_ID";
40+
41+
/**
42+
* tsf namespace id.
43+
*/
44+
public static String TSF_NAMESPACE_ID = "TSF_NAMESPACE_ID";
45+
46+
/**
47+
* tsf instance id.
48+
*/
49+
public static String TSF_INSTNACE_ID = "TSF_INSTNACE_ID";
50+
51+
/**
52+
* tsf region.
53+
*/
54+
public static String TSF_REGION = "TSF_REGION";
55+
56+
/**
57+
* tsf zone.
58+
*/
59+
public static String TSF_ZONE = "TSF_ZONE";
60+
61+
/**
62+
* tsf SDK version.
63+
*/
64+
public static String TSF_SDK_VERSION = "TSF_SDK_VERSION";
65+
66+
/**
67+
* tsf tags.
68+
*/
69+
public static String TSF_TAGS = "TSF_TAGS";
70+
71+
private TsfMetadataConstants() {
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.metadata.core.manager;
19+
20+
import com.tencent.polaris.metadata.core.MetadataContainer;
21+
import com.tencent.polaris.metadata.core.impl.MessageMetadataContainerImpl;
22+
import com.tencent.polaris.metadata.core.impl.MetadataContainerImpl;
23+
24+
import static com.tencent.polaris.metadata.core.manager.MetadataContext.DEFAULT_TRANSITIVE_PREFIX;
25+
26+
public class CalleeMetadataContainerGroup implements MetadataContainerGroup {
27+
28+
private final MetadataContainer messageMetadataContainer;
29+
30+
private final static MetadataContainer applicationMetadataContainer = new MetadataContainerImpl(DEFAULT_TRANSITIVE_PREFIX);
31+
32+
private final MetadataContainer customMetadataContainer;
33+
34+
public CalleeMetadataContainerGroup(String transitivePrefix) {
35+
assert null != transitivePrefix;
36+
this.messageMetadataContainer = new MessageMetadataContainerImpl(transitivePrefix);
37+
this.customMetadataContainer = new MetadataContainerImpl(transitivePrefix);
38+
}
39+
40+
@Override
41+
public MetadataContainer getMessageMetadataContainer() {
42+
return messageMetadataContainer;
43+
}
44+
45+
@Override
46+
public MetadataContainer getApplicationMetadataContainer() {
47+
return applicationMetadataContainer;
48+
}
49+
50+
public static MetadataContainer getStaticApplicationMetadataContainer() {
51+
return applicationMetadataContainer;
52+
}
53+
54+
@Override
55+
public MetadataContainer getCustomMetadataContainer() {
56+
return customMetadataContainer;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)