1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

git commit -m "Fix Incorrect letter , fixes #147"

This commit is contained in:
gaoyunpeng 2018-12-28 15:01:36 +08:00
parent c524df1124
commit 669e6b93b6

View File

@ -40,184 +40,183 @@ import java.util.List;
@Order(0) @Order(0)
public class NacosPropertySourceLocator implements PropertySourceLocator { public class NacosPropertySourceLocator implements PropertySourceLocator {
private static final Logger LOGGER = LoggerFactory private static final Logger LOGGER = LoggerFactory
.getLogger(NacosPropertySourceLocator.class); .getLogger(NacosPropertySourceLocator.class);
private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS"; private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";
private static final String SEP1 = "-"; private static final String SEP1 = "-";
private static final String DOT = "."; private static final String DOT = ".";
private static final String SHARED_CONFIG_SEPRATOR_CHAR = "[,]"; private static final String SHARED_CONFIG_SEPARATOR_CHAR = "[,]";
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties", private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
"yaml", "yml"); "yaml", "yml");
@Autowired @Autowired
private NacosConfigProperties nacosConfigProperties; private NacosConfigProperties nacosConfigProperties;
public NacosPropertySourceLocator() { public NacosPropertySourceLocator() {
} }
private NacosPropertySourceBuilder nacosPropertySourceBuilder; private NacosPropertySourceBuilder nacosPropertySourceBuilder;
@Override @Override
public PropertySource<?> locate(Environment env) { public PropertySource<?> locate(Environment env) {
ConfigService configService = nacosConfigProperties.configServiceInstance(); ConfigService configService = nacosConfigProperties.configServiceInstance();
if (null == configService) { if (null == configService) {
LOGGER.warn( LOGGER.warn(
"no instance of config service found, can't load config from nacos"); "no instance of config service found, can't load config from nacos");
return null; return null;
} }
long timeout = nacosConfigProperties.getTimeout(); long timeout = nacosConfigProperties.getTimeout();
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
timeout); timeout);
String name = nacosConfigProperties.getName(); String name = nacosConfigProperties.getName();
String nacosGroup = nacosConfigProperties.getGroup(); String nacosGroup = nacosConfigProperties.getGroup();
String dataIdPrefix = nacosConfigProperties.getPrefix(); String dataIdPrefix = nacosConfigProperties.getPrefix();
if (StringUtils.isEmpty(dataIdPrefix)) { if (StringUtils.isEmpty(dataIdPrefix)) {
dataIdPrefix = name; dataIdPrefix = name;
} }
if (StringUtils.isEmpty(dataIdPrefix)) { if (StringUtils.isEmpty(dataIdPrefix)) {
dataIdPrefix = env.getProperty("spring.application.name"); dataIdPrefix = env.getProperty("spring.application.name");
} }
List<String> profiles = Arrays.asList(env.getActiveProfiles()); List<String> profiles = Arrays.asList(env.getActiveProfiles());
nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0])); nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0]));
String fileExtension = nacosConfigProperties.getFileExtension(); String fileExtension = nacosConfigProperties.getFileExtension();
CompositePropertySource composite = new CompositePropertySource( CompositePropertySource composite = new CompositePropertySource(
NACOS_PROPERTY_SOURCE_NAME); NACOS_PROPERTY_SOURCE_NAME);
loadSharedConfiguration(composite); loadSharedConfiguration(composite);
loadExtConfiguration(composite); loadExtConfiguration(composite);
loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension);
return composite; return composite;
} }
private void loadSharedConfiguration( private void loadSharedConfiguration(
CompositePropertySource compositePropertySource) { CompositePropertySource compositePropertySource) {
String sharedDataIds = nacosConfigProperties.getSharedDataids(); String sharedDataIds = nacosConfigProperties.getSharedDataids();
String refreshDataIds = nacosConfigProperties.getRefreshableDataids(); String refreshDataIds = nacosConfigProperties.getRefreshableDataids();
if (sharedDataIds == null || sharedDataIds.trim().length() == 0) { if (sharedDataIds == null || sharedDataIds.trim().length() == 0) {
return; return;
} }
String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR); String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR);
checkDataIdFileExtension(sharedDataIdArry); checkDataIdFileExtension(sharedDataIdArry);
for (int i = 0; i < sharedDataIdArry.length; i++) { for (int i = 0; i < sharedDataIdArry.length; i++) {
String dataId = sharedDataIdArry[i]; String dataId = sharedDataIdArry[i];
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1); String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds, boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds,
sharedDataIdArry[i]); sharedDataIdArry[i]);
loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP", loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP",
fileExtension, isRefreshable); fileExtension, isRefreshable);
} }
} }
private void loadExtConfiguration(CompositePropertySource compositePropertySource) { private void loadExtConfiguration(CompositePropertySource compositePropertySource) {
if (nacosConfigProperties.getExtConfig() == null if (nacosConfigProperties.getExtConfig() == null
|| nacosConfigProperties.getExtConfig().isEmpty()) { || nacosConfigProperties.getExtConfig().isEmpty()) {
return; return;
} }
List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
.getExtConfig(); .getExtConfig();
checkExtConfiguration(extConfigs); checkExtConfiguration(extConfigs);
for (NacosConfigProperties.Config config : extConfigs) { for (NacosConfigProperties.Config config : extConfigs) {
String dataId = config.getDataId(); String dataId = config.getDataId();
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1); String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
loadNacosDataIfPresent(compositePropertySource, dataId, config.getGroup(), loadNacosDataIfPresent(compositePropertySource, dataId, config.getGroup(),
fileExtension, config.isRefresh()); fileExtension, config.isRefresh());
} }
} }
private void checkExtConfiguration(List<NacosConfigProperties.Config> extConfigs) { private void checkExtConfiguration(List<NacosConfigProperties.Config> extConfigs) {
String[] dataIds = new String[extConfigs.size()]; String[] dataIds = new String[extConfigs.size()];
for (int i = 0; i < extConfigs.size(); i++) { for (int i = 0; i < extConfigs.size(); i++) {
String dataId = extConfigs.get(i).getDataId(); String dataId = extConfigs.get(i).getDataId();
if (dataId == null || dataId.trim().length() == 0) { if (dataId == null || dataId.trim().length() == 0) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataid", "the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataid",
i)); i));
} }
dataIds[i] = dataId; dataIds[i] = dataId;
} }
checkDataIdFileExtension(dataIds); checkDataIdFileExtension(dataIds);
} }
private void loadApplicationConfiguration( private void loadApplicationConfiguration(
CompositePropertySource compositePropertySource, String nacosGroup, CompositePropertySource compositePropertySource, String nacosGroup,
String dataIdPrefix, String fileExtension) { String dataIdPrefix, String fileExtension) {
loadNacosDataIfPresent(compositePropertySource, loadNacosDataIfPresent(compositePropertySource,
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true);
for (String profile : nacosConfigProperties.getActiveProfiles()) { for (String profile : nacosConfigProperties.getActiveProfiles()) {
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
fileExtension, true); fileExtension, true);
} }
} }
private void loadNacosDataIfPresent(final CompositePropertySource composite, private void loadNacosDataIfPresent(final CompositePropertySource composite,
final String dataId, final String group, String fileExtension, final String dataId, final String group, String fileExtension,
boolean isRefreshable) { boolean isRefreshable) {
if (NacosContextRefresher.loadCount.get() != 0) { if (NacosContextRefresher.loadCount.get() != 0) {
NacosPropertySource ps; NacosPropertySource ps;
if (!isRefreshable) { if (!isRefreshable) {
ps = NacosPropertySourceRepository.getNacosPropertySource(dataId); ps = NacosPropertySourceRepository.getNacosPropertySource(dataId);
} } else {
else { ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true);
ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true); }
}
composite.addFirstPropertySource(ps); composite.addFirstPropertySource(ps);
} } else {
else { NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, isRefreshable);
fileExtension, isRefreshable); composite.addFirstPropertySource(ps);
composite.addFirstPropertySource(ps); }
} }
}
private static void checkDataIdFileExtension(String[] sharedDataIdArry) { private static void checkDataIdFileExtension(String[] sharedDataIdArry) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
outline: for (int i = 0; i < sharedDataIdArry.length; i++) { outline:
for (String fileExtension : SUPPORT_FILE_EXTENSION) { for (int i = 0; i < sharedDataIdArry.length; i++) {
if (sharedDataIdArry[i].indexOf(fileExtension) > 0) { for (String fileExtension : SUPPORT_FILE_EXTENSION) {
continue outline; if (sharedDataIdArry[i].indexOf(fileExtension) > 0) {
} continue outline;
} }
stringBuilder.append(sharedDataIdArry[i] + ","); }
} stringBuilder.append(sharedDataIdArry[i] + ",");
}
if (stringBuilder.length() > 0) { if (stringBuilder.length() > 0) {
String result = stringBuilder.substring(0, stringBuilder.length() - 1); String result = stringBuilder.substring(0, stringBuilder.length() - 1);
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"[%s] must contains file extension with properties|yaml|yml", "[%s] must contains file extension with properties|yaml|yml",
result)); result));
} }
} }
private boolean checkDataIdIsRefreshbable(String refreshDataIds, private boolean checkDataIdIsRefreshbable(String refreshDataIds,
String sharedDataId) { String sharedDataId) {
if (refreshDataIds == null || "".equals(refreshDataIds)) { if (refreshDataIds == null || "".equals(refreshDataIds)) {
return false; return false;
} }
String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR); String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR);
for (String refreshDataId : refreshDataIdArry) { for (String refreshDataId : refreshDataIdArry) {
if (refreshDataId.equals(sharedDataId)) { if (refreshDataId.equals(sharedDataId)) {
return true; return true;
} }
} }
return false; return false;
} }
} }