optimize:optimize PolarisRouterContext and constants. (#628)

* optimize:optimize PolarisRouterContext and constants.

* add to CHANGELOG.md.
pull/631/head
Haotian Zhang 2 years ago committed by GitHub
parent add1ce423a
commit 2c37f1a317
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,3 +6,4 @@
- [Optimize: optimize configuration conditional & optimize config data tips"](https://github.com/Tencent/spring-cloud-tencent/pull/605)
- [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/609)
- [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/623)
- [optimize:optimize PolarisRouterContext and constants.](https://github.com/Tencent/spring-cloud-tencent/pull/628)

@ -32,7 +32,6 @@ import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
import com.netflix.loadbalancer.ZoneAvoidanceRule;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.cloud.polaris.loadbalancer.LoadBalancerUtils;

@ -13,10 +13,9 @@
* 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.constant;
package com.tencent.cloud.polaris.router;
import java.util.Arrays;
import java.util.Collections;
@ -25,6 +24,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.RouterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
@ -35,14 +35,6 @@ import org.springframework.util.CollectionUtils;
* @author lepdou 2022-05-17
*/
public class PolarisRouterContext {
/**
* the labels for rule router, contain transitive metadata.
*/
public static final String ROUTER_LABELS = "allMetadata";
/**
* transitive labels.
*/
public static final String TRANSITIVE_LABELS = "transitiveMetadata";
private Map<String, Map<String, String>> labels;
@ -78,7 +70,7 @@ public class PolarisRouterContext {
}
public String getLabel(String labelKey) {
Map<String, String> routerLabels = labels.get(ROUTER_LABELS);
Map<String, String> routerLabels = labels.get(RouterConstant.ROUTER_LABELS);
if (CollectionUtils.isEmpty(routerLabels)) {
return StringUtils.EMPTY;
}
@ -86,7 +78,7 @@ public class PolarisRouterContext {
}
public Set<String> getLabelAsSet(String labelKey) {
Map<String, String> routerLabels = labels.get(ROUTER_LABELS);
Map<String, String> routerLabels = labels.get(RouterConstant.ROUTER_LABELS);
if (CollectionUtils.isEmpty(routerLabels)) {
return Collections.emptySet();
}

@ -28,11 +28,11 @@ import java.util.Optional;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.reactive.LoadBalancerCommand;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.common.constant.RouterConstants;
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.JacksonUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer;
@ -63,7 +63,7 @@ public class PolarisFeignLoadBalancer extends FeignLoadBalancer {
//set method to public for unit test
PolarisRouterContext buildRouterContext(Map<String, Collection<String>> headers) {
Collection<String> labelHeaderValues = headers.get(RouterConstants.ROUTER_LABEL_HEADER);
Collection<String> labelHeaderValues = headers.get(RouterConstant.ROUTER_LABEL_HEADER);
if (CollectionUtils.isEmpty(labelHeaderValues)) {
return null;
@ -71,7 +71,7 @@ public class PolarisFeignLoadBalancer extends FeignLoadBalancer {
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.TRANSITIVE_LABELS, MetadataContextHolder.get()
routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, MetadataContextHolder.get()
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE));
Map<String, String> labelHeaderValuesMap = new HashMap<>();
@ -86,7 +86,7 @@ public class PolarisFeignLoadBalancer extends FeignLoadBalancer {
catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported charset exception " + UTF_8);
}
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labelHeaderValuesMap);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labelHeaderValuesMap);
return routerContext;
}

@ -27,7 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.RouterConstants;
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.metadata.StaticMetadataManager;
@ -115,7 +115,7 @@ public class RouterLabelFeignInterceptor implements RequestInterceptor, Ordered
catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported charset exception " + UTF_8);
}
requestTemplate.header(RouterConstants.ROUTER_LABEL_HEADER, encodedLabelsContent);
requestTemplate.header(RouterConstant.ROUTER_LABEL_HEADER, encodedLabelsContent);
}
private Map<String, String> getRuleExpressionLabels(RequestTemplate requestTemplate, Set<String> labelKeys) {

@ -21,7 +21,8 @@ package com.tencent.cloud.polaris.router.interceptor;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.common.constant.RouterConstant;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties;
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
import com.tencent.polaris.plugins.router.metadata.MetadataRouter;
@ -49,7 +50,7 @@ public class MetadataRouterRequestInterceptor implements RouterRequestIntercepto
// 1. get metadata router label keys
Set<String> metadataRouterKeys = routerContext.getLabelAsSet(LABEL_KEY_METADATA_ROUTER_KEYS);
// 2. get metadata router labels
Map<String, String> metadataRouterLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS,
Map<String, String> metadataRouterLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS,
metadataRouterKeys);
// 3. set metadata router labels to request
request.addRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA, metadataRouterLabels);

@ -21,7 +21,7 @@ package com.tencent.cloud.polaris.router.interceptor;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
import com.tencent.polaris.plugins.router.nearby.NearbyRouter;

@ -21,7 +21,8 @@ package com.tencent.cloud.polaris.router.interceptor;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.common.constant.RouterConstant;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties;
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
import com.tencent.polaris.plugins.router.rule.RuleBasedRouter;
@ -52,7 +53,7 @@ public class RuleBasedRouterRequestInterceptor implements RouterRequestIntercept
// is placed in the metadata of the source service for transmission.
// Later, can consider putting it in routerMetadata like other routers.
if (ruleBasedRouterEnabled) {
Map<String, String> ruleRouterLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS);
Map<String, String> ruleRouterLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS);
request.getSourceService().setMetadata(ruleRouterLabels);
}
}

