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());
}
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);
}
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;
}
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);
}
}
}
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);
}
Aggregations