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,7 +112,8 @@ 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;

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 {
@ -46,7 +47,8 @@ public class NacosPropertySource extends MapPropertySource {
*/ */
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;

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,7 +84,8 @@ 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();
@ -98,24 +99,29 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
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,sharedDataIdArry[i]); boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds,
sharedDataIdArry[i]);
loadNacosDataIfPresent(compositePropertySource,dataId,"DEFAULT_GROUP",fileExtension, isRefreshable); loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP",
fileExtension, isRefreshable);
} }
} }
private void loadExtConfiguration(CompositePropertySource compositePropertySource) { private void loadExtConfiguration(CompositePropertySource compositePropertySource) {
if (nacosConfigProperties.getExtConfig() == null || nacosConfigProperties.getExtConfig().isEmpty()){ if (nacosConfigProperties.getExtConfig() == null
|| nacosConfigProperties.getExtConfig().isEmpty()) {
return; return;
} }
List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties.getExtConfig(); 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());
} }
} }
@ -124,7 +130,9 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
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) {
@ -165,11 +174,14 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
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,
String sharedDataId) {
if (refreshDataIds == null || "".equals(refreshDataIds)) { if (refreshDataIds == null || "".equals(refreshDataIds)) {
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> {
@ -105,7 +106,8 @@ public class NacosContextRefresher implements ApplicationListener<ApplicationRea
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,8 +7,11 @@ 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> {
private static final Logger log = LoggerFactory
.getLogger(NacosParameterInitListener.class);
@Override @Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
@ -18,15 +21,20 @@ public class NacosParameterInitListener implements ApplicationListener<Applicati
private void preparedNacosConfiguration() { private void preparedNacosConfiguration() {
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
.buildEdasChangeOrderConfiguration(); .buildEdasChangeOrderConfiguration();
log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",edasChangeOrderConfiguration.isEdasManaged()); log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",
edasChangeOrderConfiguration.isEdasManaged());
if (!edasChangeOrderConfiguration.isEdasManaged()) { if (!edasChangeOrderConfiguration.isEdasManaged()) {
return; return;
} }
// initialize nacos configuration // initialize nacos configuration
System.getProperties().setProperty("spring.cloud.nacos.config.server-addr", ""); 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.endpoint",
System.getProperties().setProperty("spring.cloud.nacos.config.namespace", edasChangeOrderConfiguration.getTenantId()); edasChangeOrderConfiguration.getAddressServerDomain());
System.getProperties().setProperty("spring.cloud.nacos.config.access-key", edasChangeOrderConfiguration.getDauthAccessKey()); System.getProperties().setProperty("spring.cloud.nacos.config.namespace",
System.getProperties().setProperty("spring.cloud.nacos.config.secret-key", edasChangeOrderConfiguration.getDauthSecretKey()); edasChangeOrderConfiguration.getTenantId());
System.getProperties().setProperty("spring.cloud.nacos.config.access-key",
edasChangeOrderConfiguration.getDauthAccessKey());
System.getProperties().setProperty("spring.cloud.nacos.config.secret-key",
edasChangeOrderConfiguration.getDauthSecretKey());
} }
} }