Search in sources :

Example 6 with PolarisRouterContext

use of com.tencent.cloud.polaris.router.PolarisRouterContext in project spring-cloud-tencent by Tencent.

the class PolarisFeignLoadBalancer method customizeLoadBalancerCommandBuilder.

@Override
protected void customizeLoadBalancerCommandBuilder(RibbonRequest request, IClientConfig config, LoadBalancerCommand.Builder<RibbonResponse> builder) {
    Map<String, Collection<String>> headers = request.getRequest().headers();
    PolarisRouterContext routerContext = buildRouterContext(headers);
    builder.withServerLocator(routerContext);
}
Also used : Collection(java.util.Collection) PolarisRouterContext(com.tencent.cloud.polaris.router.PolarisRouterContext)

Example 7 with PolarisRouterContext

use of com.tencent.cloud.polaris.router.PolarisRouterContext in project spring-cloud-tencent by Tencent.

the class PolarisFeignLoadBalancer method buildRouterContext.

// set method to public for unit test
PolarisRouterContext buildRouterContext(Map<String, Collection<String>> headers) {
    Collection<String> labelHeaderValues = headers.get(RouterConstants.ROUTER_LABEL_HEADER);
    if (CollectionUtils.isEmpty(labelHeaderValues)) {
        return null;
    }
    PolarisRouterContext routerContext = new PolarisRouterContext();
    routerContext.setLabels(PolarisRouterContext.TRANSITIVE_LABELS, MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE));
    labelHeaderValues.forEach(labelHeaderValue -> {
        Map<String, String> labels = JacksonUtils.deserialize2Map(labelHeaderValue);
        if (!CollectionUtils.isEmpty(labels)) {
            Map<String, String> unescapeLabels = new HashMap<>(labels.size());
            for (Map.Entry<String, String> entry : labels.entrySet()) {
                String escapedKey = ExpressionLabelUtils.unescape(entry.getKey());
                String escapedValue = ExpressionLabelUtils.unescape(entry.getValue());
                unescapeLabels.put(escapedKey, escapedValue);
            }
            routerContext.setLabels(PolarisRouterContext.RULE_ROUTER_LABELS, unescapeLabels);
        }
    });
    return routerContext;
}
Also used : HashMap(java.util.HashMap) PolarisRouterContext(com.tencent.cloud.polaris.router.PolarisRouterContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PolarisRouterContext (com.tencent.cloud.polaris.router.PolarisRouterContext)7 HashMap (java.util.HashMap)5 MetadataContext (com.tencent.cloud.common.metadata.MetadataContext)3 MetadataContextHolder (com.tencent.cloud.common.metadata.MetadataContextHolder)3 ApplicationContextAwareUtils (com.tencent.cloud.common.util.ApplicationContextAwareUtils)3 Collection (java.util.Collection)3 Test (org.junit.Test)3 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)2 ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)2 SimpleLoadBalancer (com.tencent.cloud.polaris.router.SimpleLoadBalancer)2 Mockito.anyString (org.mockito.Mockito.anyString)2 DefaultServerIntrospector (org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector)2 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 RibbonLoadBalancerClient (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient)1 HttpRequest (org.springframework.http.HttpRequest)1