From cd3765ad69952c8e7a113a76feca58f42b821ba6 Mon Sep 17 00:00:00 2001 From: onewe Date: Fri, 19 Aug 2022 10:00:34 +0800 Subject: [PATCH] =?UTF-8?q?[ISSUE=20#8622]=20=E4=BD=BF=E7=94=A8=E5=B0=BE?= =?UTF-8?q?=E9=80=92=E5=BD=92=E4=BC=98=E5=8C=96=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - SYS更名为ENV - SHARED更改为PROTOTYPE --- .../client/env/JvmArgsPropertySource.java | 6 +- .../alibaba/nacos/client/env/NProperties.java | 2 +- .../client/env/PropertiesPropertySource.java | 62 +++++++------ .../client/env/SearchableProperties.java | 22 ++--- .../alibaba/nacos/client/env/SourceType.java | 8 +- .../client/env/SystemEnvPropertySource.java | 2 +- .../nacos/client/env/NPropertiesTest.java | 89 +++++++++---------- 7 files changed, 99 insertions(+), 92 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/env/JvmArgsPropertySource.java b/client/src/main/java/com/alibaba/nacos/client/env/JvmArgsPropertySource.java index bd392bca076..e9448d8df25 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/JvmArgsPropertySource.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/JvmArgsPropertySource.java @@ -20,7 +20,11 @@ class JvmArgsPropertySource extends AbstractPropertySource { - private final Properties properties = new Properties(System.getProperties()); + private final Properties properties; + + JvmArgsPropertySource() { + this.properties = System.getProperties(); + } @Override SourceType getType() { diff --git a/client/src/main/java/com/alibaba/nacos/client/env/NProperties.java b/client/src/main/java/com/alibaba/nacos/client/env/NProperties.java index 5963cb7abf2..c8b7e800b9a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/NProperties.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/NProperties.java @@ -26,7 +26,7 @@ @SuppressWarnings("PMD.ClassNamingShouldBeCamelRule") public interface NProperties { - NProperties SHARED = SearchableProperties.INSTANCE; + NProperties PROTOTYPE = SearchableProperties.INSTANCE; /** * get property, if the value can not be got by the special key, the null will be returned. diff --git a/client/src/main/java/com/alibaba/nacos/client/env/PropertiesPropertySource.java b/client/src/main/java/com/alibaba/nacos/client/env/PropertiesPropertySource.java index 61bcabfd3c6..695f9b47b6c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/PropertiesPropertySource.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/PropertiesPropertySource.java @@ -25,9 +25,11 @@ class PropertiesPropertySource extends AbstractPropertySource { private final Properties properties = new Properties(); - private PropertiesPropertySource parent; + private final PropertiesPropertySource parent; - PropertiesPropertySource(){} + PropertiesPropertySource() { + this.parent = null; + } PropertiesPropertySource(PropertiesPropertySource parent) { this.parent = parent; @@ -40,51 +42,44 @@ SourceType getType() { @Override String getProperty(String key) { + return getProperty(this, key); + } - String value = properties.getProperty(key); + private String getProperty(PropertiesPropertySource propertiesPropertySource, String key) { + final String value = propertiesPropertySource.properties.getProperty(key); if (value != null) { return value; } - PropertiesPropertySource parent = this.parent; - while (parent != null) { - value = parent.properties.getProperty(key); - if (value != null) { - return value; - } - parent = parent.parent; + final PropertiesPropertySource parent = propertiesPropertySource.parent; + if (parent == null) { + return null; } - - return null; + return getProperty(parent, key); } @Override boolean containsKey(String key) { - boolean exist = properties.containsKey(key); + return containsKey(this, key); + } + + boolean containsKey(PropertiesPropertySource propertiesPropertySource, String key) { + final boolean exist = propertiesPropertySource.properties.containsKey(key); if (exist) { return true; } - PropertiesPropertySource parent = this.parent; - while (parent != null) { - exist = parent.properties.containsKey(key); - if (exist) { - return true; - } - parent = parent.parent; + final PropertiesPropertySource parent = propertiesPropertySource.parent; + if (parent == null) { + return false; } - return false; + return containsKey(parent, key); } @Override Properties asProperties() { List propertiesList = new ArrayList<>(8); - propertiesList.add(properties); - - PropertiesPropertySource parent = this.parent; - while (parent != null) { - propertiesList.add(parent.properties); - parent = parent.parent; - } - + + propertiesList = lookingForProperties(this, propertiesList); + Properties ret = new Properties(); final ListIterator iterator = propertiesList.listIterator(propertiesList.size()); while (iterator.hasPrevious()) { @@ -94,6 +89,15 @@ Properties asProperties() { return ret; } + List lookingForProperties(PropertiesPropertySource propertiesPropertySource, List propertiesList) { + propertiesList.add(propertiesPropertySource.properties); + final PropertiesPropertySource parent = propertiesPropertySource.parent; + if (parent == null) { + return propertiesList; + } + return lookingForProperties(parent, propertiesList); + } + synchronized void setProperty(String key, String value) { properties.setProperty(key, value); } diff --git a/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java b/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java index 1471167f068..c4da24c48a7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java @@ -48,7 +48,7 @@ class SearchableProperties implements NProperties { private static final DefaultSettingPropertySource DEFAULT_SETTING_PROPERTY_SOURCE = new DefaultSettingPropertySource(); private static final List DEFAULT_ORDER = Arrays.asList(SourceType.PROPERTIES, SourceType.JVM, - SourceType.SYS); + SourceType.ENV, SourceType.DEFAULT_SETTING); private static final CompositeConverter CONVERTER = new CompositeConverter(); @@ -64,10 +64,8 @@ private SearchableProperties() { private SearchableProperties(PropertiesPropertySource propertiesPropertySource) { this.propertiesPropertySource = propertiesPropertySource; - final List sourceList = build(propertiesPropertySource, - JVM_ARGS_PROPERTY_SOURCE, SYSTEM_ENV_PROPERTY_SOURCE); - sourceList.add(DEFAULT_SETTING_PROPERTY_SOURCE); - this.propertySources = sourceList; + this.propertySources = build(propertiesPropertySource, + JVM_ARGS_PROPERTY_SOURCE, SYSTEM_ENV_PROPERTY_SOURCE, DEFAULT_SETTING_PROPERTY_SOURCE); } @Override @@ -184,7 +182,7 @@ private List build(AbstractPropertySource... propertySou } return sortPropertySource(sourceType, propertySources); } catch (Exception e) { - LOGGER.error("first source type parse error, it will be used default order!"); + LOGGER.error("first source type parse error, it will be used default order!", e); return sortPropertySourceDefaultOrder(propertySources); } } @@ -195,7 +193,7 @@ private List sortPropertySourceDefaultOrder( .collect(Collectors.toMap(AbstractPropertySource::getType, propertySource -> propertySource)); final List collect = DEFAULT_ORDER.stream().map(sourceMap::get) .collect(Collectors.toList()); - LOGGER.info("properties search order:PROPERTIES->JVM->SYS->DEFAULT_SETTING"); + LOGGER.info("properties search order:PROPERTIES->JVM->ENV->DEFAULT_SETTING"); return collect; } @@ -211,11 +209,13 @@ private List sortPropertySource(SourceType firstType, .stream().map(sourceMap::get).filter(Objects::nonNull).collect(Collectors.toList()); StringBuilder orderInfo = new StringBuilder("properties search order:"); - for (AbstractPropertySource abstractPropertySource : collect) { - orderInfo.append(abstractPropertySource.getType().toString()).append("->"); + for (int i = 0; i < collect.size(); i++) { + final AbstractPropertySource abstractPropertySource = collect.get(i); + orderInfo.append(abstractPropertySource.getType().toString()); + if (i < collect.size() - 1) { + orderInfo.append("->"); + } } - orderInfo.append("DEFAULT_SETTING"); - LOGGER.info(orderInfo.toString()); return collect; diff --git a/client/src/main/java/com/alibaba/nacos/client/env/SourceType.java b/client/src/main/java/com/alibaba/nacos/client/env/SourceType.java index 8a5d1096b14..4727199c6d1 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/SourceType.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/SourceType.java @@ -18,17 +18,17 @@ enum SourceType { /** - * get value from system environment. + * get value from properties. */ - SYS, + PROPERTIES, /** * get value from jvm args. */ JVM, /** - * get value from properties. + * get value from system environment. */ - PROPERTIES, + ENV, /** * get value from default setting. */ diff --git a/client/src/main/java/com/alibaba/nacos/client/env/SystemEnvPropertySource.java b/client/src/main/java/com/alibaba/nacos/client/env/SystemEnvPropertySource.java index 2514770c295..d8689f130e7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/SystemEnvPropertySource.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/SystemEnvPropertySource.java @@ -25,7 +25,7 @@ class SystemEnvPropertySource extends AbstractPropertySource { @Override SourceType getType() { - return SourceType.SYS; + return SourceType.ENV; } @Override diff --git a/client/src/test/java/com/alibaba/nacos/client/env/NPropertiesTest.java b/client/src/test/java/com/alibaba/nacos/client/env/NPropertiesTest.java index a5925650a09..fa6ea131fa0 100644 --- a/client/src/test/java/com/alibaba/nacos/client/env/NPropertiesTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/env/NPropertiesTest.java @@ -37,18 +37,17 @@ public static void teardown() { @Test public void testGetProperty() { - - NProperties.SHARED.setProperty("nacos.home", "/home/nacos"); - final String value = NProperties.SHARED.getProperty("nacos.home"); + NProperties.PROTOTYPE.setProperty("nacos.home", "/home/nacos"); + final String value = NProperties.PROTOTYPE.getProperty("nacos.home"); Assert.assertEquals("/home/nacos", value); } @Test public void testGetPropertyMultiLayer() { - NProperties.SHARED.setProperty("top.layer", "top"); + NProperties.PROTOTYPE.setProperty("top.layer", "top"); - final NProperties layerAEnv = NProperties.SHARED.derive(); + final NProperties layerAEnv = NProperties.PROTOTYPE.derive(); layerAEnv.setProperty("a.layer", "a"); final NProperties layerBEnv = layerAEnv.derive(); @@ -72,70 +71,70 @@ public void testGetPropertyMultiLayer() { @Test public void testGetPropertyDefaultValue() { - final String value = NProperties.SHARED.getProperty("nacos.home.default", "/home/default_value"); + final String value = NProperties.PROTOTYPE.getProperty("nacos.home.default", "/home/default_value"); Assert.assertEquals("/home/default_value", value); } @Test public void testGetBoolean() { - NProperties.SHARED.setProperty("use.cluster", "true"); - final Boolean value = NProperties.SHARED.getBoolean("use.cluster"); + NProperties.PROTOTYPE.setProperty("use.cluster", "true"); + final Boolean value = NProperties.PROTOTYPE.getBoolean("use.cluster"); Assert.assertTrue(value); } @Test public void testGetBooleanDefaultValue() { - final Boolean value = NProperties.SHARED.getBoolean("use.cluster.default", false); + final Boolean value = NProperties.PROTOTYPE.getBoolean("use.cluster.default", false); Assert.assertFalse(value); } @Test public void testGetInteger() { - NProperties.SHARED.setProperty("max.timeout", "200"); - final Integer value = NProperties.SHARED.getInteger("max.timeout"); + NProperties.PROTOTYPE.setProperty("max.timeout", "200"); + final Integer value = NProperties.PROTOTYPE.getInteger("max.timeout"); Assert.assertEquals(200, value.intValue()); } @Test public void testGetIntegerDefaultValue() { - final Integer value = NProperties.SHARED.getInteger("max.timeout.default", 400); + final Integer value = NProperties.PROTOTYPE.getInteger("max.timeout.default", 400); Assert.assertEquals(400, value.intValue()); } @Test public void testGetLong() { - NProperties.SHARED.setProperty("connection.timeout", "200"); - final Long value = NProperties.SHARED.getLong("connection.timeout"); + NProperties.PROTOTYPE.setProperty("connection.timeout", "200"); + final Long value = NProperties.PROTOTYPE.getLong("connection.timeout"); Assert.assertEquals(200L, value.longValue()); } @Test public void testGetLongDefault() { - final Long value = NProperties.SHARED.getLong("connection.timeout.default", 400L); + final Long value = NProperties.PROTOTYPE.getLong("connection.timeout.default", 400L); Assert.assertEquals(400L, value.longValue()); } @Test public void testGetPropertyDefaultSetting() { - final String value = NProperties.SHARED.getProperty("nacos.home.default.test"); + final String value = NProperties.PROTOTYPE.getProperty("nacos.home.default.test"); Assert.assertEquals("/home/default_setting", value); } @Test public void setProperty() { - NProperties.SHARED.setProperty("nacos.set.property", "true"); - final String ret = NProperties.SHARED.getProperty("nacos.set.property"); + NProperties.PROTOTYPE.setProperty("nacos.set.property", "true"); + final String ret = NProperties.PROTOTYPE.getProperty("nacos.set.property"); Assert.assertEquals("true", ret); } @Test public void setPropertyWithScope() { - final NProperties properties = NProperties.SHARED.derive(); + final NProperties properties = NProperties.PROTOTYPE.derive(); properties.setProperty("nacos.set.property.scope", "config"); - String ret = NProperties.SHARED.getProperty("nacos.set.property.scope"); + String ret = NProperties.PROTOTYPE.getProperty("nacos.set.property.scope"); Assert.assertNull(ret); ret = properties.getProperty("nacos.set.property.scope"); @@ -147,9 +146,9 @@ public void testAddProperties() { Properties properties = new Properties(); properties.setProperty("nacos.add.properties", "true"); - NProperties.SHARED.addProperties(properties); + NProperties.PROTOTYPE.addProperties(properties); - final String ret = NProperties.SHARED.getProperty("nacos.add.properties"); + final String ret = NProperties.PROTOTYPE.getProperty("nacos.add.properties"); Assert.assertEquals("true", ret); } @@ -160,10 +159,10 @@ public void testAddPropertiesWithScope() { Properties properties = new Properties(); properties.setProperty("nacos.add.properties.scope", "config"); - final NProperties nProperties = NProperties.SHARED.derive(); + final NProperties nProperties = NProperties.PROTOTYPE.derive(); nProperties.addProperties(properties); - String ret = NProperties.SHARED.getProperty("nacos.add.properties.scope"); + String ret = NProperties.PROTOTYPE.getProperty("nacos.add.properties.scope"); Assert.assertNull(ret); ret = nProperties.getProperty("nacos.add.properties.scope"); @@ -176,7 +175,7 @@ public void testTestDerive() { Properties properties = new Properties(); properties.setProperty("nacos.derive.properties.scope", "derive"); - final NProperties nProperties = NProperties.SHARED.derive(properties); + final NProperties nProperties = NProperties.PROTOTYPE.derive(properties); final String value = nProperties.getProperty("nacos.derive.properties.scope"); @@ -186,21 +185,21 @@ public void testTestDerive() { @Test public void testContainsKey() { - NProperties.SHARED.setProperty("nacos.contains.key", "true"); + NProperties.PROTOTYPE.setProperty("nacos.contains.key", "true"); - boolean ret = NProperties.SHARED.containsKey("nacos.contains.key"); + boolean ret = NProperties.PROTOTYPE.containsKey("nacos.contains.key"); Assert.assertTrue(ret); - ret = NProperties.SHARED.containsKey("nacos.contains.key.in.sys"); + ret = NProperties.PROTOTYPE.containsKey("nacos.contains.key.in.sys"); Assert.assertFalse(ret); } @Test public void testContainsKeyMultiLayers() { - NProperties.SHARED.setProperty("top.layer", "top"); + NProperties.PROTOTYPE.setProperty("top.layer", "top"); - final NProperties layerAEnv = NProperties.SHARED.derive(); + final NProperties layerAEnv = NProperties.PROTOTYPE.derive(); layerAEnv.setProperty("a.layer", "a"); final NProperties layerBEnv = layerAEnv.derive(); @@ -225,14 +224,14 @@ public void testContainsKeyMultiLayers() { @Test public void testContainsKeyWithScope() { - NProperties.SHARED.setProperty("nacos.contains.global.scope", "global"); - final NProperties namingProperties = NProperties.SHARED.derive(); + NProperties.PROTOTYPE.setProperty("nacos.contains.global.scope", "global"); + final NProperties namingProperties = NProperties.PROTOTYPE.derive(); namingProperties.setProperty("nacos.contains.naming.scope", "naming"); - boolean ret = NProperties.SHARED.containsKey("nacos.contains.global.scope"); + boolean ret = NProperties.PROTOTYPE.containsKey("nacos.contains.global.scope"); Assert.assertTrue(ret); - ret = NProperties.SHARED.containsKey("nacos.contains.naming.scope"); + ret = NProperties.PROTOTYPE.containsKey("nacos.contains.naming.scope"); Assert.assertFalse(ret); ret = namingProperties.containsKey("nacos.contains.naming.scope"); @@ -245,8 +244,8 @@ public void testContainsKeyWithScope() { @Test public void testAsProperties() { - NProperties.SHARED.setProperty("nacos.as.properties", "true"); - final Properties properties = NProperties.SHARED.asProperties(); + NProperties.PROTOTYPE.setProperty("nacos.as.properties", "true"); + final Properties properties = NProperties.PROTOTYPE.asProperties(); Assert.assertNotNull(properties); Assert.assertEquals("true", properties.getProperty("nacos.as.properties")); } @@ -254,10 +253,10 @@ public void testAsProperties() { @Test public void testAsPropertiesWithScope() { - NProperties.SHARED.setProperty("nacos.as.properties.global.scope", "global"); - NProperties.SHARED.setProperty("nacos.server.addr.scope", "global"); + NProperties.PROTOTYPE.setProperty("nacos.as.properties.global.scope", "global"); + NProperties.PROTOTYPE.setProperty("nacos.server.addr.scope", "global"); - final NProperties configProperties = NProperties.SHARED.derive(); + final NProperties configProperties = NProperties.PROTOTYPE.derive(); configProperties.setProperty("nacos.server.addr.scope", "config"); final Properties properties = configProperties.asProperties(); @@ -273,21 +272,21 @@ public void testAsPropertiesWithScope() { @Test public void testGetPropertyWithScope() { - NProperties.SHARED.setProperty("nacos.global.scope", "global"); + NProperties.PROTOTYPE.setProperty("nacos.global.scope", "global"); - final NProperties configProperties = NProperties.SHARED.derive(); + final NProperties configProperties = NProperties.PROTOTYPE.derive(); configProperties.setProperty("nacos.config.scope", "config"); - final NProperties namingProperties = NProperties.SHARED.derive(); + final NProperties namingProperties = NProperties.PROTOTYPE.derive(); namingProperties.setProperty("nacos.naming.scope", "naming"); - String ret = NProperties.SHARED.getProperty("nacos.global.scope"); + String ret = NProperties.PROTOTYPE.getProperty("nacos.global.scope"); Assert.assertEquals("global", ret); - ret = NProperties.SHARED.getProperty("nacos.config.scope"); + ret = NProperties.PROTOTYPE.getProperty("nacos.config.scope"); Assert.assertNull(ret); - ret = NProperties.SHARED.getProperty("nacos.naming.scope"); + ret = NProperties.PROTOTYPE.getProperty("nacos.naming.scope"); Assert.assertNull(ret); ret = configProperties.getProperty("nacos.config.scope");