parent
747b6b2b19
commit
e655ef74c2
@ -1,4 +1,5 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
---
|
---
|
||||||
|
|
||||||
|
- [Feature: Support custom rate limit reject response info](https://github.com/Tencent/spring-cloud-tencent/pull/128)
|
||||||
|
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The properties for rate limit.
|
||||||
|
*
|
||||||
|
* @author lepdou 2022-04-20
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("spring.cloud.polaris.ratelimit")
|
||||||
|
public class PolarisRateLimitProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* custom tips when reject request.
|
||||||
|
*/
|
||||||
|
private String rejectRequestTips;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* context file path.
|
||||||
|
*/
|
||||||
|
private String rejectRequestTipsFilePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* custom http code when reject request.
|
||||||
|
*/
|
||||||
|
private int rejectHttpCode = HttpStatus.TOO_MANY_REQUESTS.value();
|
||||||
|
|
||||||
|
public String getRejectRequestTips() {
|
||||||
|
return rejectRequestTips;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRejectRequestTips(String rejectRequestTips) {
|
||||||
|
this.rejectRequestTips = rejectRequestTips;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRejectRequestTipsFilePath() {
|
||||||
|
return rejectRequestTipsFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRejectRequestTipsFilePath(String rejectRequestTipsFilePath) {
|
||||||
|
this.rejectRequestTipsFilePath = rejectRequestTipsFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRejectHttpCode() {
|
||||||
|
return rejectHttpCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRejectHttpCode(int rejectHttpCode) {
|
||||||
|
this.rejectHttpCode = rejectHttpCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* 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.utils;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.util.ResourceFileUtils;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant;
|
||||||
|
import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckServletFilter;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rate limit utils.
|
||||||
|
*
|
||||||
|
* @author lepdou 2022-04-20
|
||||||
|
*/
|
||||||
|
public final class RateLimitUtils {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory
|
||||||
|
.getLogger(QuotaCheckServletFilter.class);
|
||||||
|
|
||||||
|
private RateLimitUtils() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRejectTips(
|
||||||
|
PolarisRateLimitProperties polarisRateLimitProperties) {
|
||||||
|
String tips = polarisRateLimitProperties.getRejectRequestTips();
|
||||||
|
|
||||||
|
if (!StringUtils.isEmpty(tips)) {
|
||||||
|
return tips;
|
||||||
|
}
|
||||||
|
|
||||||
|
String rejectFilePath = polarisRateLimitProperties.getRejectRequestTipsFilePath();
|
||||||
|
if (!StringUtils.isEmpty(rejectFilePath)) {
|
||||||
|
try {
|
||||||
|
tips = ResourceFileUtils.readFile(rejectFilePath);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
LOG.error("[RateLimit] Read custom reject tips file error. path = {}",
|
||||||
|
rejectFilePath, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!StringUtils.isEmpty(tips)) {
|
||||||
|
return tips;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RateLimitConstant.QUOTA_LIMITED_INFO;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read file content from classpath resource.
|
||||||
|
*
|
||||||
|
* @author lepdou 2022-04-20
|
||||||
|
*/
|
||||||
|
public final class ResourceFileUtils {
|
||||||
|
|
||||||
|
private ResourceFileUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String readFile(String path) throws IOException {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
ClassPathResource classPathResource = new ClassPathResource(path);
|
||||||
|
|
||||||
|
if (classPathResource.exists() && classPathResource.isReadable()) {
|
||||||
|
try (InputStream inputStream = classPathResource.getInputStream()) {
|
||||||
|
byte[] buffer = new byte[1024 * 10];
|
||||||
|
int len;
|
||||||
|
while ((len = inputStream.read(buffer)) != -1) {
|
||||||
|
sb.append(new String(buffer, 0, len, StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,27 +1,36 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>polaris-ratelimit-example</artifactId>
|
<artifactId>polaris-ratelimit-example</artifactId>
|
||||||
<groupId>com.tencent.cloud</groupId>
|
<groupId>com.tencent.cloud</groupId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>ratelimit-callee-service</artifactId>
|
<artifactId>ratelimit-callee-service</artifactId>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.tencent.cloud</groupId>
|
<groupId>com.tencent.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
|
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
</dependencies>
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
<h1>
|
||||||
|
<span color="red">
|
||||||
|
Custom reject content.
|
||||||
|
</span>
|
||||||
|
</h1>
|
Loading…
Reference in new issue