|
@@ -1,6 +1,5 @@
|
|
package cn.com.qmth.examcloud.web.interceptor;
|
|
package cn.com.qmth.examcloud.web.interceptor;
|
|
|
|
|
|
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
|
|
|
|
import cn.com.qmth.examcloud.commons.util.Util;
|
|
import cn.com.qmth.examcloud.commons.util.Util;
|
|
import cn.com.qmth.examcloud.web.actuator.ApiStatusInfo;
|
|
import cn.com.qmth.examcloud.web.actuator.ApiStatusInfo;
|
|
import cn.com.qmth.examcloud.web.actuator.ApiStatusInfoHolder;
|
|
import cn.com.qmth.examcloud.web.actuator.ApiStatusInfoHolder;
|
|
@@ -21,6 +20,10 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.InputStreamReader;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
import java.util.Map.Entry;
|
|
import java.util.Properties;
|
|
import java.util.Properties;
|
|
@@ -55,7 +58,6 @@ public class ApiFlowLimitedInterceptor implements HandlerInterceptor {
|
|
}
|
|
}
|
|
|
|
|
|
private static void init() {
|
|
private static void init() {
|
|
-
|
|
|
|
int permitsPerSecond = PropertyHolder.getInt("examcloud.api.permitsPerSecond", 100000);
|
|
int permitsPerSecond = PropertyHolder.getInt("examcloud.api.permitsPerSecond", 100000);
|
|
rateLimiter = RateLimiter.create(permitsPerSecond);
|
|
rateLimiter = RateLimiter.create(permitsPerSecond);
|
|
|
|
|
|
@@ -63,7 +65,7 @@ public class ApiFlowLimitedInterceptor implements HandlerInterceptor {
|
|
allowedRate = PropertyHolder.getInt("examcloud.api.flowLimited.allowedRate", 5);
|
|
allowedRate = PropertyHolder.getInt("examcloud.api.flowLimited.allowedRate", 5);
|
|
minCallRate = PropertyHolder.getInt("examcloud.api.flowLimited.minCallRate", 10);
|
|
minCallRate = PropertyHolder.getInt("examcloud.api.flowLimited.minCallRate", 10);
|
|
|
|
|
|
- refreshConfig();
|
|
|
|
|
|
+ loadProperties();
|
|
|
|
|
|
for (Entry<Object, Object> entry : PROPS.entrySet()) {
|
|
for (Entry<Object, Object> entry : PROPS.entrySet()) {
|
|
String key = (String) entry.getKey();
|
|
String key = (String) entry.getKey();
|
|
@@ -99,7 +101,10 @@ public class ApiFlowLimitedInterceptor implements HandlerInterceptor {
|
|
public void run() {
|
|
public void run() {
|
|
|
|
|
|
while (true) {
|
|
while (true) {
|
|
- refreshConfig();
|
|
|
|
|
|
+ // 3分钟
|
|
|
|
+ Util.sleep(180);
|
|
|
|
+
|
|
|
|
+ loadProperties();
|
|
|
|
|
|
Map<String, Integer> updated = Maps.newHashMap();
|
|
Map<String, Integer> updated = Maps.newHashMap();
|
|
|
|
|
|
@@ -138,36 +143,39 @@ public class ApiFlowLimitedInterceptor implements HandlerInterceptor {
|
|
}
|
|
}
|
|
|
|
|
|
permitsPerSecondMap.putAll(updated);
|
|
permitsPerSecondMap.putAll(updated);
|
|
-
|
|
|
|
- Util.sleep(10);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
}).start();
|
|
}).start();
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private static void refreshConfig() {
|
|
|
|
- try {
|
|
|
|
- Properties newProps = new Properties();
|
|
|
|
-
|
|
|
|
- Properties nextProps = new Properties();
|
|
|
|
|
|
+ private synchronized static void loadProperties() {
|
|
|
|
+ final String propertyPath = "limited.properties";
|
|
|
|
|
|
- PropertiesUtil.loadFromResource("limited.properties", newProps);
|
|
|
|
- for (Entry<Object, Object> entry : newProps.entrySet()) {
|
|
|
|
- String key = (String) entry.getKey();
|
|
|
|
- String value = (String) entry.getValue();
|
|
|
|
- if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
|
|
+ InputStream is = ApiFlowLimitedInterceptor.class.getClassLoader().getResourceAsStream(propertyPath);
|
|
|
|
+ if (is == null) {
|
|
|
|
+ LOG.warn("{} is not exist!", propertyPath);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- nextProps.put(key.trim(), value.trim());
|
|
|
|
- }
|
|
|
|
|
|
+ try (InputStreamReader isr = new InputStreamReader(is, "UTF-8");
|
|
|
|
+ BufferedReader br = new BufferedReader(isr);) {
|
|
|
|
|
|
- PROPS = nextProps;
|
|
|
|
|
|
+ Properties curProperties = new Properties();
|
|
|
|
+ curProperties.load(br);
|
|
|
|
|
|
|
|
+ PROPS.clear();
|
|
|
|
+ if (curProperties.size() > 0) {
|
|
|
|
+ PROPS.putAll(curProperties);
|
|
|
|
+ LOG.debug("Load config size is {}", PROPS.size());
|
|
|
|
+ }
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- LOG.error("fail to refresh API config.", e);
|
|
|
|
|
|
+ LOG.error("Load config fail! " + e.getMessage(), e);
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ is.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ // ignore...
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|