|
@@ -0,0 +1,95 @@
|
|
|
|
|
+package space.core.apps.filter;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.Filter;
|
|
|
|
|
+import javax.servlet.FilterChain;
|
|
|
|
|
+import javax.servlet.FilterConfig;
|
|
|
|
|
+import javax.servlet.ServletException;
|
|
|
|
|
+import javax.servlet.ServletRequest;
|
|
|
|
|
+import javax.servlet.ServletResponse;
|
|
|
|
|
+
|
|
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
|
|
+
|
|
|
|
|
+import space.core.apps.common.CommonVars;
|
|
|
|
|
+import space.core.apps.jedis.JedisUtil;
|
|
|
|
|
+import space.core.apps.listener.ModuleInitListener;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 通过共享 Redis 版本号刷新双节点 JVM 内的网站配置缓存。
|
|
|
|
|
+ */
|
|
|
|
|
+public class WebsiteConfigRefreshFilter implements Filter {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger log = LogManager.getLogger(WebsiteConfigRefreshFilter.class);
|
|
|
|
|
+ private static final String REVISION_KEY = "ZQ_WEBSITE_CONFIG_REVISION";
|
|
|
|
|
+ private static final long CHECK_INTERVAL_MILLIS = 2000L;
|
|
|
|
|
+ private static final Object REFRESH_LOCK = new Object();
|
|
|
|
|
+
|
|
|
|
|
+ private static volatile String currentRevision;
|
|
|
|
|
+ private static volatile long lastCheckedAt;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void init(FilterConfig filterConfig) throws ServletException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ currentRevision = JedisUtil.str.get(REVISION_KEY);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("读取网站配置版本号失败,将在后续请求中重试", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
|
|
|
|
+ throws IOException, ServletException {
|
|
|
|
|
+ refreshIfChanged();
|
|
|
|
|
+ chain.doFilter(request, response);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 当前节点完成网站配置重载后,发布新的共享版本号。
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void publishWebsiteConfigChange() {
|
|
|
|
|
+ String revision = System.currentTimeMillis() + "-" + UUID.randomUUID().toString();
|
|
|
|
|
+ try {
|
|
|
|
|
+ JedisUtil.str.setDay30(REVISION_KEY, revision);
|
|
|
|
|
+ currentRevision = revision;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发布网站配置版本号失败,其他节点可能继续使用旧配置", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void refreshIfChanged() {
|
|
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
|
|
+ if (now - lastCheckedAt < CHECK_INTERVAL_MILLIS) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ synchronized (REFRESH_LOCK) {
|
|
|
|
|
+ now = System.currentTimeMillis();
|
|
|
|
|
+ if (now - lastCheckedAt < CHECK_INTERVAL_MILLIS) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ lastCheckedAt = now;
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ String revision = JedisUtil.str.get(REVISION_KEY);
|
|
|
|
|
+ if (revision == null || revision.equals(currentRevision)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ new ModuleInitListener().initWebsiteAll();
|
|
|
|
|
+ currentRevision = revision;
|
|
|
|
|
+ log.info("检测到网站配置版本更新,已重载网站配置,网站数={}",
|
|
|
|
|
+ CommonVars.websiteConfig == null || CommonVars.websiteConfig.getWebsiteMap() == null ? 0
|
|
|
|
|
+ : CommonVars.websiteConfig.getWebsiteMap().size());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("重载网站配置失败,将在后续请求中重试", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void destroy() {
|
|
|
|
|
+ }
|
|
|
|
|
+}
|