parent
1e1b5fb80a
commit
88912b2557
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* 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.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.common.util.JacksonUtils;
|
||||||
|
import com.tencent.cloud.metadata.util.DefaultTransferMedataUtils;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||||
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
|
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
||||||
|
import org.springframework.web.reactive.function.client.ExchangeFunction;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA;
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA;
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.DEFAULT_DISPOSABLE_METADATA;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* web client filter used for writing metadata in HTTP request header.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class EncodeTransferMedataWebClientFilter implements ExchangeFilterFunction {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<ClientResponse> filter(ClientRequest clientRequest, ExchangeFunction next) {
|
||||||
|
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||||
|
Map<String, String> customMetadata = metadataContext.getCustomMetadata();
|
||||||
|
Map<String, String> disposableMetadata = metadataContext.getDisposableMetadata();
|
||||||
|
Map<String, String> transHeaders = metadataContext.getTransHeadersKV();
|
||||||
|
Map<String, String> defaultMetadata = DefaultTransferMedataUtils.getDefaultTransferMedata();
|
||||||
|
|
||||||
|
ClientRequest.Builder requestBuilder = ClientRequest.from(clientRequest);
|
||||||
|
|
||||||
|
this.buildMetadataHeader(requestBuilder, customMetadata, CUSTOM_METADATA);
|
||||||
|
this.buildMetadataHeader(requestBuilder, disposableMetadata, CUSTOM_DISPOSABLE_METADATA);
|
||||||
|
this.buildMetadataHeader(requestBuilder, defaultMetadata, DEFAULT_DISPOSABLE_METADATA);
|
||||||
|
this.buildTransmittedHeader(requestBuilder, transHeaders);
|
||||||
|
|
||||||
|
ClientRequest request = requestBuilder.build();
|
||||||
|
|
||||||
|
TransHeadersTransfer.transfer(request);
|
||||||
|
return next.exchange(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buildTransmittedHeader(ClientRequest.Builder requestBuilder, Map<String, String> transHeaders) {
|
||||||
|
if (!CollectionUtils.isEmpty(transHeaders)) {
|
||||||
|
transHeaders.forEach(requestBuilder::header);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set metadata into the request header for {@link ClientRequest} .
|
||||||
|
* @param requestBuilder instance of {@link ClientRequest.Builder}
|
||||||
|
* @param metadata metadata map .
|
||||||
|
* @param headerName target metadata http header name .
|
||||||
|
*/
|
||||||
|
private void buildMetadataHeader(ClientRequest.Builder requestBuilder, Map<String, String> metadata, String headerName) {
|
||||||
|
if (!CollectionUtils.isEmpty(metadata)) {
|
||||||
|
String encodedMetadata = JacksonUtils.serialize2Json(metadata);
|
||||||
|
try {
|
||||||
|
requestBuilder.header(headerName, URLEncoder.encode(encodedMetadata, UTF_8));
|
||||||
|
}
|
||||||
|
catch (UnsupportedEncodingException e) {
|
||||||
|
requestBuilder.header(headerName, encodedMetadata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* 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.ratelimit.example.service.callee;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelReactiveResolver;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolver custom label from request.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class CustomLabelResolverReactive implements PolarisRateLimiterLabelReactiveResolver {
|
||||||
|
@Override
|
||||||
|
public Map<String, String> resolve(ServerWebExchange exchange) {
|
||||||
|
// rate limit by some request params. such as query params, headers ..
|
||||||
|
|
||||||
|
Map<String, String> labels = new HashMap<>();
|
||||||
|
labels.put("user", "zhangsan");
|
||||||
|
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue