parent
b75fe0d6e0
commit
4fbdd23661
@ -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<String, String> resolve(HttpServletRequest request) {
|
||||||
|
// rate limit by some request params. such as query params, headers ..
|
||||||
|
|
||||||
|
return getLabels(keyValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Map<String, String> getLabels(String[] keyValues) {
|
||||||
|
Map<String, String> 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;
|
||||||
|
}
|
||||||
|
}
|
@ -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<String, String> resolve(ServerWebExchange exchange) {
|
||||||
|
// rate limit by some request params. such as query params, headers ..
|
||||||
|
|
||||||
|
return getLabels(keyValues);
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
Loading…
Reference in new issue