This commit is contained in:
redkale
2023-12-15 22:40:19 +08:00
parent eec8939461
commit 387f1688f4
7 changed files with 157 additions and 158 deletions

View File

@@ -17,16 +17,16 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter; import java.util.logging.SimpleFormatter;
import static org.redkale.boot.Application.*; import static org.redkale.boot.Application.*;
import org.redkale.source.DataSources; import org.redkale.source.DataSources;
import org.redkale.util.AnyValue; import org.redkale.util.AnyValue;
import org.redkale.util.AnyValueWriter;
import org.redkale.util.RedkaleClassLoader; import org.redkale.util.RedkaleClassLoader;
import org.redkale.util.RedkaleException; import org.redkale.util.RedkaleException;
import org.redkale.util.AnyValueWriter;
import org.redkale.util.Utility; import org.redkale.util.Utility;
/** /**
@@ -36,11 +36,6 @@ import org.redkale.util.Utility;
*/ */
class AppConfig { class AppConfig {
//日志
private final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
final long startTime = System.currentTimeMillis();
//是否用于main方法运行 //是否用于main方法运行
final boolean singletonMode; 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;
};
} }

View File

@@ -173,9 +173,6 @@ public final class Application {
//配置信息只读版Properties //配置信息只读版Properties
private final Environment environment; private final Environment environment;
//是否从/META-INF中读取配置
private final boolean configFromCache;
//全局根ResourceFactory //全局根ResourceFactory
final ResourceFactory resourceFactory = ResourceFactory.create(); final ResourceFactory resourceFactory = ResourceFactory.create();
@@ -238,7 +235,6 @@ public final class Application {
this.singletonMode = appConfig.singletonMode; this.singletonMode = appConfig.singletonMode;
this.compileMode = appConfig.compileMode; this.compileMode = appConfig.compileMode;
this.config = appConfig.config; this.config = appConfig.config;
this.configFromCache = appConfig.configFromCache;
this.envProperties.putAll(appConfig.localEnvProperties); this.envProperties.putAll(appConfig.localEnvProperties);
this.environment = new Environment(this.envProperties); this.environment = new Environment(this.envProperties);
this.name = appConfig.name; this.name = appConfig.name;
@@ -610,45 +606,45 @@ public final class Application {
} }
} }
if (propsConf != null) { if (propsConf != null) {
final Properties agentEnvs = new Properties(); final Properties remoteEnvs = new Properties();
{ //可能通过系统环境变量配置信息 //可能通过系统环境变量配置信息
Iterator<PropertiesAgentProvider> it = ServiceLoader.load(PropertiesAgentProvider.class, classLoader).iterator(); Iterator<PropertiesAgentProvider> it = ServiceLoader.load(PropertiesAgentProvider.class, classLoader).iterator();
RedkaleClassLoader.putServiceLoader(PropertiesAgentProvider.class); RedkaleClassLoader.putServiceLoader(PropertiesAgentProvider.class);
List<PropertiesAgentProvider> providers = new ArrayList<>(); List<PropertiesAgentProvider> providers = new ArrayList<>();
while (it.hasNext()) { while (it.hasNext()) {
PropertiesAgentProvider provider = it.next(); PropertiesAgentProvider provider = it.next();
if (provider != null && provider.acceptsConf(propsConf)) { if (provider != null && provider.acceptsConf(propsConf)) {
RedkaleClassLoader.putReflectionPublicConstructors(provider.getClass(), provider.getClass().getName()); RedkaleClassLoader.putReflectionPublicConstructors(provider.getClass(), provider.getClass().getName());
providers.add(provider); providers.add(provider);
}
} }
for (PropertiesAgentProvider provider : InstanceProvider.sort(providers)) { }
long s = System.currentTimeMillis(); for (PropertiesAgentProvider provider : InstanceProvider.sort(providers)) {
this.propertiesAgent = provider.createInstance(); long s = System.currentTimeMillis();
this.resourceFactory.inject(this.propertiesAgent); this.propertiesAgent = provider.createInstance();
if (compileMode) { this.resourceFactory.inject(this.propertiesAgent);
this.propertiesAgent.compile(propsConf); if (compileMode) {
} else { this.propertiesAgent.compile(propsConf);
Map<String, Properties> propMap = this.propertiesAgent.init(this, propsConf); } else {
int propCount = 0; Map<String, Properties> propMap = this.propertiesAgent.init(this, propsConf);
if (propMap != null) { int propCount = 0;
for (Map.Entry<String, Properties> en : propMap.entrySet()) { if (propMap != null) {
propCount += en.getValue().size(); for (Map.Entry<String, Properties> en : propMap.entrySet()) {
if (en.getKey().startsWith("logging")) { propCount += en.getValue().size();
if (logProps != null) { if (en.getKey().contains("logging")) {
logger.log(Level.WARNING, "skip repeat logging config properties(" + en.getKey() + ")"); if (logProps != null) {
} else { logger.log(Level.WARNING, "skip repeat logging config properties(" + en.getKey() + ")");
logProps = en.getValue();
}
} else { } 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(); final Properties oldEnvs = new Properties();
@@ -660,7 +656,7 @@ public final class Application {
} }
oldEnvs.put(key, value); oldEnvs.put(key, value);
} }
agentEnvs.forEach((k, v) -> { remoteEnvs.forEach((k, v) -> {
if (k.toString().startsWith("redkale.")) { if (k.toString().startsWith("redkale.")) {
dyncProps.put(k, v); dyncProps.put(k, v);
} else { } else {
@@ -682,6 +678,8 @@ public final class Application {
if (k.toString().startsWith("redkale.executor.") //节点全局唯一 if (k.toString().startsWith("redkale.executor.") //节点全局唯一
|| k.toString().startsWith("redkale.transport.") //节点全局唯一 || k.toString().startsWith("redkale.transport.") //节点全局唯一
|| k.toString().startsWith("redkale.cluster.") //节点全局唯一 || 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.mq[") || k.toString().startsWith("redkale.mq[")
|| k.toString().startsWith("redkale.group.") || k.toString().startsWith("redkale.group.")
@@ -708,7 +706,7 @@ public final class Application {
Properties newDyncProps = new Properties(); Properties newDyncProps = new Properties();
dyncProps.forEach((k, v) -> newDyncProps.put(k.toString(), getPropertyValue(v.toString(), dyncProps))); 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"); propsConf = this.config.getAnyValue("properties");
@@ -736,7 +734,7 @@ public final class Application {
} }
} }
} }
//重置日志配置 //重置远程日志配置
if (logProps != null && !logProps.isEmpty()) { if (logProps != null && !logProps.isEmpty()) {
reconfigLogging(false, logProps); reconfigLogging(false, logProps);
} }

View File

@@ -72,9 +72,9 @@ public abstract class ModuleEngine {
/** /**
* 配置项加载后被调用 * 配置项加载后被调用
* *
* @param props 配置项全量 * @param allProps 配置项全量
*/ */
public void onEnvironmentLoaded(Properties props) { public void onEnvironmentLoaded(Properties allProps) {
//do nothing //do nothing
} }

View File

@@ -990,107 +990,4 @@ public abstract class NodeServer {
return this.threadName; 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;
};
} }

View File

@@ -43,7 +43,7 @@ public class ClusterModuleEngine extends ModuleEngine {
@Override @Override
public void onAppPostInit() { public void onAppPostInit() {
ClusterAgent cluster = null; ClusterAgent cluster = null;
AnyValue clusterConf = environment.getAnyValue("redkale.cluster", false); AnyValue clusterConf = application.getAppConfig().getAnyValue("cluster");
if (clusterConf != null) { if (clusterConf != null) {
try { try {
String classVal = application.getPropertyValue(clusterConf.getValue("type", clusterConf.getValue("value"))); //兼容value字段 String classVal = application.getPropertyValue(clusterConf.getValue("type", clusterConf.getValue("value"))); //兼容value字段
@@ -109,8 +109,8 @@ public class ClusterModuleEngine extends ModuleEngine {
* 配置项加载后被调用 * 配置项加载后被调用
*/ */
@Override @Override
public void onEnvironmentLoaded(Properties props) { public void onEnvironmentLoaded(Properties allProps) {
props.forEach((key, val) -> { allProps.forEach((key, val) -> {
if (key.toString().startsWith("redkale.cluster.")) { if (key.toString().startsWith("redkale.cluster.")) {
this.clusterProperties.put(key, val); this.clusterProperties.put(key, val);
} }

View File

@@ -52,11 +52,11 @@ public class MessageModuleEngine extends ModuleEngine {
* 配置项加载后被调用 * 配置项加载后被调用
*/ */
@Override @Override
public void onEnvironmentLoaded(Properties props) { public void onEnvironmentLoaded(Properties allProps) {
if (this.messageAgents == null) { if (this.messageAgents == null) {
return; return;
} }
props.forEach((key, val) -> { allProps.forEach((key, val) -> {
if (key.toString().startsWith("redkale.mq.") || key.toString().startsWith("redkale.mq[")) { if (key.toString().startsWith("redkale.mq.") || key.toString().startsWith("redkale.mq[")) {
if (key.toString().endsWith(".name")) { if (key.toString().endsWith(".name")) {
logger.log(Level.WARNING, "skip illegal key " + key + " in mq config, key cannot endsWith '.name'"); logger.log(Level.WARNING, "skip illegal key " + key + " in mq config, key cannot endsWith '.name'");

View File

@@ -62,8 +62,8 @@ public class SourceModuleEngine extends ModuleEngine {
* 配置项加载后被调用 * 配置项加载后被调用
*/ */
@Override @Override
public void onEnvironmentLoaded(Properties props) { public void onEnvironmentLoaded(Properties allProps) {
props.forEach((key, val) -> { allProps.forEach((key, val) -> {
if (key.toString().startsWith("redkale.datasource.") || key.toString().startsWith("redkale.datasource[") if (key.toString().startsWith("redkale.datasource.") || key.toString().startsWith("redkale.datasource[")
|| key.toString().startsWith("redkale.cachesource.") || key.toString().startsWith("redkale.cachesource[")) { || key.toString().startsWith("redkale.cachesource.") || key.toString().startsWith("redkale.cachesource[")) {
if (key.toString().endsWith(".name")) { if (key.toString().endsWith(".name")) {