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 xiaojing
* @author pbting
*/
@ConfigurationProperties("spring.cloud.nacos.config")
public class NacosConfigProperties {
@ -111,19 +112,20 @@ public class NacosConfigProperties {
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 .
*/
private String refreshableDataids ;
private String refreshableDataids;
/**
* a set of extended configurations .
*/
private List<Config> extConfig ;
private List<Config> extConfig;
private ConfigService configService;
@ -265,7 +267,7 @@ public class NacosConfigProperties {
this.extConfig = extConfig;
}
public static class Config{
public static class Config {
private String dataId;
private String group = "DEFAULT_GROUP";
private boolean refresh = false;

View File

@ -23,6 +23,7 @@ import java.util.Map;
/**
* @author xiaojing
* @author pbting
*/
public class NacosPropertySource extends MapPropertySource {
@ -44,17 +45,18 @@ public class NacosPropertySource extends MapPropertySource {
/**
* 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);
this.group = group;
this.dataId = dataId;
this.timestamp = timestamp;
this.isRefreshable = isRefreshable ;
this.isRefreshable = isRefreshable;
}
public String getGroup(){
public String getGroup() {
return this.group;
}
@ -66,7 +68,7 @@ public class NacosPropertySource extends MapPropertySource {
return timestamp;
}
public boolean isRefreshable(){
public boolean isRefreshable() {
return isRefreshable;
}
}

View File

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

View File

@ -42,7 +42,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
private static final String SEP1 = "-";
private static final String DOT = ".";
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
private NacosConfigProperties nacosConfigProperties;
@ -76,7 +77,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
CompositePropertySource composite = new CompositePropertySource(
NACOS_PROPERTY_SOURCE_NAME);
loadSharedConfiguration(composite);
loadExtConfiguration(composite);
loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension);
@ -84,47 +84,55 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
return composite;
}
private void loadSharedConfiguration(CompositePropertySource compositePropertySource){
private void loadSharedConfiguration(
CompositePropertySource compositePropertySource) {
String sharedDataIds = nacosConfigProperties.getSharedDataids();
String refreshDataIds = nacosConfigProperties.getRefreshableDataids();
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()){
if (sharedDataIds == null || sharedDataIds.trim().length() == 0) {
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);
for (NacosConfigProperties.Config config : extConfigs){
for (NacosConfigProperties.Config config : extConfigs) {
String dataId = config.getDataId();
String fileExtension = dataId.substring(dataId.lastIndexOf(".")+1);
loadNacosDataIfPresent(compositePropertySource,dataId,config.getGroup(),fileExtension,config.isRefresh());
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
loadNacosDataIfPresent(compositePropertySource, dataId, config.getGroup(),
fileExtension, config.isRefresh());
}
}
private void checkExtConfiguration(List<NacosConfigProperties.Config> extConfigs) {
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();
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));
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));
}
dataIds[i] = dataId;
}
@ -144,7 +152,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
}
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,
fileExtension, isRefreshable);
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();
outline:for (int i=0;i < sharedDataIdArry.length; i++){
for (String fileExtension:SUPPORT_FILE_EXTENSION) {
if (sharedDataIdArry[i].indexOf(fileExtension) > 0){
continue outline;
outline: for (int i = 0; i < sharedDataIdArry.length; i++) {
for (String fileExtension : SUPPORT_FILE_EXTENSION) {
if (sharedDataIdArry[i].indexOf(fileExtension) > 0) {
continue outline;
}
}
stringBuilder.append(sharedDataIdArry[i]+",");
stringBuilder.append(sharedDataIdArry[i] + ",");
}
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", result));
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",
result));
}
}
private boolean checkDataIdIsRefreshbable(String refreshDataIds,String sharedDataId){
if (refreshDataIds == null || "".equals(refreshDataIds)){
return false ;
private boolean checkDataIdIsRefreshbable(String refreshDataIds,
String sharedDataId) {
if (refreshDataIds == null || "".equals(refreshDataIds)) {
return false;
}
String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR);
for (String refreshDataId : refreshDataIdArry){
if (refreshDataId.equals(sharedDataId)){
return true ;
for (String refreshDataId : refreshDataIdArry) {
if (refreshDataId.equals(sharedDataId)) {
return true;
}
}
return false ;
return false;
}
}

View File

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

View File

@ -7,26 +7,34 @@ import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
public class NacosParameterInitListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger log = LoggerFactory.getLogger(NacosParameterInitListener.class);
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
public class NacosParameterInitListener
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger log = LoggerFactory
.getLogger(NacosParameterInitListener.class);
preparedNacosConfiguration();
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
private void preparedNacosConfiguration(){
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
.buildEdasChangeOrderConfiguration();
log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",edasChangeOrderConfiguration.isEdasManaged());
if (!edasChangeOrderConfiguration.isEdasManaged()) {
return ;
}
//initialize nacos configuration
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());
}
preparedNacosConfiguration();
}
private void preparedNacosConfiguration() {
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
.buildEdasChangeOrderConfiguration();
log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",
edasChangeOrderConfiguration.isEdasManaged());
if (!edasChangeOrderConfiguration.isEdasManaged()) {
return;
}
// initialize nacos configuration
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());
}
}