Search in sources :

Example 1 with MetadataContext

use of com.tencent.cloud.common.metadata.MetadataContext in project spring-cloud-tencent by Tencent.

the class EncodeTransferMedataFeignInterceptor method apply.

@Override
public void apply(RequestTemplate requestTemplate) {
    // get metadata of current thread
    MetadataContext metadataContext = MetadataContextHolder.get();
    Map<String, String> customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
    if (!CollectionUtils.isEmpty(customMetadata)) {
        String encodedTransitiveMetadata = JacksonUtils.serialize2Json(customMetadata);
        requestTemplate.removeHeader(CUSTOM_METADATA);
        try {
            requestTemplate.header(CUSTOM_METADATA, URLEncoder.encode(encodedTransitiveMetadata, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            LOG.error("Set header failed.", e);
            requestTemplate.header(CUSTOM_METADATA, encodedTransitiveMetadata);
        }
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext)

Example 2 with MetadataContext

use of com.tencent.cloud.common.metadata.MetadataContext in project spring-cloud-tencent by Tencent.

the class EncodeTransferMedataScgFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // get request builder
    ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
    // get metadata of current thread
    MetadataContext metadataContext = exchange.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
    // add new metadata and cover old
    if (metadataContext == null) {
        metadataContext = MetadataContextHolder.get();
    }
    Map<String, String> customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
    if (!CollectionUtils.isEmpty(customMetadata)) {
        String metadataStr = JacksonUtils.serialize2Json(customMetadata);
        try {
            builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, URLEncoder.encode(metadataStr, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);
        }
    }
    return chain.filter(exchange.mutate().request(builder.build()).build());
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext)

Example 3 with MetadataContext

use of com.tencent.cloud.common.metadata.MetadataContext in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerInterceptorTest method testRouterContext.

@Test
public void testRouterContext() throws Exception {
    String callerService = "callerService";
    String calleeService = "calleeService";
    HttpRequest request = new MockedHttpRequest("http://" + calleeService + "/user/get");
    // mock local metadata
    Map<String, String> localMetadata = new HashMap<>();
    localMetadata.put("k1", "v1");
    localMetadata.put("k2", "v2");
    when(metadataLocalProperties.getContent()).thenReturn(localMetadata);
    // mock custom resolved from request
    Map<String, String> customResolvedLabels = new HashMap<>();
    customResolvedLabels.put("k2", "v22");
    customResolvedLabels.put("k4", "v4");
    when(routerLabelResolver.resolve(request, null)).thenReturn(customResolvedLabels);
    // mock expression rule labels
    Set<String> expressionKeys = new HashSet<>();
    expressionKeys.add("${http.method}");
    expressionKeys.add("${http.uri}");
    when(routerRuleLabelResolver.getExpressionLabelKeys(callerService, callerService, calleeService)).thenReturn(expressionKeys);
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn(callerService);
        MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
        // mock transitive metadata
        Map<String, String> transitiveLabels = new HashMap<>();
        transitiveLabels.put("k1", "v1");
        transitiveLabels.put("k2", "v22");
        when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels);
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            PolarisLoadBalancerInterceptor polarisLoadBalancerInterceptor = new PolarisLoadBalancerInterceptor(loadBalancerClient, loadBalancerRequestFactory, Collections.singletonList(routerLabelResolver), metadataLocalProperties, routerRuleLabelResolver);
            PolarisRouterContext routerContext = polarisLoadBalancerInterceptor.genRouterContext(request, null, calleeService);
            verify(metadataLocalProperties).getContent();
            verify(routerRuleLabelResolver).getExpressionLabelKeys(callerService, callerService, calleeService);
            verify(routerLabelResolver).resolve(request, null);
            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.RULE_ROUTER_LABELS).get("k1"));
            Assert.assertEquals("v22", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("k2"));
            Assert.assertEquals("v4", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("k4"));
            Assert.assertEquals("GET", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("${http.method}"));
            Assert.assertEquals("/user/get", routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS).get("${http.uri}"));
        }
    }
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HashMap(java.util.HashMap) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PolarisRouterContext(com.tencent.cloud.polaris.router.PolarisRouterContext) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with MetadataContext

