From 595e2b83f5fe4e88eec3f265b098eb90a25666ba Mon Sep 17 00:00:00 2001 From: Redkale Date: Tue, 29 Nov 2022 18:26:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96PropertiesAgent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/boot/Application.java | 36 +++++++++---------- .../java/org/redkale/boot/NodeHttpServer.java | 2 +- .../java/org/redkale/boot/NodeServer.java | 8 ++--- .../org/redkale/boot/PropertiesAgent.java | 8 ++--- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/redkale/boot/Application.java b/src/main/java/org/redkale/boot/Application.java index ec19be1b4..0b892e137 100644 --- a/src/main/java/org/redkale/boot/Application.java +++ b/src/main/java/org/redkale/boot/Application.java @@ -168,28 +168,31 @@ public final class Application { final List 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 { diff --git a/src/main/java/org/redkale/boot/NodeHttpServer.java b/src/main/java/org/redkale/boot/NodeHttpServer.java index ad74ace0f..1e56c7f6c 100644 --- a/src/main/java/org/redkale/boot/NodeHttpServer.java +++ b/src/main/java/org/redkale/boot/NodeHttpServer.java @@ -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(); diff --git a/src/main/java/org/redkale/boot/NodeServer.java b/src/main/java/org/redkale/boot/NodeServer.java index 37627e7f7..6454263f4 100644 --- a/src/main/java/org/redkale/boot/NodeServer.java +++ b/src/main/java/org/redkale/boot/NodeServer.java @@ -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 info"); } @@ -518,7 +518,7 @@ public abstract class NodeServer { //Service.init执行之前调用 protected void preInitServices(Set localServices, Set 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 localServices, Set 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())) { diff --git a/src/main/java/org/redkale/boot/PropertiesAgent.java b/src/main/java/org/redkale/boot/PropertiesAgent.java index 04a7b194d..a511dd487 100644 --- a/src/main/java/org/redkale/boot/PropertiesAgent.java +++ b/src/main/java/org/redkale/boot/PropertiesAgent.java @@ -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) {