diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/CustomLabelResolver.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/CustomLabelResolver.java new file mode 100644 index 000000000..eb0c0c741 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/CustomLabelResolver.java @@ -0,0 +1,58 @@ +/* + * 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.caller; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelServletResolver; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * resolver custom label from request. + * + * @author atom + */ +@Component +public class CustomLabelResolver implements PolarisRateLimiterLabelServletResolver { + + @Value("${label.key-value:}") + private String[] keyValues; + @Override + public Map resolve(HttpServletRequest request) { + // rate limit by some request params. such as query params, headers .. + + return getLabels(keyValues); + } + + static Map getLabels(String[] keyValues) { + Map labels = new HashMap<>(); + for (String kv : keyValues) { + String key = kv.substring(0, kv.indexOf(":")); + String value = kv.substring(kv.indexOf(":")); + labels.put(key, value); + } + + return labels; + } +} diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/CustomLabelResolverReactive.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/CustomLabelResolverReactive.java new file mode 100644 index 000000000..580d5e732 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/CustomLabelResolverReactive.java @@ -0,0 +1,46 @@ +/* + * 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.caller; + +import java.util.Map; + +import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelReactiveResolver; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; + +import static com.tencent.cloud.ratelimit.example.service.caller.CustomLabelResolver.getLabels; + +/** + * resolver custom label from request. + * + * @author atom + */ +@Component +public class CustomLabelResolverReactive implements PolarisRateLimiterLabelReactiveResolver { + @Value("${label.key-value:}") + private String[] keyValues; + + @Override + public Map resolve(ServerWebExchange exchange) { + // rate limit by some request params. such as query params, headers .. + + return getLabels(keyValues); + } +} diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..35cd87735 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/resources/bootstrap.yml @@ -0,0 +1,23 @@ +server: + port: 58080 +spring: + application: + name: RateLimitCallerService + cloud: + polaris: + address: grpc://183.47.111.80:8091 + namespace: default + enabled: true + +management: + endpoints: + web: + exposure: + include: + - polaris-ratelimit +logging: + level: + com.tencent.cloud.polaris: debug + +label: + key-value: user:zhangsan, user2:lisi