From 669e6b93b6bfdf7c5bd27a1bf91b3f0027dad976 Mon Sep 17 00:00:00 2001 From: gaoyunpeng Date: Fri, 28 Dec 2018 15:01:36 +0800 Subject: [PATCH 1/4] git commit -m "Fix Incorrect letter , fixes #147" --- .../client/NacosPropertySourceLocator.java | 297 +++++++++--------- 1 file changed, 148 insertions(+), 149 deletions(-) diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index aa9450cc..f73b5eda 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -40,184 +40,183 @@ import java.util.List; @Order(0) public class NacosPropertySourceLocator implements PropertySourceLocator { - private static final Logger LOGGER = LoggerFactory - .getLogger(NacosPropertySourceLocator.class); - private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS"; - private static final String SEP1 = "-"; - private static final String DOT = "."; - private static final String SHARED_CONFIG_SEPRATOR_CHAR = "[,]"; - private static final List SUPPORT_FILE_EXTENSION = Arrays.asList("properties", - "yaml", "yml"); + private static final Logger LOGGER = LoggerFactory + .getLogger(NacosPropertySourceLocator.class); + private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS"; + private static final String SEP1 = "-"; + private static final String DOT = "."; + private static final String SHARED_CONFIG_SEPARATOR_CHAR = "[,]"; + private static final List SUPPORT_FILE_EXTENSION = Arrays.asList("properties", + "yaml", "yml"); - @Autowired - private NacosConfigProperties nacosConfigProperties; + @Autowired + private NacosConfigProperties nacosConfigProperties; - public NacosPropertySourceLocator() { - } + public NacosPropertySourceLocator() { + } - private NacosPropertySourceBuilder nacosPropertySourceBuilder; + private NacosPropertySourceBuilder nacosPropertySourceBuilder; - @Override - public PropertySource locate(Environment env) { + @Override + public PropertySource locate(Environment env) { - ConfigService configService = nacosConfigProperties.configServiceInstance(); + ConfigService configService = nacosConfigProperties.configServiceInstance(); - if (null == configService) { - LOGGER.warn( - "no instance of config service found, can't load config from nacos"); - return null; - } - long timeout = nacosConfigProperties.getTimeout(); - nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, - timeout); - String name = nacosConfigProperties.getName(); + if (null == configService) { + LOGGER.warn( + "no instance of config service found, can't load config from nacos"); + return null; + } + long timeout = nacosConfigProperties.getTimeout(); + nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, + timeout); + String name = nacosConfigProperties.getName(); - String nacosGroup = nacosConfigProperties.getGroup(); - String dataIdPrefix = nacosConfigProperties.getPrefix(); - if (StringUtils.isEmpty(dataIdPrefix)) { - dataIdPrefix = name; - } + String nacosGroup = nacosConfigProperties.getGroup(); + String dataIdPrefix = nacosConfigProperties.getPrefix(); + if (StringUtils.isEmpty(dataIdPrefix)) { + dataIdPrefix = name; + } - if (StringUtils.isEmpty(dataIdPrefix)) { - dataIdPrefix = env.getProperty("spring.application.name"); - } + if (StringUtils.isEmpty(dataIdPrefix)) { + dataIdPrefix = env.getProperty("spring.application.name"); + } - List profiles = Arrays.asList(env.getActiveProfiles()); - nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0])); + List profiles = Arrays.asList(env.getActiveProfiles()); + nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0])); - String fileExtension = nacosConfigProperties.getFileExtension(); + String fileExtension = nacosConfigProperties.getFileExtension(); - CompositePropertySource composite = new CompositePropertySource( - NACOS_PROPERTY_SOURCE_NAME); + CompositePropertySource composite = new CompositePropertySource( + NACOS_PROPERTY_SOURCE_NAME); - loadSharedConfiguration(composite); - loadExtConfiguration(composite); - loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); + loadSharedConfiguration(composite); + loadExtConfiguration(composite); + loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); - return composite; - } + return composite; + } - private void loadSharedConfiguration( - CompositePropertySource compositePropertySource) { - String sharedDataIds = nacosConfigProperties.getSharedDataids(); - String refreshDataIds = nacosConfigProperties.getRefreshableDataids(); + private void loadSharedConfiguration( + CompositePropertySource compositePropertySource) { + String sharedDataIds = nacosConfigProperties.getSharedDataids(); + String refreshDataIds = nacosConfigProperties.getRefreshableDataids(); - if (sharedDataIds == null || sharedDataIds.trim().length() == 0) { - return; - } + if (sharedDataIds == null || sharedDataIds.trim().length() == 0) { + return; + } - String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR); - checkDataIdFileExtension(sharedDataIdArry); + String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR); + checkDataIdFileExtension(sharedDataIdArry); - for (int i = 0; i < sharedDataIdArry.length; i++) { - String dataId = sharedDataIdArry[i]; - String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1); - boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds, - sharedDataIdArry[i]); + for (int i = 0; i < sharedDataIdArry.length; i++) { + String dataId = sharedDataIdArry[i]; + String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1); + boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds, + sharedDataIdArry[i]); - loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP", - fileExtension, isRefreshable); - } - } + loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP", + fileExtension, isRefreshable); + } + } - private void loadExtConfiguration(CompositePropertySource compositePropertySource) { - if (nacosConfigProperties.getExtConfig() == null - || nacosConfigProperties.getExtConfig().isEmpty()) { - return; - } + private void loadExtConfiguration(CompositePropertySource compositePropertySource) { + if (nacosConfigProperties.getExtConfig() == null + || nacosConfigProperties.getExtConfig().isEmpty()) { + return; + } - List extConfigs = nacosConfigProperties - .getExtConfig(); - checkExtConfiguration(extConfigs); + List extConfigs = nacosConfigProperties + .getExtConfig(); + checkExtConfiguration(extConfigs); - for (NacosConfigProperties.Config config : extConfigs) { - String dataId = config.getDataId(); - String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1); - loadNacosDataIfPresent(compositePropertySource, dataId, config.getGroup(), - fileExtension, config.isRefresh()); - } - } + for (NacosConfigProperties.Config config : extConfigs) { + String dataId = config.getDataId(); + String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1); + loadNacosDataIfPresent(compositePropertySource, dataId, config.getGroup(), + fileExtension, config.isRefresh()); + } + } - private void checkExtConfiguration(List extConfigs) { - String[] dataIds = new String[extConfigs.size()]; - for (int i = 0; i < extConfigs.size(); i++) { - String dataId = extConfigs.get(i).getDataId(); - if (dataId == null || dataId.trim().length() == 0) { - throw new IllegalStateException(String.format( - "the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataid", - i)); - } - dataIds[i] = dataId; - } - checkDataIdFileExtension(dataIds); - } + private void checkExtConfiguration(List extConfigs) { + String[] dataIds = new String[extConfigs.size()]; + for (int i = 0; i < extConfigs.size(); i++) { + String dataId = extConfigs.get(i).getDataId(); + if (dataId == null || dataId.trim().length() == 0) { + throw new IllegalStateException(String.format( + "the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataid", + i)); + } + dataIds[i] = dataId; + } + checkDataIdFileExtension(dataIds); + } - private void loadApplicationConfiguration( - CompositePropertySource compositePropertySource, String nacosGroup, - String dataIdPrefix, String fileExtension) { - loadNacosDataIfPresent(compositePropertySource, - dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); - for (String profile : nacosConfigProperties.getActiveProfiles()) { - String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; - loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, - fileExtension, true); - } - } + private void loadApplicationConfiguration( + CompositePropertySource compositePropertySource, String nacosGroup, + String dataIdPrefix, String fileExtension) { + loadNacosDataIfPresent(compositePropertySource, + dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); + for (String profile : nacosConfigProperties.getActiveProfiles()) { + String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; + loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, + fileExtension, true); + } + } - private void loadNacosDataIfPresent(final CompositePropertySource composite, - final String dataId, final String group, String fileExtension, - boolean isRefreshable) { - if (NacosContextRefresher.loadCount.get() != 0) { - NacosPropertySource ps; - if (!isRefreshable) { - ps = NacosPropertySourceRepository.getNacosPropertySource(dataId); - } - else { - ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true); - } + private void loadNacosDataIfPresent(final CompositePropertySource composite, + final String dataId, final String group, String fileExtension, + boolean isRefreshable) { + if (NacosContextRefresher.loadCount.get() != 0) { + NacosPropertySource ps; + if (!isRefreshable) { + ps = NacosPropertySourceRepository.getNacosPropertySource(dataId); + } else { + ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true); + } - composite.addFirstPropertySource(ps); - } - else { - NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, - fileExtension, isRefreshable); - composite.addFirstPropertySource(ps); - } - } + composite.addFirstPropertySource(ps); + } else { + NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, + fileExtension, isRefreshable); + composite.addFirstPropertySource(ps); + } + } - private static void checkDataIdFileExtension(String[] sharedDataIdArry) { - StringBuilder stringBuilder = new StringBuilder(); - outline: for (int i = 0; i < sharedDataIdArry.length; i++) { - for (String fileExtension : SUPPORT_FILE_EXTENSION) { - if (sharedDataIdArry[i].indexOf(fileExtension) > 0) { - continue outline; - } - } - stringBuilder.append(sharedDataIdArry[i] + ","); - } + private static void checkDataIdFileExtension(String[] sharedDataIdArry) { + StringBuilder stringBuilder = new StringBuilder(); + outline: + for (int i = 0; i < sharedDataIdArry.length; i++) { + for (String fileExtension : SUPPORT_FILE_EXTENSION) { + if (sharedDataIdArry[i].indexOf(fileExtension) > 0) { + continue outline; + } + } + stringBuilder.append(sharedDataIdArry[i] + ","); + } - if (stringBuilder.length() > 0) { - String result = stringBuilder.substring(0, stringBuilder.length() - 1); - throw new IllegalStateException(String.format( - "[%s] must contains file extension with properties|yaml|yml", - result)); - } - } + if (stringBuilder.length() > 0) { + String result = stringBuilder.substring(0, stringBuilder.length() - 1); + throw new IllegalStateException(String.format( + "[%s] must contains file extension with properties|yaml|yml", + result)); + } + } - private boolean checkDataIdIsRefreshbable(String refreshDataIds, - String sharedDataId) { - if (refreshDataIds == null || "".equals(refreshDataIds)) { - return false; - } + private boolean checkDataIdIsRefreshbable(String refreshDataIds, + String sharedDataId) { + if (refreshDataIds == null || "".equals(refreshDataIds)) { + return false; + } - String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR); - for (String refreshDataId : refreshDataIdArry) { - if (refreshDataId.equals(sharedDataId)) { - return true; - } - } + String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR); + for (String refreshDataId : refreshDataIdArry) { + if (refreshDataId.equals(sharedDataId)) { + return true; + } + } - return false; - } + return false; + } } From bec0f8830f39c624cfea2c60523322303ac27b04 Mon Sep 17 00:00:00 2001 From: gaoyunpeng Date: Fri, 28 Dec 2018 15:50:37 +0800 Subject: [PATCH 2/4] Fix Incorrect letter , fixes #147 --- .../alibaba/nacos/client/NacosPropertySourceLocator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index f73b5eda..93915dbe 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -106,7 +106,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { return; } - String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR); + String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR); checkDataIdFileExtension(sharedDataIdArry); for (int i = 0; i < sharedDataIdArry.length; i++) { @@ -209,7 +209,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { return false; } - String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR); + String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR); for (String refreshDataId : refreshDataIdArry) { if (refreshDataId.equals(sharedDataId)) { return true; From 3f1194a87a86167ed877771628951c7a22371493 Mon Sep 17 00:00:00 2001 From: xiaojing Date: Wed, 2 Jan 2019 18:25:25 +0800 Subject: [PATCH 3/4] Update README-zh.md --- README-zh.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README-zh.md b/README-zh.md index 4ef6b2c9..204541be 100644 --- a/README-zh.md +++ b/README-zh.md @@ -50,7 +50,7 @@ Spring Cloud 使用 Maven 来构建,最快的使用方式是将本项目clone ## 如何使用 ### 如何引入依赖 -项目已经发布了第一个版本,版本 0.2.0.RELEASE 对应的是 Spring Cloud Finchley 版本,版本 0.1.0.RELEASE 对应的是 Spring Cloud Edgware 版本。 +项目的最新版本是 0.2.1.RELEASE 和 0.1.1.RELEASE,版本 0.2.1.RELEASE 对应的是 Spring Cloud Finchley 版本,版本 0.1.1.RELEASE 对应的是 Spring Cloud Edgware 版本。 如果需要使用已发布的版本,在 `dependencyManagement` 中添加如下配置。 @@ -59,7 +59,7 @@ Spring Cloud 使用 Maven 来构建,最快的使用方式是将本项目clone org.springframework.cloud spring-cloud-alibaba-dependencies - 0.2.0.RELEASE + 0.2.1.RELEASE pom import From 911e3f6357d444feb96a0ed24ec49af498788a0e Mon Sep 17 00:00:00 2001 From: gaoyunpeng Date: Thu, 3 Jan 2019 10:47:57 +0800 Subject: [PATCH 4/4] add Travis CI --- .travis.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..dff5f3a5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: java