use of com.tencent.cloud.common.metadata.MetadataContext in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerInterceptorTest method testProxyRibbonLoadBalance.

@Test
public void testProxyRibbonLoadBalance() throws Exception {
    String callerService = "callerService";
    String calleeService = "calleeService";
    HttpRequest request = new MockedHttpRequest("http://" + calleeService + "/user/get");
    // mock local metadata
    Map<String, String> localMetadata = new HashMap<>();
    localMetadata.put("k1", "v1");
    localMetadata.put("k2", "v2");
    when(metadataLocalProperties.getContent()).thenReturn(localMetadata);
    // mock custom resolved from request
    Map<String, String> customResolvedLabels = new HashMap<>();
    customResolvedLabels.put("k3", "v3");
    customResolvedLabels.put("k4", "v4");
    when(routerLabelResolver.resolve(request, null)).thenReturn(customResolvedLabels);
    // mock expression rule labels
    Set<String> expressionKeys = new HashSet<>();
    expressionKeys.add("${http.method}");
    expressionKeys.add("${http.uri}");
    when(routerRuleLabelResolver.getExpressionLabelKeys(callerService, callerService, calleeService)).thenReturn(expressionKeys);
    try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
        mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn(callerService);
        MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
        // mock transitive metadata
        Map<String, String> transitiveLabels = new HashMap<>();
        transitiveLabels.put("k1", "v1");
        transitiveLabels.put("k2", "v22");
        when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels);
        try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
            mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
            LoadBalancerRequest<ClientHttpResponse> loadBalancerRequest = new MockedLoadBalancerRequest<>();
            when(loadBalancerRequestFactory.createRequest(request, null, null)).thenReturn(loadBalancerRequest);
            PolarisLoadBalancerInterceptor polarisLoadBalancerInterceptor = new PolarisLoadBalancerInterceptor(loadBalancerClient, loadBalancerRequestFactory, Collections.singletonList(routerLabelResolver), metadataLocalProperties, routerRuleLabelResolver);
            polarisLoadBalancerInterceptor.intercept(request, null, null);
            verify(metadataLocalProperties).getContent();
            verify(routerRuleLabelResolver).getExpressionLabelKeys(callerService, callerService, calleeService);
            verify(routerLabelResolver).resolve(request, null);
        }
    }
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HashMap(java.util.HashMap) ApplicationContextAwareUtils(com.tencent.cloud.common.util.ApplicationContextAwareUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MetadataContextHolder(com.tencent.cloud.common.metadata.MetadataContextHolder) MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with MetadataContext

use of com.tencent.cloud.common.metadata.MetadataContext in project spring-cloud-tencent by Tencent.

the class MetadataCalleeController method info.

/**
 * Get information of callee.
 * @return information of callee
 */
@GetMapping("/info")
public Map<String, String> info() {
    LOG.info("Metadata Service Callee [{}] is called.", port);
    // Get Custom Metadata From Context
    MetadataContext context = MetadataContextHolder.get();
    Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
    customMetadataMap.forEach((key, value) -> {
        LOG.info("Custom Metadata (Key-Value): {} : {}", key, value);
    });
    return customMetadataMap;
}
Also used : MetadataContext(com.tencent.cloud.common.metadata.MetadataContext) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

MetadataContext (com.tencent.cloud.common.metadata.MetadataContext)13 MetadataContextHolder (com.tencent.cloud.common.metadata.MetadataContextHolder)5 ApplicationContextAwareUtils (com.tencent.cloud.common.util.ApplicationContextAwareUtils)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 PolarisRouterContext (com.tencent.cloud.polaris.router.PolarisRouterContext)3 HashSet (java.util.HashSet)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)2 ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)2 SimpleLoadBalancer (com.tencent.cloud.polaris.router.SimpleLoadBalancer)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Mockito.anyString (org.mockito.Mockito.anyString)2 DefaultServerIntrospector (org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector)2 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)2 HttpRequest (org.springframework.http.HttpRequest)2 RequestContext (com.netflix.zuul.context.RequestContext)1