From 387f1688f4474f7e9b7cbbcbd371e32cdeddeb52 Mon Sep 17 00:00:00 2001 From: redkale Date: Fri, 15 Dec 2023 22:40:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/redkale/boot/AppConfig.java | 118 ++++++++++++++++-- .../java/org/redkale/boot/Application.java | 76 ++++++----- .../java/org/redkale/boot/ModuleEngine.java | 4 +- .../java/org/redkale/boot/NodeServer.java | 103 --------------- .../redkale/cluster/ClusterModuleEngine.java | 6 +- .../org/redkale/mq/MessageModuleEngine.java | 4 +- .../redkale/source/SourceModuleEngine.java | 4 +- 7 files changed, 157 insertions(+), 158 deletions(-) diff --git a/src/main/java/org/redkale/boot/AppConfig.java b/src/main/java/org/redkale/boot/AppConfig.java index 9ab6db7e9..0ba0ba67c 100644 --- a/src/main/java/org/redkale/boot/AppConfig.java +++ b/src/main/java/org/redkale/boot/AppConfig.java @@ -17,16 +17,16 @@ import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; +import java.util.Objects; import java.util.Properties; import java.util.Set; -import java.util.logging.Logger; import java.util.logging.SimpleFormatter; import static org.redkale.boot.Application.*; import org.redkale.source.DataSources; import org.redkale.util.AnyValue; +import org.redkale.util.AnyValueWriter; import org.redkale.util.RedkaleClassLoader; import org.redkale.util.RedkaleException; -import org.redkale.util.AnyValueWriter; import org.redkale.util.Utility; /** @@ -36,11 +36,6 @@ import org.redkale.util.Utility; */ class AppConfig { - //日志 - private final Logger logger = Logger.getLogger(this.getClass().getSimpleName()); - - final long startTime = System.currentTimeMillis(); - //是否用于main方法运行 final boolean singletonMode; @@ -488,4 +483,113 @@ class AppConfig { } } + static final AnyValue.MergeFunction appConfigmergeFunction = (path, key, val1, val2) -> { + if ("".equals(path)) { + if ("executor".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("cluster".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("cache".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("schedule".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("listener".equals(key)) { + if (Objects.equals(val1.getValue("value"), val2.getValue("value"))) { + return AnyValue.MergeFunction.SKIP; + } else { + return AnyValue.MergeFunction.NONE; + } + } + if ("mq".equals(key)) { + if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { + return AnyValue.MergeFunction.REPLACE; + } else { + return AnyValue.MergeFunction.NONE; + } + } + if ("group".equals(key)) { + if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { + return AnyValue.MergeFunction.REPLACE; + } else { + return AnyValue.MergeFunction.NONE; + } + } + if ("server".equals(key)) { + if (Objects.equals(val1.getValue("name", val1.getValue("protocol") + "_" + val1.getValue("port")), + val2.getValue("name", val2.getValue("protocol") + "_" + val2.getValue("port")))) { + return AnyValue.MergeFunction.REPLACE; + } else { + return AnyValue.MergeFunction.NONE; + } + } + } + if ("cachesource".equals(path)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("datasource".equals(path)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("properties".equals(path)) { + if ("property".equals(key)) { + if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { + return AnyValue.MergeFunction.REPLACE; + } else { + return AnyValue.MergeFunction.NONE; + } + } + } + if ("server".equals(path)) { + if ("ssl".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("render".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("resource-servlet".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + } + if ("server.request".equals(path)) { + if ("remoteaddr".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("rpc".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("locale".equals(key)) { + if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { + return AnyValue.MergeFunction.REPLACE; + } else { + return AnyValue.MergeFunction.NONE; + } + } + } + if ("server.response".equals(path)) { + if ("content-type".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("defcookie".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("options".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("date".equals(key)) { + return AnyValue.MergeFunction.REPLACE; + } + if ("addheader".equals(key) || "setheader".equals(key)) { + if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { + return AnyValue.MergeFunction.REPLACE; + } else { + return AnyValue.MergeFunction.NONE; + } + } + } + return AnyValue.MergeFunction.MERGE; + }; + } diff --git a/src/main/java/org/redkale/boot/Application.java b/src/main/java/org/redkale/boot/Application.java index ec3405af7..90644a07a 100644 --- a/src/main/java/org/redkale/boot/Application.java +++ b/src/main/java/org/redkale/boot/Application.java @@ -173,9 +173,6 @@ public final class Application { //配置信息,只读版Properties private final Environment environment; - //是否从/META-INF中读取配置 - private final boolean configFromCache; - //全局根ResourceFactory final ResourceFactory resourceFactory = ResourceFactory.create(); @@ -238,7 +235,6 @@ public final class Application { this.singletonMode = appConfig.singletonMode; this.compileMode = appConfig.compileMode; this.config = appConfig.config; - this.configFromCache = appConfig.configFromCache; this.envProperties.putAll(appConfig.localEnvProperties); this.environment = new Environment(this.envProperties); this.name = appConfig.name; @@ -610,45 +606,45 @@ public final class Application { } } if (propsConf != null) { - final Properties agentEnvs = new Properties(); - { //可能通过系统环境变量配置信息 - Iterator it = ServiceLoader.load(PropertiesAgentProvider.class, classLoader).iterator(); - RedkaleClassLoader.putServiceLoader(PropertiesAgentProvider.class); - List providers = new ArrayList<>(); - while (it.hasNext()) { - PropertiesAgentProvider provider = it.next(); - if (provider != null && provider.acceptsConf(propsConf)) { - RedkaleClassLoader.putReflectionPublicConstructors(provider.getClass(), provider.getClass().getName()); - providers.add(provider); - } + final Properties remoteEnvs = new Properties(); + //可能通过系统环境变量配置信息 + Iterator it = ServiceLoader.load(PropertiesAgentProvider.class, classLoader).iterator(); + RedkaleClassLoader.putServiceLoader(PropertiesAgentProvider.class); + List providers = new ArrayList<>(); + while (it.hasNext()) { + PropertiesAgentProvider provider = it.next(); + if (provider != null && provider.acceptsConf(propsConf)) { + RedkaleClassLoader.putReflectionPublicConstructors(provider.getClass(), provider.getClass().getName()); + providers.add(provider); } - for (PropertiesAgentProvider provider : InstanceProvider.sort(providers)) { - long s = System.currentTimeMillis(); - this.propertiesAgent = provider.createInstance(); - this.resourceFactory.inject(this.propertiesAgent); - if (compileMode) { - this.propertiesAgent.compile(propsConf); - } else { - Map propMap = this.propertiesAgent.init(this, propsConf); - int propCount = 0; - if (propMap != null) { - for (Map.Entry en : propMap.entrySet()) { - propCount += en.getValue().size(); - if (en.getKey().startsWith("logging")) { - if (logProps != null) { - logger.log(Level.WARNING, "skip repeat logging config properties(" + en.getKey() + ")"); - } else { - logProps = en.getValue(); - } + } + for (PropertiesAgentProvider provider : InstanceProvider.sort(providers)) { + long s = System.currentTimeMillis(); + this.propertiesAgent = provider.createInstance(); + this.resourceFactory.inject(this.propertiesAgent); + if (compileMode) { + this.propertiesAgent.compile(propsConf); + } else { + Map propMap = this.propertiesAgent.init(this, propsConf); + int propCount = 0; + if (propMap != null) { + for (Map.Entry en : propMap.entrySet()) { + propCount += en.getValue().size(); + if (en.getKey().contains("logging")) { + if (logProps != null) { + logger.log(Level.WARNING, "skip repeat logging config properties(" + en.getKey() + ")"); } else { - agentEnvs.putAll(en.getValue()); + logProps = en.getValue(); } + } else { + remoteEnvs.putAll(en.getValue()); } } - logger.info("PropertiesAgent (type = " + this.propertiesAgent.getClass().getSimpleName() + ") load " + propCount + " data in " + (System.currentTimeMillis() - s) + " ms"); } - break; + logger.info("PropertiesAgent (type = " + this.propertiesAgent.getClass().getSimpleName() + + ") load " + propCount + " data in " + (System.currentTimeMillis() - s) + " ms"); } + break; } final Properties oldEnvs = new Properties(); @@ -660,7 +656,7 @@ public final class Application { } oldEnvs.put(key, value); } - agentEnvs.forEach((k, v) -> { + remoteEnvs.forEach((k, v) -> { if (k.toString().startsWith("redkale.")) { dyncProps.put(k, v); } else { @@ -682,6 +678,8 @@ public final class Application { if (k.toString().startsWith("redkale.executor.") //节点全局唯一 || k.toString().startsWith("redkale.transport.") //节点全局唯一 || k.toString().startsWith("redkale.cluster.") //节点全局唯一 + || k.toString().startsWith("redkale.cache.") //节点全局唯一 + || k.toString().startsWith("redkale.schedule.") //节点全局唯一 || k.toString().startsWith("redkale.mq.") || k.toString().startsWith("redkale.mq[") || k.toString().startsWith("redkale.group.") @@ -708,7 +706,7 @@ public final class Application { Properties newDyncProps = new Properties(); dyncProps.forEach((k, v) -> newDyncProps.put(k.toString(), getPropertyValue(v.toString(), dyncProps))); //合并配置 - this.config.merge(AnyValue.loadFromProperties(newDyncProps).getAnyValue("redkale"), NodeServer.appConfigmergeFunction); + this.config.merge(AnyValue.loadFromProperties(newDyncProps).getAnyValue("redkale"), AppConfig.appConfigmergeFunction); } //使用合并后的新配置节点 propsConf = this.config.getAnyValue("properties"); @@ -736,7 +734,7 @@ public final class Application { } } } - //重置日志配置 + //重置远程日志配置 if (logProps != null && !logProps.isEmpty()) { reconfigLogging(false, logProps); } diff --git a/src/main/java/org/redkale/boot/ModuleEngine.java b/src/main/java/org/redkale/boot/ModuleEngine.java index e697817cc..324889eb9 100644 --- a/src/main/java/org/redkale/boot/ModuleEngine.java +++ b/src/main/java/org/redkale/boot/ModuleEngine.java @@ -72,9 +72,9 @@ public abstract class ModuleEngine { /** * 配置项加载后被调用 * - * @param props 配置项全量 + * @param allProps 配置项全量 */ - public void onEnvironmentLoaded(Properties props) { + public void onEnvironmentLoaded(Properties allProps) { //do nothing } diff --git a/src/main/java/org/redkale/boot/NodeServer.java b/src/main/java/org/redkale/boot/NodeServer.java index 0f8983dc3..e18319a37 100644 --- a/src/main/java/org/redkale/boot/NodeServer.java +++ b/src/main/java/org/redkale/boot/NodeServer.java @@ -990,107 +990,4 @@ public abstract class NodeServer { return this.threadName; } - static final AnyValue.MergeFunction appConfigmergeFunction = (path, key, val1, val2) -> { - if ("".equals(path)) { - if ("executor".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("cluster".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("listener".equals(key)) { - if (Objects.equals(val1.getValue("value"), val2.getValue("value"))) { - return AnyValue.MergeFunction.SKIP; - } else { - return AnyValue.MergeFunction.NONE; - } - } - if ("mq".equals(key)) { - if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { - return AnyValue.MergeFunction.REPLACE; - } else { - return AnyValue.MergeFunction.NONE; - } - } - if ("group".equals(key)) { - if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { - return AnyValue.MergeFunction.REPLACE; - } else { - return AnyValue.MergeFunction.NONE; - } - } - if ("server".equals(key)) { - if (Objects.equals(val1.getValue("name", val1.getValue("protocol") + "_" + val1.getValue("port")), - val2.getValue("name", val2.getValue("protocol") + "_" + val2.getValue("port")))) { - return AnyValue.MergeFunction.REPLACE; - } else { - return AnyValue.MergeFunction.NONE; - } - } - } - if ("cachesource".equals(path)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("datasource".equals(path)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("properties".equals(path)) { - if ("property".equals(key)) { - if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { - return AnyValue.MergeFunction.REPLACE; - } else { - return AnyValue.MergeFunction.NONE; - } - } - } - if ("server".equals(path)) { - if ("ssl".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("render".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("resource-servlet".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - } - if ("server.request".equals(path)) { - if ("remoteaddr".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("rpc".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("locale".equals(key)) { - if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { - return AnyValue.MergeFunction.REPLACE; - } else { - return AnyValue.MergeFunction.NONE; - } - } - } - if ("server.response".equals(path)) { - if ("content-type".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("defcookie".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("options".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("date".equals(key)) { - return AnyValue.MergeFunction.REPLACE; - } - if ("addheader".equals(key) || "setheader".equals(key)) { - if (Objects.equals(val1.getValue("name"), val2.getValue("name"))) { - return AnyValue.MergeFunction.REPLACE; - } else { - return AnyValue.MergeFunction.NONE; - } - } - } - return AnyValue.MergeFunction.MERGE; - }; - } diff --git a/src/main/java/org/redkale/cluster/ClusterModuleEngine.java b/src/main/java/org/redkale/cluster/ClusterModuleEngine.java index be7e05e92..a5c91db8f 100644 --- a/src/main/java/org/redkale/cluster/ClusterModuleEngine.java +++ b/src/main/java/org/redkale/cluster/ClusterModuleEngine.java @@ -43,7 +43,7 @@ public class ClusterModuleEngine extends ModuleEngine { @Override public void onAppPostInit() { ClusterAgent cluster = null; - AnyValue clusterConf = environment.getAnyValue("redkale.cluster", false); + AnyValue clusterConf = application.getAppConfig().getAnyValue("cluster"); if (clusterConf != null) { try { String classVal = application.getPropertyValue(clusterConf.getValue("type", clusterConf.getValue("value"))); //兼容value字段 @@ -109,8 +109,8 @@ public class ClusterModuleEngine extends ModuleEngine { * 配置项加载后被调用 */ @Override - public void onEnvironmentLoaded(Properties props) { - props.forEach((key, val) -> { + public void onEnvironmentLoaded(Properties allProps) { + allProps.forEach((key, val) -> { if (key.toString().startsWith("redkale.cluster.")) { this.clusterProperties.put(key, val); } diff --git a/src/main/java/org/redkale/mq/MessageModuleEngine.java b/src/main/java/org/redkale/mq/MessageModuleEngine.java index 726480345..0f9cf9d41 100644 --- a/src/main/java/org/redkale/mq/MessageModuleEngine.java +++ b/src/main/java/org/redkale/mq/MessageModuleEngine.java @@ -52,11 +52,11 @@ public class MessageModuleEngine extends ModuleEngine { * 配置项加载后被调用 */ @Override - public void onEnvironmentLoaded(Properties props) { + public void onEnvironmentLoaded(Properties allProps) { if (this.messageAgents == null) { return; } - props.forEach((key, val) -> { + allProps.forEach((key, val) -> { if (key.toString().startsWith("redkale.mq.") || key.toString().startsWith("redkale.mq[")) { if (key.toString().endsWith(".name")) { logger.log(Level.WARNING, "skip illegal key " + key + " in mq config, key cannot endsWith '.name'"); diff --git a/src/main/java/org/redkale/source/SourceModuleEngine.java b/src/main/java/org/redkale/source/SourceModuleEngine.java index aa390df6e..6a9d2c45d 100644 --- a/src/main/java/org/redkale/source/SourceModuleEngine.java +++ b/src/main/java/org/redkale/source/SourceModuleEngine.java @@ -62,8 +62,8 @@ public class SourceModuleEngine extends ModuleEngine { * 配置项加载后被调用 */ @Override - public void onEnvironmentLoaded(Properties props) { - props.forEach((key, val) -> { + public void onEnvironmentLoaded(Properties allProps) { + allProps.forEach((key, val) -> { if (key.toString().startsWith("redkale.datasource.") || key.toString().startsWith("redkale.datasource[") || key.toString().startsWith("redkale.cachesource.") || key.toString().startsWith("redkale.cachesource[")) { if (key.toString().endsWith(".name")) {