1. 修复无单元化 header 无法匹配单元化规则的问题

2. 修复相同 ns,单元化无法转发的问题
3. unit context 适配 sct
4. 单元化普通服务 服务发现 ns type 为 DEF
pull/1681/head
shedfreewu 1 month ago
parent cd6747be8f
commit deed3a51cd

@ -24,6 +24,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.util.EnvironmentUtils;
import com.tencent.cloud.plugin.tsf.tls.utils.SyncUtils; import com.tencent.cloud.plugin.tsf.tls.utils.SyncUtils;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
@ -222,7 +223,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group"); tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group");
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfNamespaceName + ".global_config_group"); tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfNamespaceName + ".global_config_group");
if (ClassUtils.isClassPresent("org.springframework.cloud.gateway.filter.GlobalFilter")) { if (EnvironmentUtils.isGateway()) {
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".gateway_config_group"); tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".gateway_config_group");
} }
for (String tsfConfigGroup : tsfConfigGroups) { for (String tsfConfigGroup : tsfConfigGroups) {

@ -24,6 +24,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.util.EnvironmentUtils;
import com.tencent.cloud.plugin.tsf.tls.utils.SyncUtils; import com.tencent.cloud.plugin.tsf.tls.utils.SyncUtils;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader; import com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader;
@ -155,7 +156,7 @@ public final class PolarisConfigFilePuller {
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group"); tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group");
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfNamespaceName + ".global_config_group"); tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfNamespaceName + ".global_config_group");
if (ClassUtils.isClassPresent("org.springframework.cloud.gateway.filter.GlobalFilter")) { if (EnvironmentUtils.isGateway()) {
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".gateway_config_group"); tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".gateway_config_group");
} }
for (String tsfConfigGroup : tsfConfigGroups) { for (String tsfConfigGroup : tsfConfigGroups) {

@ -43,7 +43,7 @@ public class UnitPolarisDiscoveryClient extends PolarisDiscoveryClient {
if (TencentUnitManager.isEnable()) { if (TencentUnitManager.isEnable()) {
String[] parts = service.split("/"); String[] parts = service.split("/");
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[getInstance] service:{}, unit context:{}", service, TencentUnitContext.getCompositeContextMap()); LOGGER.debug("[getInstance] service:{}, unit context:{}", service, TencentUnitContext.getOriginCompositeContextMap());
} }
if (parts.length != 2) { if (parts.length != 2) {

@ -21,11 +21,15 @@ import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
import com.tencent.tsf.unit.core.TencentUnitContext; import com.tencent.tsf.unit.core.TencentUnitContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
public class UnitClientFinallyEnhancedPlugin implements EnhancedPlugin { public class UnitClientFinallyEnhancedPlugin implements EnhancedPlugin {
private static final Logger LOG = LoggerFactory.getLogger(UnitClientFinallyEnhancedPlugin.class);
public UnitClientFinallyEnhancedPlugin() { public UnitClientFinallyEnhancedPlugin() {
} }
@ -36,6 +40,9 @@ public class UnitClientFinallyEnhancedPlugin implements EnhancedPlugin {
@Override @Override
public void run(EnhancedPluginContext context) throws Throwable { public void run(EnhancedPluginContext context) throws Throwable {
if (LOG.isDebugEnabled()) {
LOG.debug("clean unit context, unit context:{}", TencentUnitContext.getOriginCompositeContextMap());
}
TencentUnitContext.removeAll(); TencentUnitContext.removeAll();
} }

@ -53,7 +53,7 @@ public class UnitFeignEnhancedPlugin implements EnhancedPlugin {
return; return;
} }
TencentUnitContext.UnitCompositeContextMap unitCompositeContextMap = TencentUnitContext.getCompositeContextMap(); TencentUnitContext.UnitCompositeContextMap unitCompositeContextMap = TencentUnitContext.getOriginCompositeContextMap();
if (!com.tencent.polaris.api.utils.CollectionUtils.isEmpty(unitCompositeContextMap.getSystemContext())) { if (!com.tencent.polaris.api.utils.CollectionUtils.isEmpty(unitCompositeContextMap.getSystemContext())) {
buildMetadataHeader(request, unitCompositeContextMap.getSystemContext(), MetadataConstant.HeaderName.TSF_UNIT); buildMetadataHeader(request, unitCompositeContextMap.getSystemContext(), MetadataConstant.HeaderName.TSF_UNIT);
} }

@ -49,7 +49,7 @@ public class UnitRestTemplateEnhancedPlugin implements EnhancedPlugin {
return; return;
} }
TencentUnitContext.UnitCompositeContextMap unitCompositeContextMap = TencentUnitContext.getCompositeContextMap(); TencentUnitContext.UnitCompositeContextMap unitCompositeContextMap = TencentUnitContext.getOriginCompositeContextMap();
if (!CollectionUtils.isEmpty(unitCompositeContextMap.getSystemContext())) { if (!CollectionUtils.isEmpty(unitCompositeContextMap.getSystemContext())) {
buildMetadataHeader(httpRequest, unitCompositeContextMap.getSystemContext(), MetadataConstant.HeaderName.TSF_UNIT); buildMetadataHeader(httpRequest, unitCompositeContextMap.getSystemContext(), MetadataConstant.HeaderName.TSF_UNIT);
} }

@ -53,7 +53,7 @@ public class UnitScgEnhancedPlugin implements EnhancedPlugin {
// get request builder // get request builder
ServerHttpRequest.Builder builder = exchange.getRequest().mutate(); ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
TencentUnitContext.UnitCompositeContextMap unitCompositeContextMap = TencentUnitContext.getCompositeContextMap(); TencentUnitContext.UnitCompositeContextMap unitCompositeContextMap = TencentUnitContext.getOriginCompositeContextMap();
if (!com.tencent.polaris.api.utils.CollectionUtils.isEmpty(unitCompositeContextMap.getSystemContext())) { if (!com.tencent.polaris.api.utils.CollectionUtils.isEmpty(unitCompositeContextMap.getSystemContext())) {
buildMetadataHeader(builder, unitCompositeContextMap.getSystemContext(), MetadataConstant.HeaderName.TSF_UNIT); buildMetadataHeader(builder, unitCompositeContextMap.getSystemContext(), MetadataConstant.HeaderName.TSF_UNIT);
} }

@ -54,6 +54,8 @@ public class UnitServletPreEnhancedPlugin implements EnhancedPlugin {
return; return;
} }
TencentUnitContext.removeAll();
String unitContextEncoded = httpServletRequest.getHeader(MetadataConstant.HeaderName.TSF_UNIT); String unitContextEncoded = httpServletRequest.getHeader(MetadataConstant.HeaderName.TSF_UNIT);
if (StringUtils.isNotEmpty(unitContextEncoded)) { if (StringUtils.isNotEmpty(unitContextEncoded)) {
Map<String, String> unitMap = JacksonUtils.deserialize2Map(URLDecoder.decode(unitContextEncoded, StandardCharsets.UTF_8)); Map<String, String> unitMap = JacksonUtils.deserialize2Map(URLDecoder.decode(unitContextEncoded, StandardCharsets.UTF_8));
@ -73,20 +75,21 @@ public class UnitServletPreEnhancedPlugin implements EnhancedPlugin {
Optional.ofNullable(unitMap.get(TencentUnitContext.CLOUD_SPACE_GRAY_UNIT_INFO)). Optional.ofNullable(unitMap.get(TencentUnitContext.CLOUD_SPACE_GRAY_UNIT_INFO)).
ifPresent(value -> TencentUnitContext.putSystemTag(TencentUnitContext.CLOUD_SPACE_GRAY_UNIT_INFO, value)); ifPresent(value -> TencentUnitContext.putSystemTag(TencentUnitContext.CLOUD_SPACE_GRAY_UNIT_INFO, value));
} }
if (logger.isDebugEnabled()) { }
logger.debug("[getSerializeTagsFromRequestMeta] unit context:{}",
TencentUnitContext.getCompositeContextMap());
}
for (String grayKey : TencentUnitManager.getGrayUnitHeaderKey()) { for (String grayKey : TencentUnitManager.getGrayUnitHeaderKey()) {
String value = httpServletRequest.getHeader(grayKey); String value = httpServletRequest.getHeader(grayKey);
// 非空 value 才设置,便于匹配灰度规则时能否直接跳过 // 非空 value 才设置,便于匹配灰度规则时能否直接跳过
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
TencentUnitContext.putGrayUserTag(UnitTagPosition.HEADER.name(), grayKey, value); TencentUnitContext.putGrayUserTag(UnitTagPosition.HEADER.name(), grayKey, value);
}
} }
} }
if (logger.isDebugEnabled()) {
logger.debug("[getSerializeTagsFromRequestMeta] unit context:{}",
TencentUnitContext.getOriginCompositeContextMap());
}
} }
@Override @Override

@ -56,7 +56,10 @@ public final class SpringCloudUnitUtils {
} }
TencentUnitContext.putSystemTagsFromUser(); TencentUnitContext.putSystemTagsFromUser();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordUnitContext] service name:{}, unit context:{}",
serviceName, TencentUnitContext.getOriginCompositeContextMap());
}
String system = TencentUnitContext.getSystemTag(TencentUnitContext.CLOUD_SPACE_TARGET_SYSTEM); String system = TencentUnitContext.getSystemTag(TencentUnitContext.CLOUD_SPACE_TARGET_SYSTEM);
// 无论 gdu 还是 sdu, 都需要 system 信息,如果没有 system返回 false则直接返回 // 无论 gdu 还是 sdu, 都需要 system 信息,如果没有 system返回 false则直接返回
if (!checkSystem(system, serviceName)) { if (!checkSystem(system, serviceName)) {
@ -95,7 +98,7 @@ public final class SpringCloudUnitUtils {
TencentUnitContext.putSystemTag(TencentUnitContext.CLOUD_SPACE_GDU_FORWARD_ONLY, Boolean.FALSE.toString()); TencentUnitContext.putSystemTag(TencentUnitContext.CLOUD_SPACE_GDU_FORWARD_ONLY, Boolean.FALSE.toString());
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordUnitContext] gduNsNotExist. only forward to sdu, system:{}, service name:{}, unit context:{}", LOGGER.debug("[preRequestRecordUnitContext] gduNsNotExist. only forward to sdu, system:{}, service name:{}, unit context:{}",
system, serviceName, TencentUnitContext.getCompositeContextMap()); system, serviceName, TencentUnitContext.getOriginCompositeContextMap());
} }
} }
else { else {
@ -108,7 +111,7 @@ public final class SpringCloudUnitUtils {
setUnitContext(gduUnitId, gduNs, serviceName); setUnitContext(gduUnitId, gduNs, serviceName);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordUnitContext] cid or business system is empty. only forward to gdu, service name:{}, unit context:{}", LOGGER.debug("[preRequestRecordUnitContext] cid or business system is empty. only forward to gdu, service name:{}, unit context:{}",
serviceName, TencentUnitContext.getCompositeContextMap()); serviceName, TencentUnitContext.getOriginCompositeContextMap());
} }
return; return;
} }
@ -125,7 +128,7 @@ public final class SpringCloudUnitUtils {
setUnitContext(gduUnitId, gduNs, serviceName); setUnitContext(gduUnitId, gduNs, serviceName);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordUnitContext] gdu first and exist instance. forward to gdu, service name:{}, unit context:{}", LOGGER.debug("[preRequestRecordUnitContext] gdu first and exist instance. forward to gdu, service name:{}, unit context:{}",
serviceName, TencentUnitContext.getCompositeContextMap()); serviceName, TencentUnitContext.getOriginCompositeContextMap());
} }
} }
} }
@ -153,7 +156,7 @@ public final class SpringCloudUnitUtils {
} }
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordUnitContext] do calculate customer num. service name:{}, unit context:{}", LOGGER.debug("[preRequestRecordUnitContext] do calculate customer num. service name:{}, unit context:{}",
serviceName, TencentUnitContext.getCompositeContextMap()); serviceName, TencentUnitContext.getOriginCompositeContextMap());
} }
} }
} }
@ -186,7 +189,7 @@ public final class SpringCloudUnitUtils {
setCrossGduContext(gduUnitId, serviceName); setCrossGduContext(gduUnitId, serviceName);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordUnitContext] gdu in other cloud, gdu unit id:{}, service name:{}, unit context:{}", LOGGER.debug("[preRequestRecordUnitContext] gdu in other cloud, gdu unit id:{}, service name:{}, unit context:{}",
gduUnitId, serviceName, TencentUnitContext.getCompositeContextMap()); gduUnitId, serviceName, TencentUnitContext.getOriginCompositeContextMap());
} }
return true; return true;
} }
@ -207,7 +210,7 @@ public final class SpringCloudUnitUtils {
if (checkUnitIdAndSetContext(destUnitId, system, serviceName)) { if (checkUnitIdAndSetContext(destUnitId, system, serviceName)) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordUnitContext] unit id exist:{}. service name:{}, unit context:{}", LOGGER.debug("[preRequestRecordUnitContext] unit id exist:{}. service name:{}, unit context:{}",
destUnitId, serviceName, TencentUnitContext.getCompositeContextMap()); destUnitId, serviceName, TencentUnitContext.getOriginCompositeContextMap());
} }
return true; return true;
} }
@ -231,7 +234,7 @@ public final class SpringCloudUnitUtils {
if (StringUtils.isEmpty(sourceGrayUnitInfo) && CollectionUtils.isEmpty(TencentUnitManager.getGrayUnitRoutes())) { if (StringUtils.isEmpty(sourceGrayUnitInfo) && CollectionUtils.isEmpty(TencentUnitManager.getGrayUnitRoutes())) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordGrayUnitContext] empty header gray info and empty gray unit routes," LOGGER.debug("[preRequestRecordGrayUnitContext] empty header gray info and empty gray unit routes,"
+ "unitContextMap:{}", TencentUnitContext.getCompositeContextMap()); + "unitContextMap:{}", TencentUnitContext.getOriginCompositeContextMap());
} }
return null; return null;
} }
@ -242,7 +245,7 @@ public final class SpringCloudUnitUtils {
TencentUnitContext.setGrayUnitContext(grayMatchRouteUnitList); TencentUnitContext.setGrayUnitContext(grayMatchRouteUnitList);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordGrayUnitContext] using header gray, unitContextMap:{}", LOGGER.debug("[preRequestRecordGrayUnitContext] using header gray, unitContextMap:{}",
TencentUnitContext.getCompositeContextMap()); TencentUnitContext.getOriginCompositeContextMap());
} }
return null; return null;
} }
@ -251,7 +254,7 @@ public final class SpringCloudUnitUtils {
if (TencentUnitContext.grayContextIsEmpty()) { if (TencentUnitContext.grayContextIsEmpty()) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordGrayUnitContext] empty gray header, unitContextMap:{}", LOGGER.debug("[preRequestRecordGrayUnitContext] empty gray header, unitContextMap:{}",
TencentUnitContext.getCompositeContextMap()); TencentUnitContext.getOriginCompositeContextMap());
} }
return null; return null;
} }
@ -296,24 +299,19 @@ public final class SpringCloudUnitUtils {
} }
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[preRequestRecordGrayUnitContext] grayMatchRoute:{}, unitContextMap:{}", LOGGER.debug("[preRequestRecordGrayUnitContext] grayMatchRoute:{}, unitContextMap:{}",
grayMatchRoute, TencentUnitContext.getCompositeContextMap()); grayMatchRoute, TencentUnitContext.getOriginCompositeContextMap());
} }
return routingUnit; return routingUnit;
} }
/** /**
* gdu ns null ns ns serviceName ns nsId/serviceName. * gdu ns nullSCT nsId/serviceName.
*/ */
public static String getGduServiceId(String serviceName, String gduNsId) { public static String getGduServiceId(String serviceName, String gduNsId) {
if (StringUtils.isEmpty(gduNsId)) { if (StringUtils.isEmpty(gduNsId)) {
return null; return null;
} }
if (TencentUnitManager.checkLocalNamespace(gduNsId)) { return gduNsId + "/" + serviceName;
return serviceName;
}
else {
return gduNsId + "/" + serviceName;
}
} }
public static String getGduUnitId() { public static String getGduUnitId() {
@ -345,7 +343,7 @@ public final class SpringCloudUnitUtils {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[getProxyInfoFromUnitHeader] parse unit from header. path:{}, unit context:{}", LOGGER.debug("[getProxyInfoFromUnitHeader] parse unit from header. path:{}, unit context:{}",
path, TencentUnitContext.getCompositeContextMap()); path, TencentUnitContext.getOriginCompositeContextMap());
} }
} }
@ -433,7 +431,7 @@ public final class SpringCloudUnitUtils {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("[getProxyInfoFromCid] parse unit from cid. path:{}, unit context:{}", LOGGER.debug("[getProxyInfoFromCid] parse unit from cid. path:{}, unit context:{}",
path, TencentUnitContext.getCompositeContextMap()); path, TencentUnitContext.getOriginCompositeContextMap());
} }
} }