@ -29,12 +29,13 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.common.util.expresstion.SpringWebExpressionLabelUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.SpringWebRouterLabelResolver;
import org.slf4j.Logger;
@ -103,7 +104,7 @@ public class PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor {
PolarisRouterContext routerContext = genRouterContext(request, body, peerServiceName);
ClientHttpResponse response = ((RibbonLoadBalancerClient) loadBalancer).execute(peerServiceName,
this.requestFactory.createRequest(request, body, execution), routerContext);
Map<String, String> labels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS);
Map<String, String> labels = routerContext.getLabels(RouterConstant.ROUTER_LABELS);
// put labels in header
String encodedLabelsContent;
@ -113,7 +114,7 @@ public class PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor {
catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported charset exception " + UTF_8);
}
response.getHeaders().add(PolarisRouterContext.ROUTER_LABELS, encodedLabelsContent);
response.getHeaders().add(RouterConstant.ROUTER_LABELS, encodedLabelsContent);
return response;
}
@ -155,8 +156,8 @@ public class PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor {
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(PolarisRouterContext.TRANSITIVE_LABELS, transitiveLabels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, transitiveLabels);
return routerContext;
}

@ -26,11 +26,12 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.expresstion.SpringWebExpressionLabelUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.SpringWebRouterLabelResolver;
import org.slf4j.Logger;
@ -128,8 +129,8 @@ public class PolarisLoadBalancerClientFilter extends LoadBalancerClientFilter {
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(PolarisRouterContext.TRANSITIVE_LABELS, transitiveLabels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, transitiveLabels);
return routerContext;
}

