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.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;
};
}

View File

@@ -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<PropertiesAgentProvider> it = ServiceLoader.load(PropertiesAgentProvider.class, classLoader).iterator();
RedkaleClassLoader.putServiceLoader(PropertiesAgentProvider.class);
List<PropertiesAgentProvider> 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<PropertiesAgentProvider> it = ServiceLoader.load(PropertiesAgentProvider.class, classLoader).iterator();
RedkaleClassLoader.putServiceLoader(PropertiesAgentProvider.class);
List<PropertiesAgentProvider> 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<String, Properties> propMap = this.propertiesAgent.init(this, propsConf);
int propCount = 0;
if (propMap != null) {
for (Map.Entry<String, Properties> 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<String, Properties> propMap = this.propertiesAgent.init(this, propsConf);
int propCount = 0;
if (propMap != null) {
for (Map.Entry<String, Properties> 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);
}

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
}

View File

@@ -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;
};
}

View File

@@ -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);
}

View File

@@ -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'");

View File

@@ -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")) {