@ -19,8 +19,12 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.metadata.core.MetadataObjectValue;
import com.tencent.polaris.metadata.core.MetadataType;
import com.tencent.tsf.unit.core.model.UnitRouteInfo.GrayMatchRouteUnit; import com.tencent.tsf.unit.core.model.UnitRouteInfo.GrayMatchRouteUnit;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -108,19 +112,116 @@ public final class TencentUnitContext {
* GRAY_PREFIX. * GRAY_PREFIX.
*/ */
public static final String GRAY_PREFIX = "gray."; public static final String GRAY_PREFIX = "gray.";
/**
* USER_CONTEXTS_KEY.
*/
private static final String USER_CONTEXTS_KEY = "USER_CONTEXTS";
/**
* SYSTEM_CONTEXTS_KEY.
*/
private static final String SYSTEM_CONTEXTS_KEY = "SYSTEM_CONTEXTS";
/**
* ROUTE_CONTEXTS_KEY.
*/
private static final String ROUTE_CONTEXTS_KEY = "ROUTE_CONTEXTS";
/**
* SOURCE_CONTEXTS_KEY.
*/
private static final String SOURCE_CONTEXTS_KEY = "SOURCE_CONTEXTS";
/**
* GRAY_USER_CONTEXTS_KEY.
*/
private static final String GRAY_USER_CONTEXTS_KEY = "GRAY_USER_CONTEXTS";
/**
* GRAY_SYSTEM_CONTEXTS_KEY.
*/
private static final String GRAY_SYSTEM_CONTEXTS_KEY = "GRAY_SYSTEM_CONTEXTS";
private static final Logger LOGGER = LoggerFactory.getLogger(TencentUnitContext.class); private static final Logger LOGGER = LoggerFactory.getLogger(TencentUnitContext.class);
// 用户标签,客户可以直接设置的
private final static ThreadLocal<HashMap<String, String>> USER_CONTEXTS = ThreadLocal.withInitial(HashMap::new); private static Map<String, String> getUserContext() {
// 系统标签,用于中间的计算,部分需要传递 MetadataObjectValue<Map<String, String>> metadataObjectValue = MetadataContextHolder.get().
private final static ThreadLocal<HashMap<String, String>> SYSTEM_CONTEXTS = ThreadLocal.withInitial(HashMap::new); getMetadataContainer(MetadataType.APPLICATION, true).
// 路由标签, key 为 CloudSpaceRoute 前缀的,提供给 TsfConsulReactiveCommonDiscoveryClient 做服务发现使用,无需传递 getMetadataValue(USER_CONTEXTS_KEY);
private final static ThreadLocal<HashMap<String, Object>> ROUTE_CONTEXTS = ThreadLocal.withInitial(HashMap::new); if (MetadataContextUtils.existMetadataValue(metadataObjectValue)) {
// 上游标签,不需要放到 header 传递 return metadataObjectValue.getObjectValue().get();
private final static ThreadLocal<HashMap<String, String>> SOURCE_CONTEXTS = ThreadLocal.withInitial(HashMap::new); }
// 用户灰度标签,用于匹配灰度规则,不需要放到 header 传递 else {
private final static ThreadLocal<HashMap<String, String>> GRAY_USER_CONTEXTS = ThreadLocal.withInitial(HashMap::new); Map<String, String> map = new HashMap<>();
// 灰度内部标签,不需要放到 header 传递 MetadataContextUtils.putMetadataObjectValue(USER_CONTEXTS_KEY, map);
private final static ThreadLocal<HashMap<String, String>> GRAY_SYSTEM_CONTEXTS = ThreadLocal.withInitial(HashMap::new); return map;
}
}
private static Map<String, String> getSystemContext() {
MetadataObjectValue<Map<String, String>> metadataObjectValue = MetadataContextHolder.get().
getMetadataContainer(MetadataType.APPLICATION, true).
getMetadataValue(SYSTEM_CONTEXTS_KEY);
if (MetadataContextUtils.existMetadataValue(metadataObjectValue)) {
return metadataObjectValue.getObjectValue().get();
}
else {
Map<String, String> map = new HashMap<>();
MetadataContextUtils.putMetadataObjectValue(SYSTEM_CONTEXTS_KEY, map);
return map;
}
}
private static Map<String, Object> getRouteContext() {
MetadataObjectValue<Map<String, Object>> metadataObjectValue = MetadataContextHolder.get().
getMetadataContainer(MetadataType.APPLICATION, true).
getMetadataValue(ROUTE_CONTEXTS_KEY);
if (MetadataContextUtils.existMetadataValue(metadataObjectValue)) {
return metadataObjectValue.getObjectValue().get();
}
else {
Map<String, Object> map = new HashMap<>();
MetadataContextUtils.putMetadataObjectValue(ROUTE_CONTEXTS_KEY, map);
return map;
}
}
private static Map<String, String> getSourceContext() {
MetadataObjectValue<Map<String, String>> metadataObjectValue = MetadataContextHolder.get().
getMetadataContainer(MetadataType.APPLICATION, true).
getMetadataValue(SOURCE_CONTEXTS_KEY);
if (MetadataContextUtils.existMetadataValue(metadataObjectValue)) {
return metadataObjectValue.getObjectValue().get();
}
else {
Map<String, String> map = new HashMap<>();
MetadataContextUtils.putMetadataObjectValue(SOURCE_CONTEXTS_KEY, map);
return map;
}
}
private static Map<String, String> getGrayUserContext() {
MetadataObjectValue<Map<String, String>> metadataObjectValue = MetadataContextHolder.get().
getMetadataContainer(MetadataType.APPLICATION, true).
getMetadataValue(GRAY_USER_CONTEXTS_KEY);
if (MetadataContextUtils.existMetadataValue(metadataObjectValue)) {
return metadataObjectValue.getObjectValue().get();
}
else {
Map<String, String> map = new HashMap<>();
MetadataContextUtils.putMetadataObjectValue(GRAY_USER_CONTEXTS_KEY, map);
return map;
}
}
private static Map<String, String> getGraySystemContext() {
MetadataObjectValue<Map<String, String>> metadataObjectValue = MetadataContextHolder.get().
getMetadataContainer(MetadataType.APPLICATION, true).
getMetadataValue(GRAY_SYSTEM_CONTEXTS_KEY);
if (MetadataContextUtils.existMetadataValue(metadataObjectValue)) {
return metadataObjectValue.getObjectValue().get();
}
else {
Map<String, String> map = new HashMap<>();
MetadataContextUtils.putMetadataObjectValue(GRAY_SYSTEM_CONTEXTS_KEY, map);
return map;
}
}
private TencentUnitContext() { private TencentUnitContext() {
} }
@ -131,19 +232,19 @@ public final class TencentUnitContext {
} }
public static void putUserTag(String key, String value) { public static void putUserTag(String key, String value) {
USER_CONTEXTS.get().put(key, value); getUserContext().put(key, value);
} }
public static void putSystemTag(String key, String value) { public static void putSystemTag(String key, String value) {
SYSTEM_CONTEXTS.get().put(key, value); getSystemContext().put(key, value);
} }
public static void putRouteTag(String key, Object value) { public static void putRouteTag(String key, Object value) {
ROUTE_CONTEXTS.get().put(key, value); getRouteContext().put(key, value);
} }
public static void putSourceTag(String key, String value) { public static void putSourceTag(String key, String value) {
SOURCE_CONTEXTS.get().put(SOURCE_PREFIX + key, value); getSourceContext().put(SOURCE_PREFIX + key, value);
} }
public static void putGrayUserTags(String position, Map<String, String> labels) { public static void putGrayUserTags(String position, Map<String, String> labels) {
@ -154,20 +255,20 @@ public final class TencentUnitContext {
// 需要携带 position做灰度路由匹配用的 // 需要携带 position做灰度路由匹配用的
public static void putGrayUserTag(String position, String key, String value) { public static void putGrayUserTag(String position, String key, String value) {
GRAY_USER_CONTEXTS.get().put(getGrayPositionPrefix(position) + key, value); getGrayUserContext().put(getGrayPositionPrefix(position) + key, value);
} }
// 传递灰度单元信息用的 // 传递灰度单元信息用的
public static void putGraySystemTag(String key, String value) { public static void putGraySystemTag(String key, String value) {
GRAY_SYSTEM_CONTEXTS.get().put(getGrayPrefix() + key, value); getGraySystemContext().put(getGrayPrefix() + key, value);
} }
public static String getGraySystemTag(String key) { public static String getGraySystemTag(String key) {
return GRAY_SYSTEM_CONTEXTS.get().get(getGrayPrefix() + key); return getGraySystemContext().get(getGrayPrefix() + key);
} }
public static boolean grayContextIsEmpty() { public static boolean grayContextIsEmpty() {
return GRAY_USER_CONTEXTS.get().isEmpty(); return getGrayUserContext().isEmpty();
} }
public static String getGrayPositionPrefix(String position) { public static String getGrayPositionPrefix(String position) {
@ -180,15 +281,15 @@ public final class TencentUnitContext {
public static void putSystemTagsFromUser() { public static void putSystemTagsFromUser() {
for (String key : CLOUD_SPACE_SYSTEM_FROM_USER_KEYS) { for (String key : CLOUD_SPACE_SYSTEM_FROM_USER_KEYS) {
if (USER_CONTEXTS.get().containsKey(key)) { if (getUserContext().containsKey(key)) {
SYSTEM_CONTEXTS.get().put(key, USER_CONTEXTS.get().get(key)); getSystemContext().put(key, getUserContext().get(key));
} }
} }
} }
public static void putSystemTags(Map<String, String> tags) { public static void putSystemTags(Map<String, String> tags) {
for (Map.Entry<String, String> tag : tags.entrySet()) { for (Map.Entry<String, String> tag : tags.entrySet()) {
SYSTEM_CONTEXTS.get().put(tag.getKey(), tag.getValue()); getSystemContext().put(tag.getKey(), tag.getValue());
} }
} }
@ -204,69 +305,77 @@ public final class TencentUnitContext {
} }
public static String getUserTag(String key) { public static String getUserTag(String key) {
return USER_CONTEXTS.get().get(key); return getUserContext().get(key);
} }
public static String getSystemTag(String key) { public static String getSystemTag(String key) {
return SYSTEM_CONTEXTS.get().get(key); return getSystemContext().get(key);
} }
public static Object getObjectRouteTag(String key) { public static Object getObjectRouteTag(String key) {
return ROUTE_CONTEXTS.get().get(key); return getRouteContext().get(key);
} }
public static String getStringRouteTag(String key) { public static String getStringRouteTag(String key) {
return (String) ROUTE_CONTEXTS.get().get(key); return (String) getRouteContext().get(key);
} }
public static boolean containRouteTag(String key) { public static boolean containRouteTag(String key) {
return ROUTE_CONTEXTS.get().containsKey(key); return getRouteContext().containsKey(key);
} }
public static void removeAll() { public static void removeAll() {
USER_CONTEXTS.get().clear(); getUserContext().clear();
SYSTEM_CONTEXTS.get().clear(); getSystemContext().clear();
ROUTE_CONTEXTS.get().clear(); getRouteContext().clear();
SOURCE_CONTEXTS.get().clear(); getSourceContext().clear();
GRAY_USER_CONTEXTS.get().clear(); getGrayUserContext().clear();
GRAY_SYSTEM_CONTEXTS.get().clear(); getGraySystemContext().clear();
} }
public static void clearGrayUserContext() { public static void clearGrayUserContext() {
GRAY_USER_CONTEXTS.get().clear(); getGrayUserContext().clear();
} }
/** /**
* key. * key.
*/ */
public static void clearUserTags() { public static void clearUserTags() {
USER_CONTEXTS.get().remove(CLOUD_SPACE_TARGET_UNIT_ID); getUserContext().remove(CLOUD_SPACE_TARGET_UNIT_ID);
USER_CONTEXTS.get().remove(CLOUD_SPACE_CUSTOMER_IDENTIFIER); getUserContext().remove(CLOUD_SPACE_CUSTOMER_IDENTIFIER);
USER_CONTEXTS.get().remove(CLOUD_SPACE_TARGET_SYSTEM); getUserContext().remove(CLOUD_SPACE_TARGET_SYSTEM);
} }
public static void setUnitCompositeContextMap(UnitCompositeContextMap unitCompositeContextMap) { public static void setUnitCompositeContextMap(UnitCompositeContextMap unitCompositeContextMap) {
removeAll(); removeAll();
USER_CONTEXTS.get().putAll(unitCompositeContextMap.getUserContext()); getUserContext().putAll(unitCompositeContextMap.getUserContext());
SYSTEM_CONTEXTS.get().putAll(unitCompositeContextMap.getSystemContext()); getSystemContext().putAll(unitCompositeContextMap.getSystemContext());
SOURCE_CONTEXTS.get().putAll(unitCompositeContextMap.getSourceContext()); getSourceContext().putAll(unitCompositeContextMap.getSourceContext());
GRAY_USER_CONTEXTS.get().putAll(unitCompositeContextMap.getGrayUserContext()); getGrayUserContext().putAll(unitCompositeContextMap.getGrayUserContext());
GRAY_SYSTEM_CONTEXTS.get().putAll(unitCompositeContextMap.getGraySystemContext()); getGraySystemContext().putAll(unitCompositeContextMap.getGraySystemContext());
} }
public static UnitCompositeContextMap getCompositeContextMap() { public static UnitCompositeContextMap getCompositeContextMap() {
return new UnitCompositeContextMap(USER_CONTEXTS.get(), SYSTEM_CONTEXTS.get(), ROUTE_CONTEXTS.get(), return new UnitCompositeContextMap(new HashMap<>(getUserContext()), new HashMap<>(getSystemContext()), new HashMap<>(getRouteContext()),
SOURCE_CONTEXTS.get(), GRAY_USER_CONTEXTS.get(), GRAY_SYSTEM_CONTEXTS.get()); new HashMap<>(getSourceContext()), new HashMap<>(getGrayUserContext()), new HashMap<>(getGraySystemContext()));
}
/**
* Internal Use.
*/
public static UnitCompositeContextMap getOriginCompositeContextMap() {
return new UnitCompositeContextMap(getUserContext(), getSystemContext(), getRouteContext(),
getSourceContext(), getGrayUserContext(), getGraySystemContext());
} }
public static String getSourceTag(String key) { public static String getSourceTag(String key) {
return SOURCE_CONTEXTS.get().get(SOURCE_PREFIX + key); return getSourceContext().get(SOURCE_PREFIX + key);
} }
public static String getGrayTag(String position, String key) { public static String getGrayTag(String position, String key) {
return GRAY_USER_CONTEXTS.get().get(getGrayPositionPrefix(position) + key); return getGrayUserContext().get(getGrayPositionPrefix(position) + key);
} }
public static List<GrayMatchRouteUnit> parseGrayMatchRouteUnitList() { public static List<GrayMatchRouteUnit> parseGrayMatchRouteUnitList() {

@ -16,6 +16,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.metadata.core.TransitiveType;
import com.tencent.polaris.metadata.core.manager.CalleeMetadataContainerGroup;
import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant;
import com.tencent.tsf.unit.core.TencentUnitContext; import com.tencent.tsf.unit.core.TencentUnitContext;
import com.tencent.tsf.unit.core.TencentUnitContext.UnitCompositeContextMap; import com.tencent.tsf.unit.core.TencentUnitContext.UnitCompositeContextMap;
import com.tencent.tsf.unit.core.TencentUnitManager; import com.tencent.tsf.unit.core.TencentUnitManager;
@ -247,9 +250,11 @@ public final class TencentUnitUtils {
} }
/** /**
* tsf spring cloud . * tsf spring cloud .
*/ */
public static void enable() { public static void enable() {
CalleeMetadataContainerGroup.getStaticApplicationMetadataContainer().
putMetadataStringValue(ConsulConstant.NAMESPACE_TYPE_KEY, ConsulConstant.NAMESPACE_TYPE_DEFAULT, TransitiveType.NONE);
TencentUnitManager.setEnable(true); TencentUnitManager.setEnable(true);
TsfUnitConsulManager.init(); TsfUnitConsulManager.init();
} }

Loading…
Cancel
Save