@ -18,7 +18,7 @@
package com.tencent.cloud.polaris.router.spi;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
/**

@ -18,7 +18,7 @@
package com.tencent.cloud.polaris.router.spi;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse;
/**

@ -29,12 +29,13 @@ import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.BeanFactoryUtils;
import com.tencent.cloud.common.util.expresstion.ServletExpressionLabelUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.ServletRouterLabelResolver;
import org.slf4j.Logger;
@ -166,8 +167,8 @@ public class PolarisRibbonRoutingFilter extends RibbonRoutingFilter implements B
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(PolarisRouterContext.TRANSITIVE_LABELS, transitiveLabels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, transitiveLabels);
return routerContext;
}

@ -35,7 +35,7 @@ import com.netflix.loadbalancer.RetryRule;
import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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.pojo.PolarisServer;
@ -82,6 +82,7 @@ import static org.mockito.Mockito.when;
public class PolarisLoadBalancerCompositeRuleTest {
private static final AtomicBoolean initTransitiveMetadata = new AtomicBoolean(false);
private final List<RouterRequestInterceptor> requestInterceptors = new ArrayList<>();
@Mock
private PolarisLoadBalancerProperties polarisLoadBalancerProperties;
@Mock
@ -92,14 +93,11 @@ public class PolarisLoadBalancerCompositeRuleTest {
private PolarisRuleBasedRouterProperties polarisRuleBasedRouterProperties;
@Mock
private RouterAPI routerAPI;
private IClientConfig config;
private String testNamespace = "testNamespace";
private String testCallerService = "testCallerService";
private String testCalleeService = "testCalleeService";
private final List<RouterRequestInterceptor> requestInterceptors = new ArrayList<>();
@Before
public void before() {
config = new DefaultClientConfigImpl();
@ -214,10 +212,10 @@ public class PolarisLoadBalancerCompositeRuleTest {
ServiceInstances serviceInstances = assembleServiceInstances();
PolarisRouterContext routerContext = assembleRouterContext();
Map<String, String> oldRouterLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS);
Map<String, String> oldRouterLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS);
Map<String, String> newRouterLabels = new HashMap<>(oldRouterLabels);
newRouterLabels.put("system-metadata-router-keys", "k2");
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, newRouterLabels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, newRouterLabels);
ProcessRoutersRequest request = compositeRule.buildProcessRoutersBaseRequest(serviceInstances);
compositeRule.processRouterRequestInterceptors(request, routerContext);
@ -346,8 +344,8 @@ public class PolarisLoadBalancerCompositeRuleTest {
Map<String, String> routerLabels = new HashMap<>();
routerLabels.put("k2", "v2");
routerLabels.put("k3", "v3");
routerContext.putLabels(PolarisRouterContext.TRANSITIVE_LABELS, transitiveLabels);
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, routerLabels);
routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, transitiveLabels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, routerLabels);
return routerContext;
}

@ -23,7 +23,7 @@ import java.util.Map;
import java.util.Set;
import com.google.common.collect.Sets;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.common.constant.RouterConstant;
import org.junit.Assert;
import org.junit.Test;
@ -41,28 +41,28 @@ public class PolarisRouterContextTest {
labels.put("k2", "v2");
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
Assert.assertEquals(0, routerContext.getLabels(PolarisRouterContext.TRANSITIVE_LABELS).size());
Assert.assertEquals(2, routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).size());
Assert.assertEquals("v1", routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).get("k1"));
Assert.assertEquals("v2", routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).get("k2"));
Assert.assertNull(routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).get("k3"));
Assert.assertEquals(0, routerContext.getLabels(RouterConstant.TRANSITIVE_LABELS).size());
Assert.assertEquals(2, routerContext.getLabels(RouterConstant.ROUTER_LABELS).size());
Assert.assertEquals("v1", routerContext.getLabels(RouterConstant.ROUTER_LABELS).get("k1"));
Assert.assertEquals("v2", routerContext.getLabels(RouterConstant.ROUTER_LABELS).get("k2"));
Assert.assertNull(routerContext.getLabels(RouterConstant.ROUTER_LABELS).get("k3"));
}
@Test
public void testSetNull() {
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, null);
Assert.assertEquals(0, routerContext.getLabels(PolarisRouterContext.TRANSITIVE_LABELS).size());
Assert.assertEquals(0, routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).size());
routerContext.putLabels(RouterConstant.ROUTER_LABELS, null);
Assert.assertEquals(0, routerContext.getLabels(RouterConstant.TRANSITIVE_LABELS).size());
Assert.assertEquals(0, routerContext.getLabels(RouterConstant.ROUTER_LABELS).size());
}
@Test
public void testGetEmptyRouterContext() {
PolarisRouterContext routerContext = new PolarisRouterContext();
Assert.assertEquals(0, routerContext.getLabels(PolarisRouterContext.TRANSITIVE_LABELS).size());
Assert.assertEquals(0, routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).size());
Assert.assertEquals(0, routerContext.getLabels(RouterConstant.TRANSITIVE_LABELS).size());
Assert.assertEquals(0, routerContext.getLabels(RouterConstant.ROUTER_LABELS).size());
}
@Test
@ -73,9 +73,9 @@ public class PolarisRouterContextTest {
labels.put("k3", "v3");
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
Map<String, String> resolvedLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS,
Map<String, String> resolvedLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS,
Sets.newHashSet("k1", "k2", "k4"));
Assert.assertEquals(2, resolvedLabels.size());
@ -91,7 +91,7 @@ public class PolarisRouterContextTest {
labels.put("k3", "v3");
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
String resolvedLabel = routerContext.getLabel("k1");
@ -104,7 +104,7 @@ public class PolarisRouterContextTest {
labels.put("k1", "v1,v2,v3");
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
Set<String> resolvedLabels = routerContext.getLabelAsSet("k1");

@ -26,12 +26,12 @@ import java.util.Map;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.loadbalancer.ILoadBalancer;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.common.constant.RouterConstants;
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.JacksonUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.SimpleLoadBalancer;
import org.junit.Assert;
import org.junit.Test;
@ -70,7 +70,7 @@ public class PolarisFeignLoadBalancerTest {
headerValues.add(JacksonUtils.serialize2Json(labels));
Map<String, Collection<String>> headers = new HashMap<>();
headers.put(RouterConstants.ROUTER_LABEL_HEADER, headerValues);
headers.put(RouterConstant.ROUTER_LABEL_HEADER, headerValues);
// mock ApplicationContextAwareUtils#getProperties
try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
@ -85,7 +85,7 @@ public class PolarisFeignLoadBalancerTest {
PolarisRouterContext routerContext = polarisFeignLoadBalancer.buildRouterContext(headers);
Assert.assertNotNull(routerContext);
Map<String, String> routerLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS);
Map<String, String> routerLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS);
Assert.assertNotNull(routerLabels);
Assert.assertEquals("v1", routerLabels.get("k1"));
Assert.assertEquals("v2", routerLabels.get("k2"));

@ -27,7 +27,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.RouterConstants;
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.metadata.StaticMetadataManager;
@ -117,7 +117,7 @@ public class RouterLabelFeignInterceptorTest {
routerLabelFeignInterceptor.apply(requestTemplate);
Collection<String> routerLabels = requestTemplate.headers().get(RouterConstants.ROUTER_LABEL_HEADER);
Collection<String> routerLabels = requestTemplate.headers().get(RouterConstant.ROUTER_LABEL_HEADER);
Map<String, String> routerLabelsMap = new HashMap<>();
try {
String routerLabelContent = routerLabels.stream().findFirst().get();

@ -28,13 +28,14 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.common.util.expresstion.SpringWebExpressionLabelUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.SpringWebRouterLabelResolver;
import org.assertj.core.api.Assertions;
@ -150,7 +151,8 @@ public class PolarisLoadBalancerInterceptorTest {
catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported charset exception " + UTF_8);
}
Assertions.assertThat(mockedResponse.getHeaders().get(PolarisRouterContext.ROUTER_LABELS).get(0)).isEqualTo(encodedLabelsContent);
Assertions.assertThat(mockedResponse.getHeaders().get(RouterConstant.ROUTER_LABELS).get(0))
.isEqualTo(encodedLabelsContent);
}
}
}
@ -230,14 +232,14 @@ public class PolarisLoadBalancerInterceptorTest {
verify(routerRuleLabelResolver).getExpressionLabelKeys(callerService, callerService, calleeService);
verify(routerLabelResolver).resolve(request, null, expressionKeys);
Assert.assertEquals("v1", routerContext.getLabels(PolarisRouterContext.TRANSITIVE_LABELS).get("k1"));
Assert.assertEquals("v22", routerContext.getLabels(PolarisRouterContext.TRANSITIVE_LABELS).get("k2"));
Assert.assertEquals("v1", routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).get("k1"));
Assert.assertEquals("v22", routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).get("k2"));
Assert.assertEquals("v4", routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS).get("k4"));
Assert.assertEquals("GET", routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS)
Assert.assertEquals("v1", routerContext.getLabels(RouterConstant.TRANSITIVE_LABELS).get("k1"));
Assert.assertEquals("v22", routerContext.getLabels(RouterConstant.TRANSITIVE_LABELS).get("k2"));
Assert.assertEquals("v1", routerContext.getLabels(RouterConstant.ROUTER_LABELS).get("k1"));
Assert.assertEquals("v22", routerContext.getLabels(RouterConstant.ROUTER_LABELS).get("k2"));
Assert.assertEquals("v4", routerContext.getLabels(RouterConstant.ROUTER_LABELS).get("k4"));
Assert.assertEquals("GET", routerContext.getLabels(RouterConstant.ROUTER_LABELS)
.get("${http.method}"));
Assert.assertEquals("/user/get", routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS)
Assert.assertEquals("/user/get", routerContext.getLabels(RouterConstant.ROUTER_LABELS)
.get("${http.uri}"));
}
}

@ -25,11 +25,12 @@ import java.util.Set;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.SpringWebRouterLabelResolver;
import org.junit.AfterClass;
@ -127,7 +128,7 @@ public class PolarisLoadBalancerClientFilterTest {
PolarisRouterContext routerContext = polarisLoadBalancerClientFilter.genRouterContext(webExchange, calleeService);
Map<String, String> routerLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS);
Map<String, String> routerLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS);
Assert.assertEquals("v1", routerLabels.get("${http.header.k1}"));
Assert.assertEquals("zhangsan", routerLabels.get("${http.query.userid}"));
Assert.assertEquals("blue", routerLabels.get("env"));

@ -29,12 +29,13 @@ import com.netflix.client.config.IClientConfig;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.niws.client.http.RestClient;
import com.netflix.zuul.context.RequestContext;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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.metadata.StaticMetadataManager;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.ServletRouterLabelResolver;
import okhttp3.OkHttpClient;
@ -144,7 +145,7 @@ public class PolarisRibbonRoutingFilterTest {
PolarisRouterContext routerContext = polarisRibbonRoutingFilter.genRouterContext(request, calleeService);
Map<String, String> routerLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS);
Map<String, String> routerLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS);
Assert.assertEquals("v1", routerLabels.get("${http.header.k1}"));
Assert.assertEquals("zhangsan", routerLabels.get("${http.query.userid}"));
Assert.assertEquals("blue", routerLabels.get("env"));

@ -23,17 +23,24 @@ package com.tencent.cloud.common.constant;
*
* @author lepdou 2022-05-17
*/
public final class RouterConstants {
public final class RouterConstant {
/**
* Default Private Constructor.
* the labels for rule router, contain transitive metadata.
*/
private RouterConstants() {
}
public static final String ROUTER_LABELS = "allMetadata";
/**
* transitive labels.
*/
public static final String TRANSITIVE_LABELS = "transitiveMetadata";
/**
* the header of router label.
*/
public static final String ROUTER_LABEL_HEADER = "internal-router-label";
/**
* Default Private Constructor.
*/
private RouterConstant() {
}
}

@ -21,7 +21,7 @@ package com.tencent.cloud.plugin.featureenv;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
import com.tencent.polaris.api.rpc.MetadataFailoverType;
import com.tencent.polaris.plugins.router.metadata.MetadataRouter;

@ -22,7 +22,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.common.constant.PolarisRouterContext;
import com.tencent.cloud.common.constant.RouterConstant;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.pojo.ServiceKey;
@ -46,7 +47,7 @@ public class FeatureEnvRouterRequestInterceptorTest {
Map<String, String> labels = new HashMap<>();
labels.put("featureenv", "blue");
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
ProcessRoutersRequest request = new ProcessRoutersRequest();
ServiceInstances serviceInstances = new DefaultServiceInstances(Mockito.mock(ServiceKey.class), new ArrayList<>());
@ -67,7 +68,7 @@ public class FeatureEnvRouterRequestInterceptorTest {
labels.put("system-feature-env-router-label", "specify-env");
labels.put("specify-env", "blue");
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
ProcessRoutersRequest request = new ProcessRoutersRequest();
ServiceInstances serviceInstances = new DefaultServiceInstances(Mockito.mock(ServiceKey.class), new ArrayList<>());
@ -87,7 +88,7 @@ public class FeatureEnvRouterRequestInterceptorTest {
Map<String, String> labels = new HashMap<>();
labels.put("system-feature-env-router-label", "specify-env");
PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
routerContext.putLabels(RouterConstant.ROUTER_LABELS, labels);
ProcessRoutersRequest request = new ProcessRoutersRequest();
ServiceInstances serviceInstances = new DefaultServiceInstances(Mockito.mock(ServiceKey.class), new ArrayList<>());

@ -22,7 +22,7 @@ import java.net.URI;
import java.net.URLDecoder;
import java.util.Collection;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.constant.RouterConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.pojo.ServiceKey;
@ -54,7 +54,7 @@ public final class ReporterUtils {
resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE);
RequestTemplate requestTemplate = request.requestTemplate();
String serviceName = requestTemplate.feignTarget().name();
Collection<String> labels = requestTemplate.headers().get(RouterConstants.ROUTER_LABEL_HEADER);
Collection<String> labels = requestTemplate.headers().get(RouterConstant.ROUTER_LABEL_HEADER);
if (CollectionUtils.isNotEmpty(labels) && labels.iterator().hasNext()) {
String label = labels.iterator().next();
try {

@ -24,7 +24,7 @@ import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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.rpc.enhancement.AbstractPolarisReporterAdapter;
@ -55,10 +55,8 @@ import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
*/
public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter implements ResponseErrorHandler, ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedRestTemplateReporter.class);
static final String HEADER_HAS_ERROR = "X-SCT-Has-Error";
private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedRestTemplateReporter.class);
private final ConsumerAPI consumerAPI;
private ResponseErrorHandler delegateHandler;
@ -136,7 +134,7 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter
resultRequest.setRetStatus(RetStatus.RetFail);
}
List<String> labels = response.getHeaders().get(PolarisRouterContext.ROUTER_LABELS);
List<String> labels = response.getHeaders().get(RouterConstant.ROUTER_LABELS);
if (CollectionUtils.isNotEmpty(labels)) {
String label = labels.get(0);
try {

@ -20,7 +20,7 @@ package com.tencent.cloud.rpc.enhancement.feign.plugin.reporter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.tencent.cloud.common.constant.RouterConstants;
import com.tencent.cloud.common.constant.RouterConstant;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.polaris.api.pojo.RetStatus;
@ -83,7 +83,7 @@ public class ReporterUtilsTest {
RequestTemplate requestTemplate = new RequestTemplate();
requestTemplate.feignTarget(target);
try {
requestTemplate.header(RouterConstants.ROUTER_LABEL_HEADER, URLEncoder.encode("{\"k1\":\"v1\",\"k2\":\"v2\"}", UTF_8));
requestTemplate.header(RouterConstant.ROUTER_LABEL_HEADER, URLEncoder.encode("{\"k1\":\"v1\",\"k2\":\"v2\"}", UTF_8));
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported charset exception " + UTF_8);

@ -24,7 +24,7 @@ import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.common.constant.PolarisRouterContext;
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;
@ -129,7 +129,7 @@ public class EnhancedRestTemplateReporterTest {
URI uri = mock(URI.class);
String labels = URLEncoder.encode("{\"k1\":\"v1\",\"k2\":\"v2\"}", UTF_8);
response.getHeaders().set(PolarisRouterContext.ROUTER_LABELS, labels);
response.getHeaders().set(RouterConstant.ROUTER_LABELS, labels);
enhancedRestTemplateReporter.handleError(uri, HttpMethod.GET, response);
verify(consumerAPI, times(2)).updateServiceCallResult((ServiceCallResult) captor.capture());

Loading…
Cancel
Save