优化PropertiesAgent

This commit is contained in:
Redkale
2022-11-29 18:26:28 +08:00
parent ea8ffbc510
commit 595e2b83f5
4 changed files with 27 additions and 27 deletions

View File

@@ -168,28 +168,31 @@ public final class Application {
final List<NodeServer> servers = new CopyOnWriteArrayList<>();
//SNCP传输端的TransportFactory, 注意: 只给SNCP使用
final TransportFactory sncpTransportFactory;
private final TransportFactory sncpTransportFactory;
//给客户端使用包含SNCP客户端、自定义数据库客户端连接池
final AsyncGroup clientAsyncGroup;
private final AsyncGroup clientAsyncGroup;
//配置源管理接口
//@since 2.7.0
private PropertiesAgent propertiesAgent;
//只存放system.property.、mimetype.property.、redkale.cachesource(.|[)、redkale.datasource(.|[)和其他非redkale.开头的配置项
final Properties appProperties = new Properties();
//只存放不以system.property.、mimetype.property.、redkale.开头的配置项
private final Properties envProperties = new Properties();
//配置信息只读版Properties
final Environment appEnvironment;
private final Environment environment;
//第三方服务发现管理接口
//@since 2.1.0
final ClusterAgent clusterAgent;
private final ClusterAgent clusterAgent;
//MQ管理接口
//@since 2.1.0
final MessageAgent[] messageAgents;
private final MessageAgent[] messageAgents;
//是否从/META-INF中读取配置
private final boolean configFromCache;
//全局根ResourceFactory
final ResourceFactory resourceFactory = ResourceFactory.create();
@@ -197,9 +200,6 @@ public final class Application {
//服务配置项
final AnyValue config;
//是否从/META-INF中读取配置
final boolean configFromCache;
//排除的jar路径
final String excludelibs;
@@ -254,7 +254,7 @@ public final class Application {
this.compileMode = compileMode;
this.config = config;
this.configFromCache = "true".equals(config.getValue("[config-from-cache]"));
this.appEnvironment = new Environment(this.appProperties);
this.environment = new Environment(this.envProperties);
System.setProperty("redkale.version", Redkale.getDotedVersion());
final File root = new File(System.getProperty(RESNAME_APP_HOME));
@@ -299,7 +299,7 @@ public final class Application {
this.resourceFactory.register(RESNAME_APP_CONF_DIR, File.class, confFile);
this.resourceFactory.register(RESNAME_APP_CONF_DIR, Path.class, confFile.toPath());
}
this.resourceFactory.register(Environment.class, appEnvironment);
this.resourceFactory.register(Environment.class, environment);
{
int nid = config.getIntValue("nodeid", 0);
this.nodeid = nid;
@@ -737,7 +737,7 @@ public final class Application {
in.close();
}
}
} else {
} else { //从url或jar文件中resources读取
try {
final URI sourceURI = RedkaleClassLoader.getConfResourceAsURI(configFromCache ? null : confDir, "source.properties");
InputStream in = sourceURI.toURL().openStream();
@@ -773,7 +773,7 @@ public final class Application {
String key = prop.getValue("name");
String value = prop.getValue("value");
if (key == null || value == null) continue;
putEnvironmentProperties(key, value, null);
putEnvironmentProperty(key, value, null);
}
String dfloads = propertiesConf.getValue("load");
if (dfloads != null) {
@@ -788,7 +788,7 @@ public final class Application {
in.close();
if (logger.isLoggable(Level.FINEST)) logger.log(Level.FINEST, "load properties(" + dfload + ") size = " + ps.size());
ps.forEach((x, y) -> { //load中的配置项除了redkale.cachesource.和redkale.datasource.开头不应该有其他redkale.开头配置项
putEnvironmentProperties(x.toString(), y, null);
putEnvironmentProperty(x.toString(), y, null);
});
} catch (Exception e) {
logger.log(Level.WARNING, "load properties(" + dfload + ") error", e);
@@ -831,6 +831,7 @@ public final class Application {
}
}
}
//sourceProperties转换成cacheResources、dataResources的AnyValue
if (!sourceProperties.isEmpty()) {
AnyValue sourceConf = AnyValue.loadFromProperties(sourceProperties);
AnyValue redNode = sourceConf.getAnyValue("redkale");
@@ -1705,12 +1706,11 @@ public final class Application {
//初始化加载时notifyCache=null
//配置项动态变更时 notifyCache!=null, 由调用方统一执行ResourceFactory.register(notifyCache)
//key只会是system.property.、mimetype.property.、redkale.cachesource(.|[)、redkale.datasource(.|[)和其他非redkale.开头的配置项
void putEnvironmentProperties(String key, Object value, Properties notifyCache) {
void putEnvironmentProperty(String key, Object value, Properties notifyCache) {
if (key == null || value == null) return;
String val = replaceValue(value.toString());
if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[")
|| key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) {
appProperties.put(key, val);
sourceProperties.put(key, val);
} else if (key.startsWith("system.property.")) {
String propName = key.substring("system.property.".length());
@@ -1729,7 +1729,7 @@ public final class Application {
if (key.startsWith("redkale.")) {
throw new RuntimeException("property " + key + " cannot redkale. startsWith");
}
appProperties.put(key, val);
envProperties.put(key, val);
if (notifyCache == null) {
resourceFactory.register("property." + key, val);
} else {

View File

@@ -430,7 +430,7 @@ public class NodeHttpServer extends NodeServer {
@Override //loadServlet执行之后调用
protected void postLoadServlets() {
final ClusterAgent cluster = application.clusterAgent;
final ClusterAgent cluster = application.getClusterAgent();
if (!application.isCompileMode() && cluster != null) {
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
String protocol = pros.value().toUpperCase();

View File

@@ -128,7 +128,7 @@ public abstract class NodeServer {
if (isSNCP()) { // SNCP协议
String host = this.serverConf.getValue("host", isWATCH() ? "127.0.0.1" : "0.0.0.0").replace("0.0.0.0", "");
this.sncpAddress = new InetSocketAddress(host.isEmpty() ? application.localAddress.getAddress().getHostAddress() : host, this.serverConf.getIntValue("port"));
this.sncpGroup = application.sncpTransportFactory.findGroupName(this.sncpAddress);
this.sncpGroup = application.getSncpTransportFactory().findGroupName(this.sncpAddress);
//单向SNCP服务不需要对等group
//if (this.sncpGroup == null) throw new RuntimeException("Server (" + String.valueOf(config).replaceAll("\\s+", " ") + ") not found <group> info");
}
@@ -518,7 +518,7 @@ public abstract class NodeServer {
//Service.init执行之前调用
protected void preInitServices(Set<Service> localServices, Set<Service> remoteServices) {
final ClusterAgent cluster = application.clusterAgent;
final ClusterAgent cluster = application.getClusterAgent();
if (!application.isCompileMode() && cluster != null) {
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
String protocol = pros.value().toUpperCase();
@@ -535,8 +535,8 @@ public abstract class NodeServer {
//Service.destroy执行之前调用
protected void preDestroyServices(Set<Service> localServices, Set<Service> remoteServices) {
if (!application.isCompileMode() && application.clusterAgent != null) { //服务注销
final ClusterAgent cluster = application.clusterAgent;
if (!application.isCompileMode() && application.getClusterAgent() != null) { //服务注销
final ClusterAgent cluster = application.getClusterAgent();
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
String protocol = pros.value().toUpperCase();
if (cluster.containsProtocol(protocol) && cluster.containsPort(server.getSocketAddress().getPort())) {

View File

@@ -38,7 +38,7 @@ public abstract class PropertiesAgent {
public abstract boolean acceptsConf(AnyValue config);
/**
* 初始化配置源,配置项需要写入appProperties并监听配置项的变化
* 初始化配置源,配置项需要写入envProperties并监听配置项的变化
*
* @param application Application
* @param conf 节点配置
@@ -54,12 +54,12 @@ public abstract class PropertiesAgent {
protected void putEnvironmentProperties(Application application, Properties props) {
Properties notifyCache = new Properties();
props.forEach((k, v) -> application.putEnvironmentProperties(k.toString(), v, notifyCache));
props.forEach((k, v) -> application.putEnvironmentProperty(k.toString(), v, notifyCache));
application.resourceFactory.register(notifyCache);
}
protected void putEnvironmentProperties(Application application, String key, Object value) {
application.putEnvironmentProperties(key, value, null);
protected void putEnvironmentProperty(Application application, String key, Object value) {
application.putEnvironmentProperty(key, value, null);
}
protected void reconfigLogging(Application application, Properties loggingProperties) {