Search in sources :

Example 1 with MetadataContext

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

the class Metadata2HeaderScgFilter method doFilter.

@Override
public Mono<Void> doFilter(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.getAllTransitiveCustomMetadata();
    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.metadata.context.MetadataContext)

Example 2 with MetadataContext

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

the class RateLimitScgFilter method doFilter.

@Override
public Mono<Void> doFilter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // get metadata of current thread
    MetadataContext metadataContext = exchange.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
    String peerNamespace = metadataContext.getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE);
    String peerService = metadataContext.getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE);
    String peerPath = metadataContext.getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH);
    Map<String, String> labels = null;
    if (StringUtils.isNotBlank(peerPath)) {
        labels = new HashMap<>();
        labels.put("method", peerPath);
    }
    try {
        QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI, peerNamespace, peerService, 1, labels, null);
        if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) {
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
            response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
            DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write((Consts.QUOTA_LIMITED_INFO + quotaResponse.getInfo()).getBytes(StandardCharsets.UTF_8));
            return response.writeWith(Mono.just(dataBuffer));
        }
    } catch (Throwable throwable) {
        // 限流API调用出现异常,不应该影响业务流程的调用
        LOG.error("fail to rate limit with QuotaRequest[{}-{}-{}].", peerNamespace, peerService, peerPath, throwable);
    }
    return chain.filter(exchange);
}
Also used : MetadataContext(com.tencent.cloud.metadata.context.MetadataContext) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 3 with MetadataContext

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

the class Metadata2HeaderZuulFilter method run.

@Override
public Object run() {
    // get request context
    RequestContext requestContext = RequestContext.getCurrentContext();
    // get metadata of current thread
    MetadataContext metadataContext = MetadataContextHolder.get();
    // add new metadata and cover old
    Map<String, String> customMetadata = metadataContext.getAllTransitiveCustomMetadata();
    if (!CollectionUtils.isEmpty(customMetadata)) {
        String metadataStr = JacksonUtils.serialize2Json(customMetadata);
        try {
            requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, URLEncoder.encode(metadataStr, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);
        }
    }
    return null;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestContext(com.netflix.zuul.context.RequestContext) MetadataContext(com.tencent.cloud.metadata.context.MetadataContext)

Example 4 with MetadataContext

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

the class Metadata2HeaderFeignInterceptor method apply.

@Override
public void apply(RequestTemplate requestTemplate) {
    // get metadata of current thread
    MetadataContext metadataContext = MetadataContextHolder.get();
    // add new metadata and cover old
    if (!CollectionUtils.isEmpty(requestTemplate.headers()) && !CollectionUtils.isEmpty(requestTemplate.headers().get(CUSTOM_METADATA))) {
        for (String headerMetadataStr : requestTemplate.headers().get(CUSTOM_METADATA)) {
            Map<String, String> headerMetadataMap = JacksonUtils.deserialize2Map(headerMetadataStr);
            for (String key : headerMetadataMap.keySet()) {
                metadataContext.putTransitiveCustomMetadata(key, headerMetadataMap.get(key));
            }
        }
    }
    Map<String, String> customMetadata = metadataContext.getAllTransitiveCustomMetadata();
    if (!CollectionUtils.isEmpty(customMetadata)) {
        String metadataStr = JacksonUtils.serialize2Json(customMetadata);
        requestTemplate.removeHeader(CUSTOM_METADATA);
        try {
            requestTemplate.header(CUSTOM_METADATA, URLEncoder.encode(metadataStr, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            LOG.error("Set header failed.", e);
            requestTemplate.header(CUSTOM_METADATA, metadataStr);
        }
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) MetadataContext(com.tencent.cloud.metadata.context.MetadataContext)

Example 5 with MetadataContext

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

the class MetadataRestTemplateInterceptor method intercept.

@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
    // get metadata of current thread
    MetadataContext metadataContext = MetadataContextHolder.get();
    // add new metadata and cover old
    String metadataStr = httpRequest.getHeaders().getFirst(MetadataConstant.HeaderName.CUSTOM_METADATA);
    if (!StringUtils.isEmpty(metadataStr)) {
        Map<String, String> headerMetadataMap = JacksonUtils.deserialize2Map(metadataStr);
        for (String key : headerMetadataMap.keySet()) {
            metadataContext.putTransitiveCustomMetadata(key, headerMetadataMap.get(key));
        }
    }
    Map<String, String> customMetadata = metadataContext.getAllTransitiveCustomMetadata();
    if (!CollectionUtils.isEmpty(customMetadata)) {
        metadataStr = JacksonUtils.serialize2Json(customMetadata);
        try {
            httpRequest.getHeaders().set(MetadataConstant.HeaderName.CUSTOM_METADATA, URLEncoder.encode(metadataStr, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            httpRequest.getHeaders().set(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);
        }
    }
    return clientHttpRequestExecution.execute(httpRequest, bytes);
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) MetadataContext(com.tencent.cloud.metadata.context.MetadataContext)

Aggregations

MetadataContext (com.tencent.cloud.metadata.context.MetadataContext)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 RequestContext (com.netflix.zuul.context.RequestContext)1 QuotaResponse (com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)1 MethodMetadata (feign.MethodMetadata)1 RequestTemplate (feign.RequestTemplate)1 Route (org.springframework.cloud.gateway.route.Route)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1