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

code format

This commit is contained in:
pbting 2018-12-04 10:37:57 +08:00
parent 63fd351256
commit 356ce112a9
6 changed files with 113 additions and 83 deletions

View File

@ -38,6 +38,7 @@ import static com.alibaba.nacos.api.PropertyKeyConst.*;
* *
* @author leijuan * @author leijuan
* @author xiaojing * @author xiaojing
* @author pbting
*/ */
@ConfigurationProperties("spring.cloud.nacos.config") @ConfigurationProperties("spring.cloud.nacos.config")
public class NacosConfigProperties { public class NacosConfigProperties {
@ -111,19 +112,20 @@ public class NacosConfigProperties {
private String[] activeProfiles; 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 . * refreshable dataids , multiple separated by commas .
*/ */
private String refreshableDataids ; private String refreshableDataids;
/** /**
* a set of extended configurations . * a set of extended configurations .
*/ */
private List<Config> extConfig ; private List<Config> extConfig;
private ConfigService configService; private ConfigService configService;
@ -265,7 +267,7 @@ public class NacosConfigProperties {
this.extConfig = extConfig; this.extConfig = extConfig;
} }
public static class Config{ public static class Config {
private String dataId; private String dataId;
private String group = "DEFAULT_GROUP"; private String group = "DEFAULT_GROUP";
private boolean refresh = false; private boolean refresh = false;

View File

@ -23,6 +23,7 @@ import java.util.Map;
/** /**
* @author xiaojing * @author xiaojing
* @author pbting
*/ */
public class NacosPropertySource extends MapPropertySource { public class NacosPropertySource extends MapPropertySource {
@ -44,17 +45,18 @@ public class NacosPropertySource extends MapPropertySource {
/** /**
* Whether to support dynamic refresh for this Property Source * Whether to support dynamic refresh for this Property Source
*/ */
private final boolean isRefreshable ; private final boolean isRefreshable;
NacosPropertySource(String group,String dataId, Map<String, Object> source, Date timestamp,boolean isRefreshable) { NacosPropertySource(String group, String dataId, Map<String, Object> source,
Date timestamp, boolean isRefreshable) {
super(dataId, source); super(dataId, source);
this.group = group; this.group = group;
this.dataId = dataId; this.dataId = dataId;
this.timestamp = timestamp; this.timestamp = timestamp;
this.isRefreshable = isRefreshable ; this.isRefreshable = isRefreshable;
} }
public String getGroup(){ public String getGroup() {
return this.group; return this.group;
} }
@ -66,7 +68,7 @@ public class NacosPropertySource extends MapPropertySource {
return timestamp; return timestamp;
} }
public boolean isRefreshable(){ public boolean isRefreshable() {
return isRefreshable; return isRefreshable;
} }
} }

View File

@ -29,6 +29,7 @@ import java.util.*;
/** /**
* @author xiaojing * @author xiaojing
* @author pbting
*/ */
public class NacosPropertySourceBuilder { public class NacosPropertySourceBuilder {
@ -66,12 +67,14 @@ public class NacosPropertySourceBuilder {
* @param dataId Nacos dataId * @param dataId Nacos dataId
* @param group Nacos group * @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); Properties p = loadNacosData(dataId, group, fileExtension);
if (p == null) { if (p == null) {
return 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) { private Properties loadNacosData(String dataId, String group, String fileExtension) {

View File

@ -42,7 +42,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
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_SEPRATOR_CHAR = "[,]";
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties","yaml","yml"); private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
"yaml", "yml");
@Autowired @Autowired
private NacosConfigProperties nacosConfigProperties; private NacosConfigProperties nacosConfigProperties;
@ -76,7 +77,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
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);
@ -84,47 +84,55 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
return composite; return composite;
} }
private void loadSharedConfiguration(CompositePropertySource compositePropertySource){ private void loadSharedConfiguration(
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 ;
}
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; return;
} }
List<NacosConfigProperties.Config> 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<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
.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(),fileExtension,config.isRefresh()); loadNacosDataIfPresent(compositePropertySource, dataId, config.getGroup(),
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("the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataid", i)); throw new IllegalStateException(String.format(
"the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataid",
i));
} }
dataIds[i] = dataId; dataIds[i] = dataId;
} }
@ -144,7 +152,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
} }
private void loadNacosDataIfPresent(final CompositePropertySource composite, private void loadNacosDataIfPresent(final CompositePropertySource composite,
final String dataId, final String group, String fileExtension, boolean isRefreshable) { final String dataId, final String group, String fileExtension,
boolean isRefreshable) {
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
fileExtension, isRefreshable); fileExtension, isRefreshable);
if (ps != null) { if (ps != null) {
@ -152,36 +161,39 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
} }
} }
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 (int i = 0; i < sharedDataIdArry.length; i++) {
for (String fileExtension:SUPPORT_FILE_EXTENSION) { for (String fileExtension : SUPPORT_FILE_EXTENSION) {
if (sharedDataIdArry[i].indexOf(fileExtension) > 0){ if (sharedDataIdArry[i].indexOf(fileExtension) > 0) {
continue outline; 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("[%s] must contains file extension with properties|yaml|yml", result)); throw new IllegalStateException(String.format(
"[%s] must contains file extension with properties|yaml|yml",
result));
} }
} }
private boolean checkDataIdIsRefreshbable(String refreshDataIds,String sharedDataId){ private boolean checkDataIdIsRefreshbable(String refreshDataIds,
if (refreshDataIds == null || "".equals(refreshDataIds)){ String sharedDataId) {
return false ; if (refreshDataIds == null || "".equals(refreshDataIds)) {
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;
} }
} }

