pull/1496/head
shedfreewu 7 months ago
parent e1d557563b
commit e70f5f7b00

@ -20,12 +20,11 @@ package com.tencent.cloud.metadata.core;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.cloud.common.util.UrlUtils;
import com.tencent.cloud.metadata.provider.ReactiveMetadataProvider;
import com.tencent.polaris.api.utils.StringUtils;
@ -99,8 +98,8 @@ public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered
String targetNamespace = serverWebExchange.getRequest().getHeaders().getFirst(MetadataConstant.HeaderName.NAMESPACE);
if (StringUtils.isNotBlank(targetNamespace)) {
MetadataContextUtils.putCallerApplicationMetadataStringValue(
MetadataContextHolder.get(), ContextConstant.POLARIS_TARGET_NAMESPACE, targetNamespace);
MetadataContextHolder.get().putFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, targetNamespace);
}
TransHeadersTransfer.transfer(serverHttpRequest);
return webFilterChain.filter(serverWebExchange)

@ -28,6 +28,7 @@ import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.enums.ConfigFileFormat;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.api.utils.ClassUtils;
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
@ -221,7 +222,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
tsfConfigGroups.add((StringUtils.hasText(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group");
tsfConfigGroups.add((StringUtils.hasText(tsfId) ? tsfId + "." : "") + tsfNamespaceName + ".global_config_group");
if (isGatewayEnabled()) {
if (ClassUtils.isClassPresent("org.springframework.cloud.gateway.filter.GlobalFilter")) {
tsfConfigGroups.add((StringUtils.hasText(tsfId) ? tsfId + "." : "") + tsfGroupName + ".gateway_config_group");
}
for (String tsfConfigGroup : tsfConfigGroups) {
@ -332,14 +333,4 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
}
return configKVFile;
}
public static boolean isGatewayEnabled() {
try {
Class.forName("org.springframework.cloud.gateway.filter.GlobalFilter");
return true;
}
catch (ClassNotFoundException e) {
return false;
}
}
}

@ -17,9 +17,9 @@
package com.tencent.cloud.polaris.discovery;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.polaris.api.core.ConsumerAPI;
@ -52,8 +52,8 @@ public class PolarisDiscoveryHandler {
* @return list of healthy instances
*/
public InstancesResponse getHealthyInstances(String service) {
String namespace = MetadataContextUtils.getCallerApplicationMetadataStringValue(
MetadataContextHolder.get(), ContextConstant.POLARIS_TARGET_NAMESPACE, polarisDiscoveryProperties.getNamespace());
String namespace = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, polarisDiscoveryProperties.getNamespace());
GetHealthyInstancesRequest getHealthyInstancesRequest = new GetHealthyInstancesRequest();
getHealthyInstancesRequest.setNamespace(namespace);

@ -29,7 +29,6 @@ import com.tencent.cloud.polaris.registry.PolarisRegistration;
import com.tencent.cloud.polaris.registry.PolarisRegistrationCustomizer;
import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.util.StringUtils;

@ -25,10 +25,9 @@ import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.Instance;
@ -75,8 +74,8 @@ public final class RouterUtils {
serviceMetadata = instanceList.get(0).getServiceMetadata();
}
String namespace = MetadataContextUtils.getCallerApplicationMetadataStringValue(
MetadataContextHolder.get(), ContextConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
String namespace = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
ServiceKey serviceKey = new ServiceKey(namespace, serviceName);

@ -74,6 +74,7 @@ public class RouterUtilsTest {
.thenReturn(testNamespaceAndService);
MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
Mockito.when(metadataContext.getFragmentContext(anyString(), anyString(), anyString())).thenReturn(testNamespaceAndService);
int instanceSize = 100;
int weight = 50;

@ -40,10 +40,6 @@ public final class ContextConstant {
* Default registry heartbeat time interval: 5 (s).
*/
public static final Integer DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL = 5;
/**
* Name of polaris target namespace.
*/
public static final String POLARIS_TARGET_NAMESPACE = "POLARIS_TARGET_NAMESPACE";
private ContextConstant() {
}

@ -41,6 +41,10 @@ public final class MetadataConstant {
* polaris transitive header prefix length.
*/
public static final int POLARIS_TRANSITIVE_HEADER_PREFIX_LENGTH = POLARIS_TRANSITIVE_HEADER_PREFIX.length();
/**
* Name of polaris target namespace.
*/
public static final String POLARIS_TARGET_NAMESPACE = "POLARIS_TARGET_NAMESPACE";
private MetadataConstant() {

@ -59,6 +59,10 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
* disposable Context.
*/
public static final String FRAGMENT_APPLICATION = "application";
/**
* none Context.
*/
public static final String FRAGMENT_APPLICATION_NONE = "application-none";
/**
* upstream disposable Context.
@ -266,6 +270,8 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
return getMetadataAsMap(MetadataType.CUSTOM, TransitiveType.DISPOSABLE, true);
case FRAGMENT_APPLICATION:
return getMetadataAsMap(MetadataType.APPLICATION, TransitiveType.DISPOSABLE, false);
case FRAGMENT_APPLICATION_NONE:
return getMetadataAsMap(MetadataType.APPLICATION, TransitiveType.NONE, false);
case FRAGMENT_UPSTREAM_APPLICATION:
return getMetadataAsMap(MetadataType.APPLICATION, TransitiveType.DISPOSABLE, true);
case FRAGMENT_RAW_TRANSHEADERS:
@ -277,6 +283,10 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
}
}
public String getFragmentContext(String fragment, String key, String defaultValue) {
return getFragmentContext(fragment).getOrDefault(key, defaultValue);
}
public String getContext(String fragment, String key) {
Map<String, String> fragmentContext = getFragmentContext(fragment);
if (fragmentContext == null) {
@ -305,6 +315,9 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
case FRAGMENT_APPLICATION:
putMetadataAsMap(MetadataType.APPLICATION, TransitiveType.DISPOSABLE, false, context);
break;
case FRAGMENT_APPLICATION_NONE:
putMetadataAsMap(MetadataType.APPLICATION, TransitiveType.NONE, false, context);
break;
case FRAGMENT_UPSTREAM_APPLICATION:
putMetadataAsMap(MetadataType.APPLICATION, TransitiveType.DISPOSABLE, true, context);
break;
@ -320,6 +333,12 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
}
}
public void putFragmentContext(String fragment, String key, String value) {
Map<String, String> context = new HashMap<>(1);
context.put(key, value);
putFragmentContext(fragment, context);
}
public static void setLocalService(String service) {
LOCAL_SERVICE = service;
}

@ -1,41 +0,0 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.common.util;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.polaris.metadata.core.MetadataObjectValue;
import com.tencent.polaris.metadata.core.MetadataType;
public final class MetadataContextUtils {
private MetadataContextUtils() {
}
public static String getCallerApplicationMetadataStringValue(MetadataContext metadataContext, String key, String defaultValue) {
MetadataObjectValue<String> metadataObjectValue = metadataContext.getMetadataContainer(MetadataType.APPLICATION, true).getMetadataValue(key);
if (metadataObjectValue == null) {
return defaultValue;
}
return metadataObjectValue.getObjectValue().orElse(defaultValue);
}
public static void putCallerApplicationMetadataStringValue(MetadataContext metadataContext, String key, String value) {
metadataContext.getMetadataContainer(MetadataType.APPLICATION, true).putMetadataObjectValue(key, value);
}
}

@ -47,7 +47,7 @@ import org.springframework.core.env.Environment;
@Configuration
@ConditionalOnPolarisEnabled
@ConditionalOnProperty(value = "spring.cloud.tencent.plugin.scg.enabled", matchIfMissing = true)
public class SCGPluginsAutoConfiguration {
public class GatewayPluginAutoConfiguration {
@Configuration
@ConditionalOnProperty(value = "spring.cloud.tencent.plugin.scg.context.enabled", matchIfMissing = true)

@ -21,10 +21,8 @@ import java.lang.reflect.Constructor;
import java.net.URI;
import java.util.HashMap;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.polaris.api.utils.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -68,7 +66,7 @@ public class ContextGatewayFilter implements GatewayFilter, Ordered {
private Mono<Void> externalFilter(ServerWebExchange exchange, GatewayFilterChain chain, GroupContext groupContext) {
ServerHttpRequest request = exchange.getRequest();
String[] apis = rebuildExternalApi(request, groupContext, request.getPath().value());
String[] apis = rebuildExternalApi(request, request.getPath().value());
GroupContext.ContextRoute contextRoute = manager.getGroupPathRoute(config.getGroup(), apis[0]);
if (contextRoute == null) {
throw new RuntimeException(String.format("Can't find context route for group: %s, path: %s, origin path: %s", config.getGroup(), apis[0], request.getPath()));
@ -94,7 +92,9 @@ public class ContextGatewayFilter implements GatewayFilter, Ordered {
MetadataContext metadataContext = (MetadataContext) exchange.getAttributes().get(
MetadataConstant.HeaderName.METADATA_CONTEXT);
MetadataContextUtils.putCallerApplicationMetadataStringValue(metadataContext, ContextConstant.POLARIS_TARGET_NAMESPACE, contextRoute.getNamespace());
metadataContext.putFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, contextRoute.getNamespace());
URI requestUri = URI.create("lb://" + contextRoute.getService() + apis[1]);
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUri);
@ -103,7 +103,10 @@ public class ContextGatewayFilter implements GatewayFilter, Ordered {
return chain.filter(exchange.mutate().request(newRequest).build());
}
private String[] rebuildExternalApi(ServerHttpRequest request, GroupContext groupContext, String path) {
/**
* e.g. "/context/api/test" [ "GET|/api/test", "/api/test"]
*/
private String[] rebuildExternalApi(ServerHttpRequest request, String path) {
String[] pathSegments = path.split("/");
StringBuilder matchPath = new StringBuilder();
StringBuilder realPath = new StringBuilder();
@ -122,6 +125,7 @@ public class ContextGatewayFilter implements GatewayFilter, Ordered {
/**
* returns an array of two strings, the first is the match path, the second is the real path.
* e.g. "/context/namespace/svc/api/test" [ "GET|/namespace/svc/api/test", "/api/test"]
*/
private String[] rebuildMsApi(ServerHttpRequest request, GroupContext groupContext, String path) {
String[] pathSegments = path.split("/");

@ -21,9 +21,9 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient;
import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClient;
import com.tencent.polaris.api.utils.StringUtils;
@ -121,7 +121,9 @@ public class ContextGatewayPropertiesManager {
String service = contextRoute.getService();
if (StringUtils.isNotEmpty(namespace) && StringUtils.isNotEmpty(service)) {
logger.info("[{},{}] eager-load start", namespace, service);
MetadataContextUtils.putCallerApplicationMetadataStringValue(MetadataContextHolder.get(), ContextConstant.POLARIS_TARGET_NAMESPACE, namespace);
MetadataContextHolder.get().putFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, namespace);
if (polarisDiscoveryClient != null) {
polarisDiscoveryClient.getInstances(service);
}

@ -1,2 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.plugin.gateway.SCGPluginsAutoConfiguration
com.tencent.cloud.plugin.gateway.GatewayPluginAutoConfiguration

@ -1,65 +0,0 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.plugin.gateway;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
/**
* Test for {@link SCGPluginsAutoConfiguration}.
* @author derek.yi 2022-11-03
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = SCGPluginsAutoConfigurationTest.TestApplication.class,
properties = {"server.port=48081", "spring.config.location = classpath:application-test.yml",
"spring.cloud.tencent.plugin.scg.staining.rule-staining.enabled = true"})
public class SCGPluginsAutoConfigurationTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void testAutoConfiguration() {
// assertThat(applicationContext.getBeansOfType(RuleStainingProperties.class).size()).isEqualTo(1);
// assertThat(applicationContext.getBeansOfType(StainingRuleManager.class).size()).isEqualTo(1);
// assertThat(applicationContext.getBeansOfType(TrafficStainingGatewayFilter.class).size()).isEqualTo(1);
// assertThat(applicationContext.getBeansOfType(RuleStainingExecutor.class).size()).isEqualTo(1);
// assertThat(applicationContext.getBeansOfType(RuleTrafficStainer.class).size()).isEqualTo(1);
}
@SpringBootApplication
public static class TestApplication {
@Bean
public ConfigFileService configFileService(PolarisSDKContextManager polarisSDKContextManager) {
return ConfigFileServiceFactory.createConfigFileService(polarisSDKContextManager.getSDKContext());
}
}
}

@ -97,6 +97,7 @@ public class EnhancedFeignClient implements Client {
try {
// Run pre enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext);
startMillis = System.currentTimeMillis();
Response response = delegate.execute(request, options);
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
@ -115,7 +116,6 @@ public class EnhancedFeignClient implements Client {
return response;
}
catch (CallAbortedException callAbortedException) {
// TODO: shedfree 是否需要执行 exception 插件
// circuit breaker fallback, not need to run post/exception enhanced plugins.
if (callAbortedException.getFallbackInfo() != null) {
return getFallbackResponse(callAbortedException.getFallbackInfo());

@ -83,6 +83,7 @@ public class EnhancedRestTemplateWrapInterceptor {
try {
// Run pre enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext);
startMillis = System.currentTimeMillis();
ClientHttpResponse response = delegate.execute(serviceId, loadBalancerRequest);
// get target instance after execute
@ -103,7 +104,6 @@ public class EnhancedRestTemplateWrapInterceptor {
return response;
}
catch (CallAbortedException callAbortedException) {
// TODO: shedfree 是否需要执行 exception 插件
MetadataObjectValue<Object> fallbackResponseValue = MetadataContextHolder.get().
getMetadataContainer(MetadataType.APPLICATION, true).
getMetadataValue(ContextConstant.CircuitBreaker.CIRCUIT_BREAKER_FALLBACK_HTTP_RESPONSE);

@ -21,12 +21,10 @@ import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
@ -81,8 +79,8 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
metadataContext = MetadataContextHolder.get();
}
String governanceNamespace = MetadataContextUtils.getCallerApplicationMetadataStringValue(
metadataContext, ContextConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
String governanceNamespace = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
@ -102,8 +100,8 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext);
}
catch (CallAbortedException e) {
// TODO: shedfree 是否需要执行 exception 插件
pluginRunner.run(EnhancedPluginType.Client.EXCEPTION, enhancedPluginContext);
// Run finally enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.FINALLY, enhancedPluginContext);
if (e.getFallbackInfo() == null) {
throw e;
}
@ -116,11 +114,7 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
}
String body = Optional.of(e.getFallbackInfo().getBody()).orElse("");
DataBuffer dataBuffer = response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
return response.writeWith(Mono.just(dataBuffer)).doFinally(v -> {
// TODO: shedfree 是在外层的 finally 中执行吗?
// Run finally enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.FINALLY, enhancedPluginContext);
});
return response.writeWith(Mono.just(dataBuffer));
}
// Exchange may be changed in plugin
ServerWebExchange exchange = (ServerWebExchange) enhancedPluginContext.getOriginRequest();

@ -74,7 +74,9 @@ public class EnhancedWebClientExchangeFilterFunction implements ExchangeFilterFu
pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext);
}
catch (CallAbortedException e) {
// TODO: shedfree 是否需要执行 exception 插件
// Run finally enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.FINALLY, enhancedPluginContext);
if (e.getFallbackInfo() == null) {
throw e;
}
@ -84,11 +86,7 @@ public class EnhancedWebClientExchangeFilterFunction implements ExchangeFilterFu
if (CollectionUtils.isNotEmpty(e.getFallbackInfo().getHeaders())) {
e.getFallbackInfo().getHeaders().forEach(responseBuilder::header);
}
return Mono.just(responseBuilder.build()).doFinally(v -> {
// TODO: shedfree 是在外层的 finally 中执行吗?
// Run finally enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.FINALLY, enhancedPluginContext);
});
return Mono.just(responseBuilder.build());
}
// request may be changed by plugin
ClientRequest request = (ClientRequest) enhancedPluginContext.getOriginRequest();

@ -32,13 +32,12 @@ import java.util.Objects;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.HeaderConstant;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.constant.RouterConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.cloud.common.util.RequestLabelUtils;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
@ -137,8 +136,8 @@ public final class PolarisEnhancedPluginUtils {
@Nullable String calleeServiceName, @Nullable String calleeHost, @Nullable Integer calleePort,
URI uri, @Nullable Integer statusCode, long delay, @Nullable Throwable exception) {
String governanceNamespace = MetadataContextUtils.getCallerApplicationMetadataStringValue(
MetadataContextHolder.get(), ContextConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
String governanceNamespace = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
ServiceKey calleeServiceKey = new ServiceKey(governanceNamespace, StringUtils.isBlank(calleeServiceName) ? uri.getHost() : calleeServiceName);
ServiceKey callerServiceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);

@ -17,10 +17,10 @@
package com.tencent.cloud.rpc.enhancement.transformer;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.cloud.common.util.MetadataContextUtils;
import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.api.utils.CollectionUtils;
@ -46,8 +46,9 @@ public class PolarisInstanceTransformer implements InstanceTransformer {
instance.setServiceMetadata(polarisServiceInstance.getServiceMetadata());
}
String namespace = MetadataContextUtils.getCallerApplicationMetadataStringValue(
MetadataContextHolder.get(), ContextConstant.POLARIS_TARGET_NAMESPACE, instance.getNamespace());
String namespace = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, instance.getNamespace());
instance.setNamespace(namespace);
}
}

@ -173,7 +173,7 @@ class EnhancedRestTemplateWrapInterceptorTest {
when(pluginRunner.getLocalServiceInstance()).thenReturn(localServiceInstance);
doThrow(abortedException)
.when(pluginRunner)
.run(any(), any());
.run(eq(EnhancedPluginType.Client.PRE), any(EnhancedPluginContext.class));
Object fallbackResponse = new MockClientHttpResponse();
MetadataContextHolder.get().getMetadataContainer(MetadataType.APPLICATION, true).

Loading…
Cancel
Save