|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
/*
|
|
|
|
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
|
|
|
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2021 Tencent. All rights reserved.
|
|
|
|
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the BSD 3-Clause License (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
@ -13,27 +13,32 @@
|
|
|
|
|
* 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.metadata.core;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import com.tencent.cloud.common.constant.OrderConstant;
|
|
|
|
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
|
|
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
|
|
|
import com.tencent.cloud.common.tsf.TsfContextUtils;
|
|
|
|
|
import com.tencent.cloud.common.util.JacksonUtils;
|
|
|
|
|
import com.tencent.cloud.common.util.TsfTagUtils;
|
|
|
|
|
import com.tencent.cloud.common.util.UrlUtils;
|
|
|
|
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
|
|
|
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
|
|
|
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
|
|
|
|
import com.tencent.cloud.rpc.enhancement.plugin.PluginOrderConstant;
|
|
|
|
|
import com.tencent.polaris.metadata.core.MessageMetadataContainer;
|
|
|
|
|
import com.tencent.polaris.metadata.core.MetadataType;
|
|
|
|
|
import shade.polaris.com.google.common.collect.ImmutableMap;
|
|
|
|
|
|
|
|
|
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
|
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
|
import org.springframework.http.HttpRequest;
|
|
|
|
|
import org.springframework.http.client.ClientHttpRequestExecution;
|
|
|
|
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
|
|
|
import org.springframework.http.client.ClientHttpResponse;
|
|
|
|
|
import org.springframework.lang.NonNull;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.APPLICATION_METADATA;
|
|
|
|
@ -41,31 +46,33 @@ import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUST
|
|
|
|
|
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pre EnhancedPlugin for rest template to encode transfer metadata.
|
|
|
|
|
* Interceptor used for adding the metadata in http headers from context when web client
|
|
|
|
|
* is RestTemplate.
|
|
|
|
|
*
|
|
|
|
|
* It needs to execute after {@link LoadBalancerInterceptor}, because LaneRouter may add calleeTransitiveHeaders.
|
|
|
|
|
*
|
|
|
|
|
* @author Shedfree Wu
|
|
|
|
|
* @author Haotian Zhang
|
|
|
|
|
*/
|
|
|
|
|
public class EncodeTransferMedataRestTemplateEnhancedPlugin implements EnhancedPlugin {
|
|
|
|
|
public class EncodeTransferMedataRestTemplateInterceptor implements ClientHttpRequestInterceptor, Ordered {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public EnhancedPluginType getType() {
|
|
|
|
|
return EnhancedPluginType.Client.PRE;
|
|
|
|
|
public int getOrder() {
|
|
|
|
|
return OrderConstant.Client.RestTemplate.ENCODE_TRANSFER_METADATA_INTERCEPTOR_ORDER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run(EnhancedPluginContext context) throws Throwable {
|
|
|
|
|
if (!(context.getOriginRequest() instanceof HttpRequest)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
HttpRequest httpRequest = (HttpRequest) context.getOriginRequest();
|
|
|
|
|
|
|
|
|
|
public ClientHttpResponse intercept(@NonNull HttpRequest httpRequest, @NonNull byte[] bytes,
|
|
|
|
|
@NonNull ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
|
|
|
|
|
// get metadata of current thread
|
|
|
|
|
MetadataContext metadataContext = MetadataContextHolder.get();
|
|
|
|
|
Map<String, String> customMetadata = metadataContext.getCustomMetadata();
|
|
|
|
|
Map<String, String> disposableMetadata = metadataContext.getDisposableMetadata();
|
|
|
|
|
Map<String, String> applicationMetadata = metadataContext.getApplicationMetadata();
|
|
|
|
|
Map<String, String> transHeaders = metadataContext.getTransHeadersKV();
|
|
|
|
|
|
|
|
|
|
MessageMetadataContainer calleeMessageMetadataContainer = metadataContext.getMetadataContainer(MetadataType.MESSAGE, false);
|
|
|
|
|
Map<String, String> calleeTransitiveHeaders = calleeMessageMetadataContainer.getTransitiveHeaders();
|
|
|
|
|
|
|
|
|
|
if (TsfContextUtils.isOnlyTsfConsulEnabled()) {
|
|
|
|
|
Map<String, String> tsfMetadataMap = TsfTagUtils.getTsfMetadataMap(calleeTransitiveHeaders, disposableMetadata, customMetadata, applicationMetadata);
|
|
|
|
|
this.buildHeaderMap(httpRequest, tsfMetadataMap);
|
|
|
|
@ -86,7 +93,7 @@ public class EncodeTransferMedataRestTemplateEnhancedPlugin implements EnhancedP
|
|
|
|
|
// set headers that need to be transmitted from the upstream
|
|
|
|
|
this.buildTransmittedHeader(httpRequest, transHeaders);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return clientHttpRequestExecution.execute(httpRequest, bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buildTransmittedHeader(HttpRequest request, Map<String, String> transHeaders) {
|
|
|
|
@ -115,9 +122,4 @@ public class EncodeTransferMedataRestTemplateEnhancedPlugin implements EnhancedP
|
|
|
|
|
buildHeaderMap(request, ImmutableMap.of(headerName, JacksonUtils.serialize2Json(metadata)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getOrder() {
|
|
|
|
|
return PluginOrderConstant.ClientPluginOrder.CONSUMER_TRANSFER_METADATA_PLUGIN_ORDER;
|
|
|
|
|
}
|
|
|
|
|
}
|