diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java index 244834c8..37f9ce96 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java @@ -38,6 +38,7 @@ import static com.alibaba.nacos.api.PropertyKeyConst.*; * * @author leijuan * @author xiaojing + * @author pbting */ @ConfigurationProperties("spring.cloud.nacos.config") public class NacosConfigProperties { @@ -111,19 +112,20 @@ public class NacosConfigProperties { private String[] activeProfiles; /** - * the dataids for configurable multiple shared configurations , multiple separated by commas . + * the dataids for configurable multiple shared configurations , multiple separated by + * commas . */ - private String sharedDataids ; + private String sharedDataids; /** * refreshable dataids , multiple separated by commas . */ - private String refreshableDataids ; + private String refreshableDataids; /** * a set of extended configurations . */ - private List extConfig ; + private List extConfig; private ConfigService configService; @@ -265,7 +267,7 @@ public class NacosConfigProperties { this.extConfig = extConfig; } - public static class Config{ + public static class Config { private String dataId; private String group = "DEFAULT_GROUP"; private boolean refresh = false; diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySource.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySource.java index a47f8182..0066d17e 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySource.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySource.java @@ -23,6 +23,7 @@ import java.util.Map; /** * @author xiaojing + * @author pbting */ public class NacosPropertySource extends MapPropertySource { @@ -44,17 +45,18 @@ public class NacosPropertySource extends MapPropertySource { /** * Whether to support dynamic refresh for this Property Source */ - private final boolean isRefreshable ; + private final boolean isRefreshable; - NacosPropertySource(String group,String dataId, Map source, Date timestamp,boolean isRefreshable) { + NacosPropertySource(String group, String dataId, Map source, + Date timestamp, boolean isRefreshable) { super(dataId, source); this.group = group; this.dataId = dataId; this.timestamp = timestamp; - this.isRefreshable = isRefreshable ; + this.isRefreshable = isRefreshable; } - public String getGroup(){ + public String getGroup() { return this.group; } @@ -66,7 +68,7 @@ public class NacosPropertySource extends MapPropertySource { return timestamp; } - public boolean isRefreshable(){ + public boolean isRefreshable() { return isRefreshable; } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java index 93a7440a..589a3bac 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java @@ -29,6 +29,7 @@ import java.util.*; /** * @author xiaojing + * @author pbting */ public class NacosPropertySourceBuilder { @@ -66,12 +67,14 @@ public class NacosPropertySourceBuilder { * @param dataId Nacos dataId * @param group Nacos group */ - NacosPropertySource build(String dataId, String group, String fileExtension, boolean isRefreshable) { + NacosPropertySource build(String dataId, String group, String fileExtension, + boolean isRefreshable) { Properties p = loadNacosData(dataId, group, fileExtension); if (p == null) { return null; } - return new NacosPropertySource(group,dataId, propertiesToMap(p), new Date(), isRefreshable); + return new NacosPropertySource(group, dataId, propertiesToMap(p), new Date(), + isRefreshable); } private Properties loadNacosData(String dataId, String group, String fileExtension) { 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 598a35e9..a6d69783 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 @@ -42,7 +42,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { 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 List SUPPORT_FILE_EXTENSION = Arrays.asList("properties", + "yaml", "yml"); @Autowired private NacosConfigProperties nacosConfigProperties; @@ -76,7 +77,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); - loadSharedConfiguration(composite); loadExtConfiguration(composite); loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); @@ -84,47 +84,55 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { return composite; } - private void loadSharedConfiguration(CompositePropertySource compositePropertySource){ + private void loadSharedConfiguration( + CompositePropertySource compositePropertySource) { String sharedDataIds = nacosConfigProperties.getSharedDataids(); String refreshDataIds = nacosConfigProperties.getRefreshableDataids(); - if (sharedDataIds == null || sharedDataIds.trim().length() == 0){ - return ; - } - - 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]); - - loadNacosDataIfPresent(compositePropertySource,dataId,"DEFAULT_GROUP",fileExtension, isRefreshable); - } - } - - private void loadExtConfiguration(CompositePropertySource compositePropertySource){ - if (nacosConfigProperties.getExtConfig() == null || nacosConfigProperties.getExtConfig().isEmpty()){ + if (sharedDataIds == null || sharedDataIds.trim().length() == 0) { return; } - List extConfigs = nacosConfigProperties.getExtConfig(); + 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]); + + loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP", + fileExtension, isRefreshable); + } + } + + private void loadExtConfiguration(CompositePropertySource compositePropertySource) { + if (nacosConfigProperties.getExtConfig() == null + || nacosConfigProperties.getExtConfig().isEmpty()) { + return; + } + + List extConfigs = nacosConfigProperties + .getExtConfig(); checkExtConfiguration(extConfigs); - for (NacosConfigProperties.Config config : 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()); + 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 0){ - continue outline; + 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]+","); + 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 ; + for (String refreshDataId : refreshDataIdArry) { + if (refreshDataId.equals(sharedDataId)) { + return true; } } - return false ; + return false; } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/refresh/NacosContextRefresher.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/refresh/NacosContextRefresher.java index 3f8f048e..448f6dd1 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/refresh/NacosContextRefresher.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/refresh/NacosContextRefresher.java @@ -43,6 +43,7 @@ import java.util.concurrent.Executor; * configurations. * * @author juven.xuxb + * @author pbting */ public class NacosContextRefresher implements ApplicationListener { @@ -60,7 +61,7 @@ public class NacosContextRefresher implements ApplicationListener listenerMap = new ConcurrentHashMap<>(16); + private Map listenerMap = new ConcurrentHashMap<>(16); public NacosContextRefresher(ContextRefresher contextRefresher, NacosConfigProperties properties, NacosRefreshProperties refreshProperties, @@ -84,12 +85,12 @@ public class NacosContextRefresher implements ApplicationListener { - private static final Logger log = LoggerFactory.getLogger(NacosParameterInitListener.class); - @Override - public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { +public class NacosParameterInitListener + implements ApplicationListener { + private static final Logger log = LoggerFactory + .getLogger(NacosParameterInitListener.class); - preparedNacosConfiguration(); - } + @Override + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { - private void preparedNacosConfiguration(){ - EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory - .buildEdasChangeOrderConfiguration(); - log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",edasChangeOrderConfiguration.isEdasManaged()); - if (!edasChangeOrderConfiguration.isEdasManaged()) { - return ; - } - //initialize nacos configuration - System.getProperties().setProperty("spring.cloud.nacos.config.server-addr",""); - System.getProperties().setProperty("spring.cloud.nacos.config.endpoint", edasChangeOrderConfiguration.getAddressServerDomain()); - System.getProperties().setProperty("spring.cloud.nacos.config.namespace", edasChangeOrderConfiguration.getTenantId()); - System.getProperties().setProperty("spring.cloud.nacos.config.access-key", edasChangeOrderConfiguration.getDauthAccessKey()); - System.getProperties().setProperty("spring.cloud.nacos.config.secret-key", edasChangeOrderConfiguration.getDauthSecretKey()); - } + preparedNacosConfiguration(); + } + + private void preparedNacosConfiguration() { + EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory + .buildEdasChangeOrderConfiguration(); + log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.", + edasChangeOrderConfiguration.isEdasManaged()); + if (!edasChangeOrderConfiguration.isEdasManaged()) { + return; + } + // initialize nacos configuration + System.getProperties().setProperty("spring.cloud.nacos.config.server-addr", ""); + System.getProperties().setProperty("spring.cloud.nacos.config.endpoint", + edasChangeOrderConfiguration.getAddressServerDomain()); + System.getProperties().setProperty("spring.cloud.nacos.config.namespace", + edasChangeOrderConfiguration.getTenantId()); + System.getProperties().setProperty("spring.cloud.nacos.config.access-key", + edasChangeOrderConfiguration.getDauthAccessKey()); + System.getProperties().setProperty("spring.cloud.nacos.config.secret-key", + edasChangeOrderConfiguration.getDauthSecretKey()); + } } \ No newline at end of file