View File

@ -43,6 +43,7 @@ import java.util.concurrent.Executor;
* configurations. * configurations.
* *
* @author juven.xuxb * @author juven.xuxb
* @author pbting
*/ */
public class NacosContextRefresher implements ApplicationListener<ApplicationReadyEvent> { public class NacosContextRefresher implements ApplicationListener<ApplicationReadyEvent> {
@ -60,7 +61,7 @@ public class NacosContextRefresher implements ApplicationListener<ApplicationRea
private final ConfigService configService; private final ConfigService configService;
private Map<String,Listener> listenerMap = new ConcurrentHashMap<>(16); private Map<String, Listener> listenerMap = new ConcurrentHashMap<>(16);
public NacosContextRefresher(ContextRefresher contextRefresher, public NacosContextRefresher(ContextRefresher contextRefresher,
NacosConfigProperties properties, NacosRefreshProperties refreshProperties, NacosConfigProperties properties, NacosRefreshProperties refreshProperties,
@ -84,12 +85,12 @@ public class NacosContextRefresher implements ApplicationListener<ApplicationRea
if (refreshProperties.isEnabled()) { if (refreshProperties.isEnabled()) {
for (NacosPropertySource nacosPropertySource : nacosPropertySourceRepository for (NacosPropertySource nacosPropertySource : nacosPropertySourceRepository
.getAll()) { .getAll()) {
if (!nacosPropertySource.isRefreshable()){ if (!nacosPropertySource.isRefreshable()) {
continue; continue;
} }
String dataId = nacosPropertySource.getDataId(); String dataId = nacosPropertySource.getDataId();
registerNacosListener(nacosPropertySource.getGroup(),dataId); registerNacosListener(nacosPropertySource.getGroup(), dataId);
} }
} }
} }
@ -104,8 +105,9 @@ public class NacosContextRefresher implements ApplicationListener<ApplicationRea
try { try {
MessageDigest md = MessageDigest.getInstance("MD5"); MessageDigest md = MessageDigest.getInstance("MD5");
md5 = new BigInteger(1, md.digest(configInfo.getBytes("UTF-8"))) md5 = new BigInteger(1, md.digest(configInfo.getBytes("UTF-8")))
.toString(16); .toString(16);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { }
catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
logger.warn("[Nacos] unable to get md5 for dataId: " + dataId, e); logger.warn("[Nacos] unable to get md5 for dataId: " + dataId, e);
} }
} }
@ -121,7 +123,8 @@ public class NacosContextRefresher implements ApplicationListener<ApplicationRea
try { try {
configService.addListener(dataId, group, listener); configService.addListener(dataId, group, listener);
} catch (NacosException e) { }
catch (NacosException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -7,26 +7,34 @@ import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
public class NacosParameterInitListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { public class NacosParameterInitListener
private static final Logger log = LoggerFactory.getLogger(NacosParameterInitListener.class); implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override private static final Logger log = LoggerFactory
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { .getLogger(NacosParameterInitListener.class);
preparedNacosConfiguration(); @Override
} public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
private void preparedNacosConfiguration(){ preparedNacosConfiguration();
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory }
.buildEdasChangeOrderConfiguration();
log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",edasChangeOrderConfiguration.isEdasManaged()); private void preparedNacosConfiguration() {
if (!edasChangeOrderConfiguration.isEdasManaged()) { EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
return ; .buildEdasChangeOrderConfiguration();
} log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",
//initialize nacos configuration edasChangeOrderConfiguration.isEdasManaged());
System.getProperties().setProperty("spring.cloud.nacos.config.server-addr",""); if (!edasChangeOrderConfiguration.isEdasManaged()) {
System.getProperties().setProperty("spring.cloud.nacos.config.endpoint", edasChangeOrderConfiguration.getAddressServerDomain()); return;
System.getProperties().setProperty("spring.cloud.nacos.config.namespace", edasChangeOrderConfiguration.getTenantId()); }
System.getProperties().setProperty("spring.cloud.nacos.config.access-key", edasChangeOrderConfiguration.getDauthAccessKey()); // initialize nacos configuration
System.getProperties().setProperty("spring.cloud.nacos.config.secret-key", edasChangeOrderConfiguration.getDauthSecretKey()); 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());
}
} }