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