feature: add ratelimit provider info and refactor ratelimit use arguments (#904)
parent
776a646e81
commit
48dc5962da
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* 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 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
|
||||||
|
ClientRequest.Builder requestBuilder = ClientRequest.from(clientRequest);
|
||||||
|
|
||||||
|
this.buildMetadataHeader(requestBuilder, customMetadata, CUSTOM_METADATA);
|
||||||
|
this.buildMetadataHeader(requestBuilder, disposableMetadata, CUSTOM_DISPOSABLE_METADATA);
|
||||||
|
this.buildTransmittedHeader(requestBuilder, transHeaders);
|
||||||
|
|
||||||
|
ClientRequest request = requestBuilder.build();
|
||||||
|
|
||||||
|
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,84 @@
|
|||||||
|
/*
|
||||||
|
* 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 com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link EncodeTransferMedataWebClientFilter}.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(webEnvironment = RANDOM_PORT,
|
||||||
|
classes = EncodeTransferMedataWebClientFilterTest.TestApplication.class,
|
||||||
|
properties = {"spring.config.location = classpath:application-test.yml"})
|
||||||
|
public class EncodeTransferMedataWebClientFilterTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebClient.Builder webClientBuilder;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTransitiveMetadataFromApplicationConfig() {
|
||||||
|
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||||
|
metadataContext.setTransHeadersKV("xxx", "xxx");
|
||||||
|
String metadata = webClientBuilder.baseUrl("http://localhost:" + localServerPort).build()
|
||||||
|
.get()
|
||||||
|
.uri("/test")
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class)
|
||||||
|
.block();
|
||||||
|
assertThat(metadata).isEqualTo("2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int localServerPort;
|
||||||
|
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@RestController
|
||||||
|
protected static class TestApplication {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public WebClient.Builder webClientBuilder() {
|
||||||
|
return WebClient.builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/test")
|
||||||
|
public String test() {
|
||||||
|
return MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_TRANSITIVE, "b");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* 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.polaris.ratelimit.resolver;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.polaris.context.ServiceRuleManager;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelReactiveResolver;
|
||||||
|
import com.tencent.polaris.ratelimit.api.rpc.Argument;
|
||||||
|
import com.tencent.polaris.specification.api.v1.traffic.manage.RateLimitProto;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAME;
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolve arguments from rate limit rule for Reactive.
|
||||||
|
*
|
||||||
|
* @author seansyyu 2023-03-09
|
||||||
|
*/
|
||||||
|
public class RateLimitRuleArgumentReactiveResolver {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(RateLimitRuleArgumentReactiveResolver.class);
|
||||||
|
|
||||||
|
private final ServiceRuleManager serviceRuleManager;
|
||||||
|
|
||||||
|
private final PolarisRateLimiterLabelReactiveResolver labelResolver;
|
||||||
|
|
||||||
|
public RateLimitRuleArgumentReactiveResolver(ServiceRuleManager serviceRuleManager, PolarisRateLimiterLabelReactiveResolver labelResolver) {
|
||||||
|
this.serviceRuleManager = serviceRuleManager;
|
||||||
|
this.labelResolver = labelResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Argument> getArguments(ServerWebExchange request, String namespace, String service) {
|
||||||
|
RateLimitProto.RateLimit rateLimitRule = serviceRuleManager.getServiceRateLimitRule(namespace, service);
|
||||||
|
if (rateLimitRule == null) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
List<RateLimitProto.Rule> rules = rateLimitRule.getRulesList();
|
||||||
|
if (CollectionUtils.isEmpty(rules)) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
return rules.stream()
|
||||||
|
.flatMap(rule -> rule.getArgumentsList().stream())
|
||||||
|
.map(matchArgument -> {
|
||||||
|
String matchKey = matchArgument.getKey();
|
||||||
|
Argument argument = null;
|
||||||
|
switch (matchArgument.getType()) {
|
||||||
|
case CUSTOM:
|
||||||
|
argument = StringUtils.isBlank(matchKey) ? null :
|
||||||
|
Argument.buildCustom(matchKey, Optional.ofNullable(getCustomResolvedLabels(request).get(matchKey)).orElse(StringUtils.EMPTY));
|
||||||
|
break;
|
||||||
|
case METHOD:
|
||||||
|
argument = Argument.buildMethod(request.getRequest().getMethodValue());
|
||||||
|
break;
|
||||||
|
case HEADER:
|
||||||
|
argument = StringUtils.isBlank(matchKey) ? null :
|
||||||
|
Argument.buildHeader(matchKey, Optional.ofNullable(request.getRequest().getHeaders().getFirst(matchKey)).orElse(StringUtils.EMPTY));
|
||||||
|
break;
|
||||||
|
case QUERY:
|
||||||
|
argument = StringUtils.isBlank(matchKey) ? null :
|
||||||
|
Argument.buildQuery(matchKey, Optional.ofNullable(request.getRequest().getQueryParams().getFirst(matchKey)).orElse(StringUtils.EMPTY));
|
||||||
|
break;
|
||||||
|
case CALLER_SERVICE:
|
||||||
|
String sourceServiceNamespace = MetadataContextHolder.getDisposableMetadata(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, true).orElse(StringUtils.EMPTY);
|
||||||
|
String sourceServiceName = MetadataContextHolder.getDisposableMetadata(DEFAULT_METADATA_SOURCE_SERVICE_NAME, true).orElse(StringUtils.EMPTY);
|
||||||
|
if (!StringUtils.isEmpty(sourceServiceNamespace) && !StringUtils.isEmpty(sourceServiceName)) {
|
||||||
|
argument = Argument.buildCallerService(sourceServiceNamespace, sourceServiceName);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CALLER_IP:
|
||||||
|
InetSocketAddress remoteAddress = request.getRequest().getRemoteAddress();
|
||||||
|
argument = Argument.buildCallerIP(remoteAddress != null ? remoteAddress.getAddress().getHostAddress() : StringUtils.EMPTY);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return argument;
|
||||||
|
}).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> getCustomResolvedLabels(ServerWebExchange request) {
|
||||||
|
if (labelResolver != null) {
|
||||||
|
try {
|
||||||
|
return labelResolver.resolve(request);
|
||||||
|
}
|
||||||
|
catch (Throwable e) {
|
||||||
|
LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* 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.polaris.ratelimit.resolver;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.polaris.context.ServiceRuleManager;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckServletFilter;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelServletResolver;
|
||||||
|
import com.tencent.polaris.ratelimit.api.rpc.Argument;
|
||||||
|
import com.tencent.polaris.specification.api.v1.traffic.manage.RateLimitProto;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAME;
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolve arguments from rate limit rule for Servlet.
|
||||||
|
*
|
||||||
|
* @author seansyyu 2023-03-09
|
||||||
|
*/
|
||||||
|
public class RateLimitRuleArgumentServletResolver {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckServletFilter.class);
|
||||||
|
|
||||||
|
private final ServiceRuleManager serviceRuleManager;
|
||||||
|
|
||||||
|
private final PolarisRateLimiterLabelServletResolver labelResolver;
|
||||||
|
|
||||||
|
public RateLimitRuleArgumentServletResolver(ServiceRuleManager serviceRuleManager, PolarisRateLimiterLabelServletResolver labelResolver) {
|
||||||
|
this.serviceRuleManager = serviceRuleManager;
|
||||||
|
this.labelResolver = labelResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Argument> getArguments(HttpServletRequest request, String namespace, String service) {
|
||||||
|
RateLimitProto.RateLimit rateLimitRule = serviceRuleManager.getServiceRateLimitRule(namespace, service);
|
||||||
|
if (rateLimitRule == null) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
List<RateLimitProto.Rule> rules = rateLimitRule.getRulesList();
|
||||||
|
if (CollectionUtils.isEmpty(rules)) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
return rules.stream()
|
||||||
|
.flatMap(rule -> rule.getArgumentsList().stream())
|
||||||
|
.map(matchArgument -> {
|
||||||
|
String matchKey = matchArgument.getKey();
|
||||||
|
Argument argument = null;
|
||||||
|
switch (matchArgument.getType()) {
|
||||||
|
case CUSTOM:
|
||||||
|
argument = StringUtils.isBlank(matchKey) ? null :
|
||||||
|
Argument.buildCustom(matchKey, Optional.ofNullable(getCustomResolvedLabels(request).get(matchKey)).orElse(StringUtils.EMPTY));
|
||||||
|
break;
|
||||||
|
case METHOD:
|
||||||
|
argument = Argument.buildMethod(request.getMethod());
|
||||||
|
break;
|
||||||
|
case HEADER:
|
||||||
|
argument = StringUtils.isBlank(matchKey) ? null :
|
||||||
|
Argument.buildHeader(matchKey, Optional.ofNullable(request.getHeader(matchKey)).orElse(StringUtils.EMPTY));
|
||||||
|
break;
|
||||||
|
case QUERY:
|
||||||
|
argument = StringUtils.isBlank(matchKey) ? null :
|
||||||
|
Argument.buildQuery(matchKey, Optional.ofNullable(request.getParameter(matchKey)).orElse(StringUtils.EMPTY));
|
||||||
|
break;
|
||||||
|
case CALLER_SERVICE:
|
||||||
|
String sourceServiceNamespace = MetadataContextHolder.getDisposableMetadata(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, true).orElse(StringUtils.EMPTY);
|
||||||
|
String sourceServiceName = MetadataContextHolder.getDisposableMetadata(DEFAULT_METADATA_SOURCE_SERVICE_NAME, true).orElse(StringUtils.EMPTY);
|
||||||
|
if (!StringUtils.isEmpty(sourceServiceNamespace) && !StringUtils.isEmpty(sourceServiceName)) {
|
||||||
|
argument = Argument.buildCallerService(sourceServiceNamespace, sourceServiceName);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CALLER_IP:
|
||||||
|
argument = Argument.buildCallerIP(Optional.ofNullable(request.getRemoteAddr()).orElse(StringUtils.EMPTY));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return argument;
|
||||||
|
}).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> getCustomResolvedLabels(HttpServletRequest request) {
|
||||||
|
if (labelResolver != null) {
|
||||||
|
try {
|
||||||
|
return labelResolver.resolve(request);
|
||||||
|
}
|
||||||
|
catch (Throwable e) {
|
||||||
|
LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* 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.polaris.ratelimit.resolver;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
import com.google.protobuf.util.JsonFormat;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.polaris.context.ServiceRuleManager;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckServletFilterTest;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelReactiveResolver;
|
||||||
|
import com.tencent.polaris.ratelimit.api.rpc.Argument;
|
||||||
|
import com.tencent.polaris.specification.api.v1.traffic.manage.RateLimitProto;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||||
|
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAME;
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||||
|
classes = RateLimitRuleArgumentReactiveResolverTest.TestApplication.class,
|
||||||
|
properties = {
|
||||||
|
"spring.cloud.polaris.namespace=Test", "spring.cloud.polaris.service=TestApp"
|
||||||
|
})
|
||||||
|
public class RateLimitRuleArgumentReactiveResolverTest {
|
||||||
|
|
||||||
|
private final PolarisRateLimiterLabelReactiveResolver labelResolver =
|
||||||
|
exchange -> Collections.singletonMap("xxx", "xxx");
|
||||||
|
|
||||||
|
private final PolarisRateLimiterLabelReactiveResolver labelResolverEx =
|
||||||
|
exchange -> {
|
||||||
|
throw new RuntimeException();
|
||||||
|
};
|
||||||
|
|
||||||
|
private RateLimitRuleArgumentReactiveResolver rateLimitRuleArgumentReactiveResolver1;
|
||||||
|
private RateLimitRuleArgumentReactiveResolver rateLimitRuleArgumentReactiveResolver2;
|
||||||
|
private RateLimitRuleArgumentReactiveResolver rateLimitRuleArgumentReactiveResolver3;
|
||||||
|
private RateLimitRuleArgumentReactiveResolver rateLimitRuleArgumentReactiveResolver4;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() throws InvalidProtocolBufferException {
|
||||||
|
MetadataContext.LOCAL_NAMESPACE = "TEST";
|
||||||
|
|
||||||
|
ServiceRuleManager serviceRuleManager = mock(ServiceRuleManager.class);
|
||||||
|
|
||||||
|
RateLimitProto.Rule.Builder ratelimitRuleBuilder = RateLimitProto.Rule.newBuilder();
|
||||||
|
InputStream inputStream = QuotaCheckServletFilterTest.class.getClassLoader().getResourceAsStream("ratelimit.json");
|
||||||
|
String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining(""));
|
||||||
|
JsonFormat.parser().ignoringUnknownFields().merge(json, ratelimitRuleBuilder);
|
||||||
|
RateLimitProto.Rule rateLimitRule = ratelimitRuleBuilder.build();
|
||||||
|
RateLimitProto.RateLimit rateLimit = RateLimitProto.RateLimit.newBuilder().addRules(rateLimitRule).build();
|
||||||
|
when(serviceRuleManager.getServiceRateLimitRule(anyString(), anyString())).thenReturn(rateLimit);
|
||||||
|
|
||||||
|
// normal
|
||||||
|
this.rateLimitRuleArgumentReactiveResolver1 = new RateLimitRuleArgumentReactiveResolver(serviceRuleManager, labelResolver);
|
||||||
|
// ex
|
||||||
|
this.rateLimitRuleArgumentReactiveResolver2 = new RateLimitRuleArgumentReactiveResolver(serviceRuleManager, labelResolverEx);
|
||||||
|
// null
|
||||||
|
ServiceRuleManager serviceRuleManager1 = mock(ServiceRuleManager.class);
|
||||||
|
when(serviceRuleManager1.getServiceRateLimitRule(anyString(), anyString())).thenReturn(null);
|
||||||
|
this.rateLimitRuleArgumentReactiveResolver3 = new RateLimitRuleArgumentReactiveResolver(serviceRuleManager1, labelResolver);
|
||||||
|
// null 2
|
||||||
|
ServiceRuleManager serviceRuleManager2 = mock(ServiceRuleManager.class);
|
||||||
|
RateLimitProto.RateLimit rateLimit2 = RateLimitProto.RateLimit.newBuilder().build();
|
||||||
|
when(serviceRuleManager2.getServiceRateLimitRule(anyString(), anyString())).thenReturn(rateLimit2);
|
||||||
|
this.rateLimitRuleArgumentReactiveResolver4 = new RateLimitRuleArgumentReactiveResolver(serviceRuleManager2, labelResolver);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRuleArguments() {
|
||||||
|
// Mock request
|
||||||
|
MetadataContext.LOCAL_SERVICE = "Test";
|
||||||
|
// Mock request
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.get("http://127.0.0.1:8080/test")
|
||||||
|
.remoteAddress(new InetSocketAddress("127.0.0.1", 8080))
|
||||||
|
.header("xxx", "xxx")
|
||||||
|
.queryParam("yyy", "yyy")
|
||||||
|
.build();
|
||||||
|
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||||
|
MetadataContext metadataContext = new MetadataContext();
|
||||||
|
metadataContext.setUpstreamDisposableMetadata(new HashMap<String, String>() {{
|
||||||
|
put(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
|
||||||
|
put(DEFAULT_METADATA_SOURCE_SERVICE_NAME, MetadataContext.LOCAL_SERVICE);
|
||||||
|
}});
|
||||||
|
MetadataContextHolder.set(metadataContext);
|
||||||
|
Set<Argument> arguments = rateLimitRuleArgumentReactiveResolver1.getArguments(exchange, MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
||||||
|
Set<Argument> exceptRes = new HashSet<>();
|
||||||
|
exceptRes.add(Argument.buildMethod("GET"));
|
||||||
|
exceptRes.add(Argument.buildHeader("xxx", "xxx"));
|
||||||
|
exceptRes.add(Argument.buildQuery("yyy", "yyy"));
|
||||||
|
exceptRes.add(Argument.buildCallerIP("127.0.0.1"));
|
||||||
|
exceptRes.add(Argument.buildCustom("xxx", "xxx"));
|
||||||
|
exceptRes.add(Argument.buildCallerService(MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE));
|
||||||
|
assertThat(arguments).isEqualTo(exceptRes);
|
||||||
|
|
||||||
|
rateLimitRuleArgumentReactiveResolver2.getArguments(exchange, MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
||||||
|
rateLimitRuleArgumentReactiveResolver3.getArguments(exchange, MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
||||||
|
rateLimitRuleArgumentReactiveResolver4.getArguments(exchange, MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
protected static class TestApplication {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* 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.polaris.ratelimit.resolver;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
import com.google.protobuf.util.JsonFormat;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.polaris.context.ServiceRuleManager;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckServletFilterTest;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelServletResolver;
|
||||||
|
import com.tencent.polaris.ratelimit.api.rpc.Argument;
|
||||||
|
import com.tencent.polaris.specification.api.v1.traffic.manage.RateLimitProto;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAME;
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||||
|
classes = RateLimitRuleArgumentServletResolverTest.TestApplication.class,
|
||||||
|
properties = {
|
||||||
|
"spring.cloud.polaris.namespace=Test", "spring.cloud.polaris.service=TestApp"
|
||||||
|
})
|
||||||
|
public class RateLimitRuleArgumentServletResolverTest {
|
||||||
|
|
||||||
|
private final PolarisRateLimiterLabelServletResolver labelResolver =
|
||||||
|
exchange -> Collections.singletonMap("xxx", "xxx");
|
||||||
|
private final PolarisRateLimiterLabelServletResolver labelResolverEx =
|
||||||
|
exchange -> {
|
||||||
|
throw new RuntimeException();
|
||||||
|
};
|
||||||
|
|
||||||
|
private RateLimitRuleArgumentServletResolver rateLimitRuleArgumentServletResolver1;
|
||||||
|
private RateLimitRuleArgumentServletResolver rateLimitRuleArgumentServletResolver2;
|
||||||
|
private RateLimitRuleArgumentServletResolver rateLimitRuleArgumentServletResolver3;
|
||||||
|
private RateLimitRuleArgumentServletResolver rateLimitRuleArgumentServletResolver4;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() throws InvalidProtocolBufferException {
|
||||||
|
MetadataContext.LOCAL_NAMESPACE = "TEST";
|
||||||
|
|
||||||
|
ServiceRuleManager serviceRuleManager = mock(ServiceRuleManager.class);
|
||||||
|
|
||||||
|
RateLimitProto.Rule.Builder ratelimitRuleBuilder = RateLimitProto.Rule.newBuilder();
|
||||||
|
InputStream inputStream = QuotaCheckServletFilterTest.class.getClassLoader().getResourceAsStream("ratelimit.json");
|
||||||
|
String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining(""));
|
||||||
|
JsonFormat.parser().ignoringUnknownFields().merge(json, ratelimitRuleBuilder);
|
||||||
|
RateLimitProto.Rule rateLimitRule = ratelimitRuleBuilder.build();
|
||||||
|
RateLimitProto.RateLimit rateLimit = RateLimitProto.RateLimit.newBuilder().addRules(rateLimitRule).build();
|
||||||
|
when(serviceRuleManager.getServiceRateLimitRule(anyString(), anyString())).thenReturn(rateLimit);
|
||||||
|
|
||||||
|
// normal
|
||||||
|
this.rateLimitRuleArgumentServletResolver1 = new RateLimitRuleArgumentServletResolver(serviceRuleManager, labelResolver);
|
||||||
|
// ex
|
||||||
|
this.rateLimitRuleArgumentServletResolver2 = new RateLimitRuleArgumentServletResolver(serviceRuleManager, labelResolverEx);
|
||||||
|
// null
|
||||||
|
ServiceRuleManager serviceRuleManager1 = mock(ServiceRuleManager.class);
|
||||||
|
when(serviceRuleManager1.getServiceRateLimitRule(anyString(), anyString())).thenReturn(null);
|
||||||
|
this.rateLimitRuleArgumentServletResolver3 = new RateLimitRuleArgumentServletResolver(serviceRuleManager1, labelResolver);
|
||||||
|
// null 2
|
||||||
|
ServiceRuleManager serviceRuleManager2 = mock(ServiceRuleManager.class);
|
||||||
|
RateLimitProto.RateLimit rateLimit2 = RateLimitProto.RateLimit.newBuilder().build();
|
||||||
|
when(serviceRuleManager2.getServiceRateLimitRule(anyString(), anyString())).thenReturn(rateLimit2);
|
||||||
|
this.rateLimitRuleArgumentServletResolver4 = new RateLimitRuleArgumentServletResolver(serviceRuleManager2, labelResolver);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRuleArguments() {
|
||||||
|
// Mock request
|
||||||
|
MetadataContext.LOCAL_SERVICE = "Test";
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest(null, "GET", "/xxx");
|
||||||
|
request.setParameter("yyy", "yyy");
|
||||||
|
request.addHeader("xxx", "xxx");
|
||||||
|
MetadataContext metadataContext = new MetadataContext();
|
||||||
|
metadataContext.setUpstreamDisposableMetadata(new HashMap<String, String>() {{
|
||||||
|
put(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
|
||||||
|
put(DEFAULT_METADATA_SOURCE_SERVICE_NAME, MetadataContext.LOCAL_SERVICE);
|
||||||
|
}});
|
||||||
|
MetadataContextHolder.set(metadataContext);
|
||||||
|
Set<Argument> arguments = rateLimitRuleArgumentServletResolver1.getArguments(request, MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
||||||
|
Set<Argument> exceptRes = new HashSet<>();
|
||||||
|
exceptRes.add(Argument.buildMethod("GET"));
|
||||||
|
exceptRes.add(Argument.buildHeader("xxx", "xxx"));
|
||||||
|
exceptRes.add(Argument.buildQuery("yyy", "yyy"));
|
||||||
|
exceptRes.add(Argument.buildCallerIP("127.0.0.1"));
|
||||||
|
exceptRes.add(Argument.buildCustom("xxx", "xxx"));
|
||||||
|
exceptRes.add(Argument.buildCallerService(MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE));
|
||||||
|
assertThat(arguments).isEqualTo(exceptRes);
|
||||||
|
|
||||||
|
rateLimitRuleArgumentServletResolver2.getArguments(request, MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
||||||
|
rateLimitRuleArgumentServletResolver3.getArguments(request, MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
||||||
|
rateLimitRuleArgumentServletResolver4.getArguments(request, MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
protected static class TestApplication {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
{
|
||||||
|
"id": "f7560cce829e4d4c8556a6be63539af5",
|
||||||
|
"service": "xxx",
|
||||||
|
"namespace": "default",
|
||||||
|
"subset": {},
|
||||||
|
"priority": 0,
|
||||||
|
"resource": "QPS",
|
||||||
|
"type": "GLOBAL",
|
||||||
|
"labels": {},
|
||||||
|
"amounts": [
|
||||||
|
{
|
||||||
|
"maxAmount": 1,
|
||||||
|
"validDuration": "1s",
|
||||||
|
"precision": null,
|
||||||
|
"startAmount": null,
|
||||||
|
"minAmount": null
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"action": "REJECT",
|
||||||
|
"disable": false,
|
||||||
|
"report": null,
|
||||||
|
"ctime": "2022-12-11 21:56:59",
|
||||||
|
"mtime": "2023-03-10 15:40:33",
|
||||||
|
"revision": "6eec6f416bee40ecbf664c93add61358",
|
||||||
|
"service_token": null,
|
||||||
|
"adjuster": null,
|
||||||
|
"regex_combine": true,
|
||||||
|
"amountMode": "GLOBAL_TOTAL",
|
||||||
|
"failover": "FAILOVER_LOCAL",
|
||||||
|
"cluster": null,
|
||||||
|
"method": {
|
||||||
|
"type": "EXACT",
|
||||||
|
"value": "/xxx",
|
||||||
|
"value_type": "TEXT"
|
||||||
|
},
|
||||||
|
"arguments": [
|
||||||
|
{
|
||||||
|
"type": "CALLER_SERVICE",
|
||||||
|
"key": "default",
|
||||||
|
"value": {
|
||||||
|
"type": "EXACT",
|
||||||
|
"value": "xxx",
|
||||||
|
"value_type": "TEXT"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "HEADER",
|
||||||
|
"key": "xxx",
|
||||||
|
"value": {
|
||||||
|
"type": "EXACT",
|
||||||
|
"value": "xxx",
|
||||||
|
"value_type": "TEXT"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "QUERY",
|
||||||
|
"key": "yyy",
|
||||||
|
"value": {
|
||||||
|
"type": "EXACT",
|
||||||
|
"value": "yyy",
|
||||||
|
"value_type": "TEXT"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "METHOD",
|
||||||
|
"key": "$method",
|
||||||
|
"value": {
|
||||||
|
"type": "EXACT",
|
||||||
|
"value": "GET",
|
||||||
|
"value_type": "TEXT"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "CALLER_IP",
|
||||||
|
"key": "$caller_ip",
|
||||||
|
"value": {
|
||||||
|
"type": "EXACT",
|
||||||
|
"value": "127.0.0.1",
|
||||||
|
"value_type": "TEXT"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "CUSTOM",
|
||||||
|
"key": "xxx",
|
||||||
|
"value": {
|
||||||
|
"type": "EXACT",
|
||||||
|
"value": "xxx",
|
||||||
|
"value_type": "TEXT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"name": "xxx",
|
||||||
|
"etime": "2023-03-10 15:40:33",
|
||||||
|
"max_queue_delay": 30
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.spi.impl;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.spi.InstanceMetadataProvider;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAME;
|
||||||
|
import static com.tencent.cloud.common.constant.MetadataConstant.DefaultMetadata.DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE;
|
||||||
|
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_NAMESPACE;
|
||||||
|
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_SERVICE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DefaultInstanceMetadataProvider.
|
||||||
|
* provide DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, DEFAULT_METADATA_SOURCE_SERVICE_NAME
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class DefaultInstanceMetadataProvider implements InstanceMetadataProvider {
|
||||||
|
|
||||||
|
private final ApplicationContextAwareUtils applicationContextAwareUtils;
|
||||||
|
|
||||||
|
// ensure ApplicationContextAwareUtils init before
|
||||||
|
public DefaultInstanceMetadataProvider(ApplicationContextAwareUtils applicationContextAwareUtils) {
|
||||||
|
this.applicationContextAwareUtils = applicationContextAwareUtils;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> getMetadata() {
|
||||||
|
return new HashMap<String, String>() {{
|
||||||
|
put(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, LOCAL_NAMESPACE);
|
||||||
|
put(DEFAULT_METADATA_SOURCE_SERVICE_NAME, LOCAL_SERVICE);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> getDisposableMetadataKeys() {
|
||||||
|
return new HashSet<>(Arrays.asList(DEFAULT_METADATA_SOURCE_SERVICE_NAMESPACE, DEFAULT_METADATA_SOURCE_SERVICE_NAME));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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