mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #852 from echooymxq/master
Optimize code and fix #841
This commit is contained in:
commit
b171b30e84
@ -15,10 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.cloud.dubbo.autoconfigure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
@ -148,8 +145,8 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration
|
||||
MethodMetadata factoryMethodMetadata = annotatedBeanDefinition
|
||||
.getFactoryMethodMetadata();
|
||||
attributes = factoryMethodMetadata != null
|
||||
? factoryMethodMetadata
|
||||
.getAnnotationAttributes(DUBBO_TRANSPORTED_CLASS_NAME)
|
||||
? Optional.ofNullable(factoryMethodMetadata
|
||||
.getAnnotationAttributes(DUBBO_TRANSPORTED_CLASS_NAME)).orElse(attributes)
|
||||
: Collections.emptyMap();
|
||||
}
|
||||
return attributesResolver.resolve(attributes);
|
||||
|
@ -41,8 +41,7 @@ public class NacosConfigAutoConfiguration {
|
||||
return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(),
|
||||
NacosConfigProperties.class);
|
||||
}
|
||||
NacosConfigProperties nacosConfigProperties = new NacosConfigProperties();
|
||||
return nacosConfigProperties;
|
||||
return new NacosConfigProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -90,14 +90,14 @@ public class NacosPropertySourceBuilder {
|
||||
log.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
|
||||
dataId, group));
|
||||
|
||||
if (fileExtension.equalsIgnoreCase("properties")) {
|
||||
if ("properties".equalsIgnoreCase(fileExtension)) {
|
||||
Properties properties = new Properties();
|
||||
|
||||
properties.load(new StringReader(data));
|
||||
return properties;
|
||||
}
|
||||
else if (fileExtension.equalsIgnoreCase("yaml")
|
||||
|| fileExtension.equalsIgnoreCase("yml")) {
|
||||
else if ("yaml".equalsIgnoreCase(fileExtension)
|
||||
|| "yml".equalsIgnoreCase(fileExtension)) {
|
||||
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
|
||||
yamlFactory.setResources(new ByteArrayResource(data.getBytes()));
|
||||
return yamlFactory.getObject();
|
||||
@ -118,9 +118,9 @@ public class NacosPropertySourceBuilder {
|
||||
Enumeration<String> keys = (Enumeration<String>) properties.propertyNames();
|
||||
while (keys.hasMoreElements()) {
|
||||
String key = keys.nextElement();
|
||||
Object value = properties.getProperty(key);
|
||||
String value = properties.getProperty(key);
|
||||
if (value != null) {
|
||||
result.put(key, ((String) value).trim());
|
||||
result.put(key, value.trim());
|
||||
}
|
||||
else {
|
||||
result.put(key, null);
|
||||
|
@ -26,6 +26,7 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||
@ -99,14 +100,14 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
return;
|
||||
}
|
||||
|
||||
String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR);
|
||||
checkDataIdFileExtension(sharedDataIdArry);
|
||||
String[] sharedDataIdArray = sharedDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR);
|
||||
checkDataIdFileExtension(sharedDataIdArray);
|
||||
|
||||
for (int i = 0; i < sharedDataIdArry.length; i++) {
|
||||
String dataId = sharedDataIdArry[i];
|
||||
for (int i = 0; i < sharedDataIdArray.length; i++) {
|
||||
String dataId = sharedDataIdArray[i];
|
||||
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
|
||||
boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds,
|
||||
sharedDataIdArry[i]);
|
||||
boolean isRefreshable = checkDataIdIsRefreshable(refreshDataIds,
|
||||
sharedDataIdArray[i]);
|
||||
|
||||
loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP",
|
||||
fileExtension, isRefreshable);
|
||||
@ -114,13 +115,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
}
|
||||
|
||||
private void loadExtConfiguration(CompositePropertySource compositePropertySource) {
|
||||
if (nacosConfigProperties.getExtConfig() == null
|
||||
|| nacosConfigProperties.getExtConfig().isEmpty()) {
|
||||
List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
|
||||
.getExtConfig();
|
||||
|
||||
if (CollectionUtils.isEmpty(extConfigs)) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
|
||||
.getExtConfig();
|
||||
checkExtConfiguration(extConfigs);
|
||||
|
||||
for (NacosConfigProperties.Config config : extConfigs) {
|
||||
@ -137,7 +138,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
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",
|
||||
"the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataId",
|
||||
i));
|
||||
}
|
||||
dataIds[i] = dataId;
|
||||
@ -184,36 +185,35 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private static void checkDataIdFileExtension(String[] dataIdArray) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (int i = 0; i < dataIdArray.length; i++) {
|
||||
boolean isLegal = false;
|
||||
for (String fileExtension : SUPPORT_FILE_EXTENSION) {
|
||||
if (dataIdArray[i].indexOf(fileExtension) > 0) {
|
||||
isLegal = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// add tips
|
||||
if (!isLegal) {
|
||||
stringBuilder.append(dataIdArray[i] + ",");
|
||||
|
||||
for (String dataId : dataIdArray) {
|
||||
if (!canLoadFileExtension(dataId)) {
|
||||
stringBuilder.append(dataId).append(",");
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
"[%s] must end file extension with properties|yaml|yml",
|
||||
result));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkDataIdIsRefreshbable(String refreshDataIds,
|
||||
private static boolean canLoadFileExtension(String dataId) {
|
||||
return SUPPORT_FILE_EXTENSION.stream()
|
||||
.anyMatch((fileExtension) -> StringUtils.endsWithIgnoreCase(dataId,
|
||||
fileExtension));
|
||||
}
|
||||
|
||||
private boolean checkDataIdIsRefreshable(String refreshDataIds,
|
||||
String sharedDataId) {
|
||||
if (refreshDataIds == null || "".equals(refreshDataIds)) {
|
||||
if (StringUtils.isEmpty(refreshDataIds)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR);
|
||||
for (String refreshDataId : refreshDataIdArry) {
|
||||
String[] refreshDataIdArray = refreshDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR);
|
||||
for (String refreshDataId : refreshDataIdArray) {
|
||||
if (refreshDataId.equals(sharedDataId)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -32,6 +32,6 @@ public class NacosConnectionFailureAnalyzer
|
||||
protected FailureAnalysis analyze(Throwable rootFailure,
|
||||
NacosConnectionFailureException cause) {
|
||||
return new FailureAnalysis("Application failed to connect to Nacos server",
|
||||
"check your nacos server config", cause);
|
||||
"check your Nacos server config", cause);
|
||||
}
|
||||
}
|
||||
|
@ -42,12 +42,8 @@ public class NacosConfigEndpoint {
|
||||
|
||||
private final NacosRefreshHistory refreshHistory;
|
||||
|
||||
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
|
||||
@Override
|
||||
protected DateFormat initialValue() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
};
|
||||
private ThreadLocal<DateFormat> dateFormat = ThreadLocal.withInitial(() ->
|
||||
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
public NacosConfigEndpoint(NacosConfigProperties properties,
|
||||
NacosRefreshHistory refreshHistory) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user