mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish alibaba/spring-cloud-alibaba/#1283 : Renaming spring-cloud-starter-alibaba to be spring-cloud-alibaba-starters
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.refresh.NacosContextRefresher;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshProperties;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
|
||||
public class NacosConfigAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public NacosConfigProperties nacosConfigProperties(ApplicationContext context) {
|
||||
if (context.getParent() != null
|
||||
&& BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
||||
context.getParent(), NacosConfigProperties.class).length > 0) {
|
||||
return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(),
|
||||
NacosConfigProperties.class);
|
||||
}
|
||||
return new NacosConfigProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosRefreshProperties nacosRefreshProperties() {
|
||||
return new NacosRefreshProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosRefreshHistory nacosRefreshHistory() {
|
||||
return new NacosRefreshHistory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosConfigManager nacosConfigManager(
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
return new NacosConfigManager(nacosConfigProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosContextRefresher nacosContextRefresher(
|
||||
NacosConfigManager nacosConfigManager,
|
||||
NacosRefreshHistory nacosRefreshHistory) {
|
||||
// Consider that it is not necessary to be compatible with the previous
|
||||
// configuration
|
||||
// and use the new configuration if necessary.
|
||||
return new NacosContextRefresher(nacosConfigManager, nacosRefreshHistory);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
|
||||
public class NacosConfigBootstrapConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public NacosConfigProperties nacosConfigProperties() {
|
||||
return new NacosConfigProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public NacosConfigManager nacosConfigManager(
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
return new NacosConfigManager(nacosConfigProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosPropertySourceLocator nacosPropertySourceLocator(
|
||||
NacosConfigManager nacosConfigManager) {
|
||||
return new NacosPropertySourceLocator(nacosConfigManager);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureException;
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author zkzlx
|
||||
*/
|
||||
public class NacosConfigManager {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(NacosConfigManager.class);
|
||||
|
||||
private static ConfigService service = null;
|
||||
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
public NacosConfigManager(NacosConfigProperties nacosConfigProperties) {
|
||||
this.nacosConfigProperties = nacosConfigProperties;
|
||||
// Compatible with older code in NacosConfigProperties,It will be deleted in the
|
||||
// future.
|
||||
createConfigService(nacosConfigProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compatible with old design,It will be perfected in the future.
|
||||
*/
|
||||
static ConfigService createConfigService(
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
if (Objects.isNull(service)) {
|
||||
synchronized (NacosConfigManager.class) {
|
||||
try {
|
||||
if (Objects.isNull(service)) {
|
||||
service = NacosFactory.createConfigService(
|
||||
nacosConfigProperties.assembleConfigServiceProperties());
|
||||
}
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.error(e.getMessage());
|
||||
throw new NacosConnectionFailureException(
|
||||
nacosConfigProperties.getServerAddr(), e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return service;
|
||||
}
|
||||
|
||||
public ConfigService getConfigService() {
|
||||
if (Objects.isNull(service)) {
|
||||
createConfigService(this.nacosConfigProperties);
|
||||
}
|
||||
return service;
|
||||
}
|
||||
|
||||
public NacosConfigProperties getNacosConfigProperties() {
|
||||
return nacosConfigProperties;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,700 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.spring.util.PropertySourcesUtils;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.CONFIG_RETRY_TIME;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.ENCODE;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT_PORT;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.MAX_RETRY;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.PASSWORD;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.USERNAME;
|
||||
|
||||
/**
|
||||
* Nacos properties.
|
||||
*
|
||||
* @author leijuan
|
||||
* @author xiaojing
|
||||
* @author pbting
|
||||
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||
*/
|
||||
@ConfigurationProperties(NacosConfigProperties.PREFIX)
|
||||
public class NacosConfigProperties {
|
||||
|
||||
/**
|
||||
* Prefix of {@link NacosConfigProperties}.
|
||||
*/
|
||||
public static final String PREFIX = "spring.cloud.nacos.config";
|
||||
|
||||
/**
|
||||
* COMMAS , .
|
||||
*/
|
||||
public static final String COMMAS = ",";
|
||||
|
||||
/**
|
||||
* SEPARATOR , .
|
||||
*/
|
||||
public static final String SEPARATOR = "[,]";
|
||||
|
||||
private static final Pattern PATTERN = Pattern.compile("-(\\w)");
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(NacosConfigProperties.class);
|
||||
|
||||
@Autowired
|
||||
@JsonIgnore
|
||||
private Environment environment;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.overrideFromEnv();
|
||||
}
|
||||
|
||||
private void overrideFromEnv() {
|
||||
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||
String serverAddr = environment
|
||||
.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}");
|
||||
if (StringUtils.isEmpty(serverAddr)) {
|
||||
serverAddr = environment.resolvePlaceholders(
|
||||
"${spring.cloud.nacos.server-addr:localhost:8848}");
|
||||
}
|
||||
this.setServerAddr(serverAddr);
|
||||
}
|
||||
if (StringUtils.isEmpty(this.getUsername())) {
|
||||
this.setUsername(
|
||||
environment.resolvePlaceholders("${spring.cloud.nacos.username:}"));
|
||||
}
|
||||
if (StringUtils.isEmpty(this.getPassword())) {
|
||||
this.setPassword(
|
||||
environment.resolvePlaceholders("${spring.cloud.nacos.password:}"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* nacos config server address.
|
||||
*/
|
||||
private String serverAddr;
|
||||
|
||||
/**
|
||||
* the nacos authentication username.
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* the nacos authentication password.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* encode for nacos config content.
|
||||
*/
|
||||
private String encode;
|
||||
|
||||
/**
|
||||
* nacos config group, group is config data meta info.
|
||||
*/
|
||||
private String group = "DEFAULT_GROUP";
|
||||
|
||||
/**
|
||||
* nacos config dataId prefix.
|
||||
*/
|
||||
private String prefix;
|
||||
|
||||
/**
|
||||
* the suffix of nacos config dataId, also the file extension of config content.
|
||||
*/
|
||||
private String fileExtension = "properties";
|
||||
|
||||
/**
|
||||
* timeout for get config from nacos.
|
||||
*/
|
||||
private int timeout = 3000;
|
||||
|
||||
/**
|
||||
* nacos maximum number of tolerable server reconnection errors.
|
||||
*/
|
||||
private String maxRetry;
|
||||
|
||||
/**
|
||||
* nacos get config long poll timeout.
|
||||
*/
|
||||
private String configLongPollTimeout;
|
||||
|
||||
/**
|
||||
* nacos get config failure retry time.
|
||||
*/
|
||||
private String configRetryTime;
|
||||
|
||||
/**
|
||||
* If you want to pull it yourself when the program starts to get the configuration
|
||||
* for the first time, and the registered Listener is used for future configuration
|
||||
* updates, you can keep the original code unchanged, just add the system parameter:
|
||||
* enableRemoteSyncConfig = "true" ( But there is network overhead); therefore we
|
||||
* recommend that you use {@link ConfigService#getConfigAndSignListener} directly.
|
||||
*/
|
||||
private boolean enableRemoteSyncConfig = false;
|
||||
|
||||
/**
|
||||
* endpoint for Nacos, the domain name of a service, through which the server address
|
||||
* can be dynamically obtained.
|
||||
*/
|
||||
private String endpoint;
|
||||
|
||||
/**
|
||||
* namespace, separation configuration of different environments.
|
||||
*/
|
||||
private String namespace;
|
||||
|
||||
/**
|
||||
* access key for namespace.
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* secret key for namespace.
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* context path for nacos config server.
|
||||
*/
|
||||
private String contextPath;
|
||||
|
||||
/**
|
||||
* nacos config cluster name.
|
||||
*/
|
||||
private String clusterName;
|
||||
|
||||
/**
|
||||
* nacos config dataId name.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* a set of shared configurations .e.g:
|
||||
* spring.cloud.nacos.config.shared-configs[0]=xxx .
|
||||
*/
|
||||
private List<Config> sharedConfigs;
|
||||
|
||||
/**
|
||||
* a set of extensional configurations .e.g:
|
||||
* spring.cloud.nacos.config.extension-configs[0]=xxx .
|
||||
*/
|
||||
private List<Config> extensionConfigs;
|
||||
|
||||
/**
|
||||
* the master switch for refresh configuration, it default opened(true).
|
||||
*/
|
||||
private boolean refreshEnabled = true;
|
||||
|
||||
// todo sts support
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
|
||||
public void setServerAddr(String serverAddr) {
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public void setPrefix(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public String getFileExtension() {
|
||||
return fileExtension;
|
||||
}
|
||||
|
||||
public void setFileExtension(String fileExtension) {
|
||||
this.fileExtension = fileExtension;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setGroup(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public String getMaxRetry() {
|
||||
return maxRetry;
|
||||
}
|
||||
|
||||
public void setMaxRetry(String maxRetry) {
|
||||
this.maxRetry = maxRetry;
|
||||
}
|
||||
|
||||
public String getConfigLongPollTimeout() {
|
||||
return configLongPollTimeout;
|
||||
}
|
||||
|
||||
public void setConfigLongPollTimeout(String configLongPollTimeout) {
|
||||
this.configLongPollTimeout = configLongPollTimeout;
|
||||
}
|
||||
|
||||
public String getConfigRetryTime() {
|
||||
return configRetryTime;
|
||||
}
|
||||
|
||||
public void setConfigRetryTime(String configRetryTime) {
|
||||
this.configRetryTime = configRetryTime;
|
||||
}
|
||||
|
||||
public Boolean getEnableRemoteSyncConfig() {
|
||||
return enableRemoteSyncConfig;
|
||||
}
|
||||
|
||||
public void setEnableRemoteSyncConfig(Boolean enableRemoteSyncConfig) {
|
||||
this.enableRemoteSyncConfig = enableRemoteSyncConfig;
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
|
||||
public String getEncode() {
|
||||
return encode;
|
||||
}
|
||||
|
||||
public void setEncode(String encode) {
|
||||
this.encode = encode;
|
||||
}
|
||||
|
||||
public String getContextPath() {
|
||||
return contextPath;
|
||||
}
|
||||
|
||||
public void setContextPath(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
public String getClusterName() {
|
||||
return clusterName;
|
||||
}
|
||||
|
||||
public void setClusterName(String clusterName) {
|
||||
this.clusterName = clusterName;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Environment getEnvironment() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
public List<Config> getSharedConfigs() {
|
||||
return sharedConfigs;
|
||||
}
|
||||
|
||||
public void setSharedConfigs(List<Config> sharedConfigs) {
|
||||
this.sharedConfigs = sharedConfigs;
|
||||
}
|
||||
|
||||
public List<Config> getExtensionConfigs() {
|
||||
return extensionConfigs;
|
||||
}
|
||||
|
||||
public void setExtensionConfigs(List<Config> extensionConfigs) {
|
||||
this.extensionConfigs = extensionConfigs;
|
||||
}
|
||||
|
||||
public boolean isRefreshEnabled() {
|
||||
return refreshEnabled;
|
||||
}
|
||||
|
||||
public void setRefreshEnabled(boolean refreshEnabled) {
|
||||
this.refreshEnabled = refreshEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use {@link NacosConfigProperties#sharedConfigs} .
|
||||
* @return string
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecatedConfigurationProperty(
|
||||
reason = "replaced to NacosConfigProperties#sharedConfigs and not use it at the same time.",
|
||||
replacement = PREFIX + ".shared-configs[x]")
|
||||
public String getSharedDataids() {
|
||||
return null == getSharedConfigs() ? null : getSharedConfigs().stream()
|
||||
.map(Config::getDataId).collect(Collectors.joining(COMMAS));
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use {@link NacosConfigProperties#sharedConfigs} and not use it at the
|
||||
* same time .
|
||||
* @param sharedDataids the dataids for configurable multiple shared configurations ,
|
||||
* multiple separated by commas .
|
||||
*/
|
||||
@Deprecated
|
||||
public void setSharedDataids(String sharedDataids) {
|
||||
if (null != sharedDataids && sharedDataids.trim().length() > 0) {
|
||||
List<Config> list = new ArrayList<>();
|
||||
Stream.of(sharedDataids.split(SEPARATOR))
|
||||
.forEach(dataId -> list.add(new Config(dataId.trim())));
|
||||
this.compatibleSharedConfigs(list);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Not providing support,the need to refresh is specified by the respective refresh
|
||||
* configuration and not use it at the same time .
|
||||
* @return string
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecatedConfigurationProperty(
|
||||
reason = "replaced to NacosConfigProperties#sharedConfigs and not use it at the same time.",
|
||||
replacement = PREFIX + ".shared-configs[x].refresh")
|
||||
public String getRefreshableDataids() {
|
||||
return null == getSharedConfigs() ? null
|
||||
: getSharedConfigs().stream().filter(Config::isRefresh)
|
||||
.map(Config::getDataId).collect(Collectors.joining(COMMAS));
|
||||
}
|
||||
|
||||
/**
|
||||
* Not providing support,the need to refresh is specified by the respective refresh
|
||||
* configuration and not use it at the same time .
|
||||
* @param refreshableDataids refreshable dataids ,multiple separated by commas .
|
||||
*/
|
||||
@Deprecated
|
||||
public void setRefreshableDataids(String refreshableDataids) {
|
||||
if (null != refreshableDataids && refreshableDataids.trim().length() > 0) {
|
||||
List<Config> list = new ArrayList<>();
|
||||
Stream.of(refreshableDataids.split(SEPARATOR)).forEach(
|
||||
dataId -> list.add(new Config(dataId.trim()).setRefresh(true)));
|
||||
this.compatibleSharedConfigs(list);
|
||||
}
|
||||
}
|
||||
|
||||
private void compatibleSharedConfigs(List<Config> configList) {
|
||||
if (null != this.getSharedConfigs()) {
|
||||
configList.addAll(this.getSharedConfigs());
|
||||
}
|
||||
List<Config> result = new ArrayList<>();
|
||||
configList.stream()
|
||||
.collect(Collectors.groupingBy(cfg -> (cfg.getGroup() + cfg.getDataId()),
|
||||
() -> new ConcurrentHashMap<>(new LinkedHashMap<>()),
|
||||
Collectors.toList()))
|
||||
.forEach((key, list) -> {
|
||||
list.stream()
|
||||
.reduce((a, b) -> new Config(a.getDataId(), a.getGroup(),
|
||||
a.isRefresh() || (b != null && b.isRefresh())))
|
||||
.ifPresent(result::add);
|
||||
});
|
||||
this.setSharedConfigs(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use
|
||||
* {@link com.alibaba.cloud.nacos.NacosConfigProperties#extensionConfigs} and not use
|
||||
* it at the same time .
|
||||
* @return extensionConfigs
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecatedConfigurationProperty(
|
||||
reason = "replaced to NacosConfigProperties#extensionConfigs and not use it at the same time .",
|
||||
replacement = PREFIX + ".extension-configs[x]")
|
||||
public List<Config> getExtConfig() {
|
||||
return this.getExtensionConfigs();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setExtConfig(List<Config> extConfig) {
|
||||
this.setExtensionConfigs(extConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use {@link NacosConfigManager#getConfigService()}.
|
||||
* @return ConfigService
|
||||
*/
|
||||
@Deprecated
|
||||
public ConfigService configServiceInstance() {
|
||||
// The following code will be migrated
|
||||
return NacosConfigManager.createConfigService(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use {@link NacosConfigProperties#assembleConfigServiceProperties()}.
|
||||
* @return ConfigServiceProperties
|
||||
*/
|
||||
@Deprecated
|
||||
public Properties getConfigServiceProperties() {
|
||||
return this.assembleConfigServiceProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* assemble properties for configService. (cause by rename : Remove the interference
|
||||
* of auto prompts when writing,because autocue is based on get method.
|
||||
* @return properties
|
||||
*/
|
||||
public Properties assembleConfigServiceProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, ""));
|
||||
properties.put(USERNAME, Objects.toString(this.username, ""));
|
||||
properties.put(PASSWORD, Objects.toString(this.password, ""));
|
||||
properties.put(ENCODE, Objects.toString(this.encode, ""));
|
||||
properties.put(NAMESPACE, Objects.toString(this.namespace, ""));
|
||||
properties.put(ACCESS_KEY, Objects.toString(this.accessKey, ""));
|
||||
properties.put(SECRET_KEY, Objects.toString(this.secretKey, ""));
|
||||
properties.put(CLUSTER_NAME, Objects.toString(this.clusterName, ""));
|
||||
properties.put(MAX_RETRY, Objects.toString(this.maxRetry, ""));
|
||||
properties.put(CONFIG_LONG_POLL_TIMEOUT,
|
||||
Objects.toString(this.configLongPollTimeout, ""));
|
||||
properties.put(CONFIG_RETRY_TIME, Objects.toString(this.configRetryTime, ""));
|
||||
properties.put(ENABLE_REMOTE_SYNC_CONFIG,
|
||||
Objects.toString(this.enableRemoteSyncConfig, ""));
|
||||
String endpoint = Objects.toString(this.endpoint, "");
|
||||
if (endpoint.contains(":")) {
|
||||
int index = endpoint.indexOf(":");
|
||||
properties.put(ENDPOINT, endpoint.substring(0, index));
|
||||
properties.put(ENDPOINT_PORT, endpoint.substring(index + 1));
|
||||
}
|
||||
else {
|
||||
properties.put(ENDPOINT, endpoint);
|
||||
}
|
||||
|
||||
enrichNacosConfigProperties(properties);
|
||||
return properties;
|
||||
}
|
||||
|
||||
private void enrichNacosConfigProperties(Properties nacosConfigProperties) {
|
||||
Map<String, Object> properties = PropertySourcesUtils
|
||||
.getSubProperties((ConfigurableEnvironment) environment, PREFIX);
|
||||
properties.forEach((k, v) -> nacosConfigProperties.putIfAbsent(resolveKey(k),
|
||||
String.valueOf(v)));
|
||||
}
|
||||
|
||||
private String resolveKey(String key) {
|
||||
Matcher matcher = PATTERN.matcher(key);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
||||
+ ", encode='" + encode + '\'' + ", group='" + group + '\'' + ", prefix='"
|
||||
+ prefix + '\'' + ", fileExtension='" + fileExtension + '\''
|
||||
+ ", timeout=" + timeout + ", maxRetry='" + maxRetry + '\''
|
||||
+ ", configLongPollTimeout='" + configLongPollTimeout + '\''
|
||||
+ ", configRetryTime='" + configRetryTime + '\''
|
||||
+ ", enableRemoteSyncConfig=" + enableRemoteSyncConfig + ", endpoint='"
|
||||
+ endpoint + '\'' + ", namespace='" + namespace + '\'' + ", accessKey='"
|
||||
+ accessKey + '\'' + ", secretKey='" + secretKey + '\''
|
||||
+ ", contextPath='" + contextPath + '\'' + ", clusterName='" + clusterName
|
||||
+ '\'' + ", name='" + name + '\'' + '\'' + ", shares=" + sharedConfigs
|
||||
+ ", extensions=" + extensionConfigs + ", refreshEnabled="
|
||||
+ refreshEnabled + '}';
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
|
||||
/**
|
||||
* the data id of extended configuration.
|
||||
*/
|
||||
private String dataId;
|
||||
|
||||
/**
|
||||
* the group of extended configuration, the default value is DEFAULT_GROUP.
|
||||
*/
|
||||
private String group = "DEFAULT_GROUP";
|
||||
|
||||
/**
|
||||
* whether to support dynamic refresh, the default does not support .
|
||||
*/
|
||||
private boolean refresh = false;
|
||||
|
||||
public Config() {
|
||||
}
|
||||
|
||||
public Config(String dataId) {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
|
||||
public Config(String dataId, String group) {
|
||||
this(dataId);
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public Config(String dataId, boolean refresh) {
|
||||
this(dataId);
|
||||
this.refresh = refresh;
|
||||
}
|
||||
|
||||
public Config(String dataId, String group, boolean refresh) {
|
||||
this(dataId, group);
|
||||
this.refresh = refresh;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public Config setDataId(String dataId) {
|
||||
this.dataId = dataId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public Config setGroup(String group) {
|
||||
this.group = group;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isRefresh() {
|
||||
return refresh;
|
||||
}
|
||||
|
||||
public Config setRefresh(boolean refresh) {
|
||||
this.refresh = refresh;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Config{" + "dataId='" + dataId + '\'' + ", group='" + group + '\''
|
||||
+ ", refresh=" + refresh + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Config config = (Config) o;
|
||||
return refresh == config.refresh && Objects.equals(dataId, config.dataId)
|
||||
&& Objects.equals(group, config.group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(dataId, group, refresh);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySource;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author pbting
|
||||
*/
|
||||
public final class NacosPropertySourceRepository {
|
||||
|
||||
private final static ConcurrentHashMap<String, NacosPropertySource> NACOS_PROPERTY_SOURCE_REPOSITORY = new ConcurrentHashMap<>();
|
||||
|
||||
private NacosPropertySourceRepository() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all nacos properties from application context.
|
||||
*/
|
||||
public static List<NacosPropertySource> getAll() {
|
||||
return new ArrayList<>(NACOS_PROPERTY_SOURCE_REPOSITORY.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use {@link NacosPropertySourceRepository#collectNacosPropertySource}.
|
||||
* @param nacosPropertySource nacosPropertySource
|
||||
*/
|
||||
@Deprecated
|
||||
public static void collectNacosPropertySources(
|
||||
NacosPropertySource nacosPropertySource) {
|
||||
NACOS_PROPERTY_SOURCE_REPOSITORY.putIfAbsent(nacosPropertySource.getDataId(),
|
||||
nacosPropertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use
|
||||
* {@link NacosPropertySourceRepository#getNacosPropertySource(java.lang.String, java.lang.String)}.
|
||||
* @param dataId dataId
|
||||
* @return NacosPropertySource
|
||||
*/
|
||||
@Deprecated
|
||||
public static NacosPropertySource getNacosPropertySource(String dataId) {
|
||||
return NACOS_PROPERTY_SOURCE_REPOSITORY.get(dataId);
|
||||
}
|
||||
|
||||
public static void collectNacosPropertySource(
|
||||
NacosPropertySource nacosPropertySource) {
|
||||
NACOS_PROPERTY_SOURCE_REPOSITORY
|
||||
.putIfAbsent(getMapKey(nacosPropertySource.getDataId(),
|
||||
nacosPropertySource.getGroup()), nacosPropertySource);
|
||||
}
|
||||
|
||||
public static NacosPropertySource getNacosPropertySource(String dataId,
|
||||
String group) {
|
||||
return NACOS_PROPERTY_SOURCE_REPOSITORY.get(getMapKey(dataId, group));
|
||||
}
|
||||
|
||||
public static String getMapKey(String dataId, String group) {
|
||||
return String.join(NacosConfigProperties.COMMAS, String.valueOf(dataId),
|
||||
String.valueOf(group));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.client;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author pbting
|
||||
*/
|
||||
public class NacosPropertySource extends MapPropertySource {
|
||||
|
||||
/**
|
||||
* Nacos Group.
|
||||
*/
|
||||
private final String group;
|
||||
|
||||
/**
|
||||
* Nacos dataID.
|
||||
*/
|
||||
private final String dataId;
|
||||
|
||||
/**
|
||||
* timestamp the property get.
|
||||
*/
|
||||
private final Date timestamp;
|
||||
|
||||
/**
|
||||
* Whether to support dynamic refresh for this Property Source.
|
||||
*/
|
||||
private final boolean isRefreshable;
|
||||
|
||||
NacosPropertySource(String group, String dataId, Map<String, Object> source,
|
||||
Date timestamp, boolean isRefreshable) {
|
||||
super(String.join(NacosConfigProperties.COMMAS, dataId, group), source);
|
||||
this.group = group;
|
||||
this.dataId = dataId;
|
||||
this.timestamp = timestamp;
|
||||
this.isRefreshable = isRefreshable;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return this.group;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public Date getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public boolean isRefreshable() {
|
||||
return isRefreshable;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.client;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
|
||||
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author pbting
|
||||
*/
|
||||
public class NacosPropertySourceBuilder {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(NacosPropertySourceBuilder.class);
|
||||
|
||||
private static final Map<String, Object> EMPTY_MAP = new LinkedHashMap();
|
||||
|
||||
private ConfigService configService;
|
||||
|
||||
private long timeout;
|
||||
|
||||
public NacosPropertySourceBuilder(ConfigService configService, long timeout) {
|
||||
this.configService = configService;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public long getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(long timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public ConfigService getConfigService() {
|
||||
return configService;
|
||||
}
|
||||
|
||||
public void setConfigService(ConfigService configService) {
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dataId Nacos dataId
|
||||
* @param group Nacos group
|
||||
*/
|
||||
NacosPropertySource build(String dataId, String group, String fileExtension,
|
||||
boolean isRefreshable) {
|
||||
Map<String, Object> p = loadNacosData(dataId, group, fileExtension);
|
||||
NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
|
||||
p, new Date(), isRefreshable);
|
||||
NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource);
|
||||
return nacosPropertySource;
|
||||
}
|
||||
|
||||
private Map<String, Object> loadNacosData(String dataId, String group,
|
||||
String fileExtension) {
|
||||
String data = null;
|
||||
try {
|
||||
data = configService.getConfig(dataId, group, timeout);
|
||||
if (StringUtils.isEmpty(data)) {
|
||||
log.warn(
|
||||
"Ignore the empty nacos configuration and get it based on dataId[{}] & group[{}]",
|
||||
dataId, group);
|
||||
return EMPTY_MAP;
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(String.format(
|
||||
"Loading nacos data, dataId: '%s', group: '%s', data: %s", dataId,
|
||||
group, data));
|
||||
}
|
||||
Map<String, Object> dataMap = NacosDataParserHandler.getInstance()
|
||||
.parseNacosData(data, fileExtension);
|
||||
return dataMap == null ? EMPTY_MAP : dataMap;
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.error("get data from Nacos error,dataId:{}, ", dataId, e);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("parse data from Nacos error,dataId:{},data:{},", dataId, data, e);
|
||||
}
|
||||
return EMPTY_MAP;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.client;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
|
||||
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosContextRefresher;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author pbting
|
||||
*/
|
||||
@Order(0)
|
||||
public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(NacosPropertySourceLocator.class);
|
||||
|
||||
private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";
|
||||
|
||||
private static final String SEP1 = "-";
|
||||
|
||||
private static final String DOT = ".";
|
||||
|
||||
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
|
||||
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
/**
|
||||
* recommend to use
|
||||
* {@link NacosPropertySourceLocator#NacosPropertySourceLocator(com.alibaba.cloud.nacos.NacosConfigManager)}.
|
||||
* @param nacosConfigProperties nacosConfigProperties
|
||||
*/
|
||||
@Deprecated
|
||||
public NacosPropertySourceLocator(NacosConfigProperties nacosConfigProperties) {
|
||||
this.nacosConfigProperties = nacosConfigProperties;
|
||||
}
|
||||
|
||||
public NacosPropertySourceLocator(NacosConfigManager nacosConfigManager) {
|
||||
this.nacosConfigManager = nacosConfigManager;
|
||||
this.nacosConfigProperties = nacosConfigManager.getNacosConfigProperties();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertySource<?> locate(Environment env) {
|
||||
nacosConfigProperties.setEnvironment(env);
|
||||
ConfigService configService = nacosConfigManager.getConfigService();
|
||||
|
||||
if (null == configService) {
|
||||
log.warn("no instance of config service found, can't load config from nacos");
|
||||
return null;
|
||||
}
|
||||
long timeout = nacosConfigProperties.getTimeout();
|
||||
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
|
||||
timeout);
|
||||
String name = nacosConfigProperties.getName();
|
||||
|
||||
String dataIdPrefix = nacosConfigProperties.getPrefix();
|
||||
if (StringUtils.isEmpty(dataIdPrefix)) {
|
||||
dataIdPrefix = name;
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(dataIdPrefix)) {
|
||||
dataIdPrefix = env.getProperty("spring.application.name");
|
||||
}
|
||||
|
||||
CompositePropertySource composite = new CompositePropertySource(
|
||||
NACOS_PROPERTY_SOURCE_NAME);
|
||||
|
||||
loadSharedConfiguration(composite);
|
||||
loadExtConfiguration(composite);
|
||||
loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);
|
||||
|
||||
return composite;
|
||||
}
|
||||
|
||||
/**
|
||||
* load shared configuration.
|
||||
*/
|
||||
private void loadSharedConfiguration(
|
||||
CompositePropertySource compositePropertySource) {
|
||||
List<NacosConfigProperties.Config> sharedConfigs = nacosConfigProperties
|
||||
.getSharedConfigs();
|
||||
if (!CollectionUtils.isEmpty(sharedConfigs)) {
|
||||
checkConfiguration(sharedConfigs, "shared-configs");
|
||||
loadNacosConfiguration(compositePropertySource, sharedConfigs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* load extensional configuration.
|
||||
*/
|
||||
private void loadExtConfiguration(CompositePropertySource compositePropertySource) {
|
||||
List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
|
||||
.getExtensionConfigs();
|
||||
if (!CollectionUtils.isEmpty(extConfigs)) {
|
||||
checkConfiguration(extConfigs, "extension-configs");
|
||||
loadNacosConfiguration(compositePropertySource, extConfigs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* load configuration of application.
|
||||
*/
|
||||
private void loadApplicationConfiguration(
|
||||
CompositePropertySource compositePropertySource, String dataIdPrefix,
|
||||
NacosConfigProperties properties, Environment environment) {
|
||||
String fileExtension = properties.getFileExtension();
|
||||
String nacosGroup = properties.getGroup();
|
||||
// load directly once by default
|
||||
loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup,
|
||||
fileExtension, true);
|
||||
// load with suffix, which have a higher priority than the default
|
||||
loadNacosDataIfPresent(compositePropertySource,
|
||||
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true);
|
||||
// Loaded with profile, which have a higher priority than the suffix
|
||||
for (String profile : environment.getActiveProfiles()) {
|
||||
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
|
||||
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
|
||||
fileExtension, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void loadNacosConfiguration(final CompositePropertySource composite,
|
||||
List<NacosConfigProperties.Config> configs) {
|
||||
for (NacosConfigProperties.Config config : configs) {
|
||||
String dataId = config.getDataId();
|
||||
String fileExtension = dataId.substring(dataId.lastIndexOf(DOT) + 1);
|
||||
loadNacosDataIfPresent(composite, dataId, config.getGroup(), fileExtension,
|
||||
config.isRefresh());
|
||||
}
|
||||
}
|
||||
|
||||
private void checkConfiguration(List<NacosConfigProperties.Config> configs,
|
||||
String tips) {
|
||||
String[] dataIds = new String[configs.size()];
|
||||
for (int i = 0; i < configs.size(); i++) {
|
||||
String dataId = configs.get(i).getDataId();
|
||||
if (dataId == null || dataId.trim().length() == 0) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"the [ spring.cloud.nacos.config.%s[%s] ] must give a dataId",
|
||||
tips, i));
|
||||
}
|
||||
dataIds[i] = dataId;
|
||||
}
|
||||
// Just decide that the current dataId must have a suffix
|
||||
NacosDataParserHandler.getInstance().checkDataId(dataIds);
|
||||
}
|
||||
|
||||
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
||||
final String dataId, final String group, String fileExtension,
|
||||
boolean isRefreshable) {
|
||||
if (null == dataId || dataId.trim().length() < 1) {
|
||||
return;
|
||||
}
|
||||
if (null == group || group.trim().length() < 1) {
|
||||
return;
|
||||
}
|
||||
NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group,
|
||||
fileExtension, isRefreshable);
|
||||
this.addFirstPropertySource(composite, propertySource, false);
|
||||
}
|
||||
|
||||
private NacosPropertySource loadNacosPropertySource(final String dataId,
|
||||
final String group, String fileExtension, boolean isRefreshable) {
|
||||
if (NacosContextRefresher.getRefreshCount() != 0) {
|
||||
if (!isRefreshable) {
|
||||
return NacosPropertySourceRepository.getNacosPropertySource(dataId,
|
||||
group);
|
||||
}
|
||||
}
|
||||
return nacosPropertySourceBuilder.build(dataId, group, fileExtension,
|
||||
isRefreshable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the nacos configuration to the first place and maybe ignore the empty
|
||||
* configuration.
|
||||
*/
|
||||
private void addFirstPropertySource(final CompositePropertySource composite,
|
||||
NacosPropertySource nacosPropertySource, boolean ignoreEmpty) {
|
||||
if (null == nacosPropertySource || null == composite) {
|
||||
return;
|
||||
}
|
||||
if (ignoreEmpty && nacosPropertySource.getSource().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
composite.addFirstPropertySource(nacosPropertySource);
|
||||
}
|
||||
|
||||
public void setNacosConfigManager(NacosConfigManager nacosConfigManager) {
|
||||
this.nacosConfigManager = nacosConfigManager;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.diagnostics.analyzer;
|
||||
|
||||
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
|
||||
/**
|
||||
* A {@code FailureAnalyzer} that performs analysis of failures caused by a
|
||||
* {@code NacosConnectionFailureException}.
|
||||
*
|
||||
* @author juven.xuxb
|
||||
*/
|
||||
public class NacosConnectionFailureAnalyzer
|
||||
extends AbstractFailureAnalyzer<NacosConnectionFailureException> {
|
||||
|
||||
@Override
|
||||
protected FailureAnalysis analyze(Throwable rootFailure,
|
||||
NacosConnectionFailureException cause) {
|
||||
return new FailureAnalysis(
|
||||
"Application failed to connect to Nacos server: \""
|
||||
+ cause.getServerAddr() + "\"",
|
||||
"Please check your Nacos server config", cause);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.diagnostics.analyzer;
|
||||
|
||||
/**
|
||||
* A {@code NacosConnectionFailureException} is thrown when the application fails to
|
||||
* connect to Nacos Server.
|
||||
*
|
||||
* @author juven.xuxb
|
||||
*/
|
||||
public class NacosConnectionFailureException extends RuntimeException {
|
||||
|
||||
private final String serverAddr;
|
||||
|
||||
public NacosConnectionFailureException(String serverAddr, String message) {
|
||||
super(message);
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
|
||||
public NacosConnectionFailureException(String serverAddr, String message,
|
||||
Throwable cause) {
|
||||
super(message, cause);
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.endpoint;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySource;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
|
||||
/**
|
||||
* Endpoint for Nacos, contains config data and refresh history.
|
||||
*
|
||||
* @author xiaojing
|
||||
*/
|
||||
@Endpoint(id = "nacos-config")
|
||||
public class NacosConfigEndpoint {
|
||||
|
||||
private final NacosConfigProperties properties;
|
||||
|
||||
private final NacosRefreshHistory refreshHistory;
|
||||
|
||||
private ThreadLocal<DateFormat> dateFormat = ThreadLocal
|
||||
.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
public NacosConfigEndpoint(NacosConfigProperties properties,
|
||||
NacosRefreshHistory refreshHistory) {
|
||||
this.properties = properties;
|
||||
this.refreshHistory = refreshHistory;
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> invoke() {
|
||||
Map<String, Object> result = new HashMap<>(16);
|
||||
result.put("NacosConfigProperties", properties);
|
||||
|
||||
List<NacosPropertySource> all = NacosPropertySourceRepository.getAll();
|
||||
|
||||
List<Map<String, Object>> sources = new ArrayList<>();
|
||||
for (NacosPropertySource ps : all) {
|
||||
Map<String, Object> source = new HashMap<>(16);
|
||||
source.put("dataId", ps.getDataId());
|
||||
source.put("lastSynced", dateFormat.get().format(ps.getTimestamp()));
|
||||
sources.add(source);
|
||||
}
|
||||
result.put("Sources", sources);
|
||||
result.put("RefreshHistory", refreshHistory.getRecords());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.endpoint;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnClass(Endpoint.class)
|
||||
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
|
||||
public class NacosConfigEndpointAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory nacosRefreshHistory;
|
||||
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledEndpoint
|
||||
@Bean
|
||||
public NacosConfigEndpoint nacosConfigEndpoint() {
|
||||
return new NacosConfigEndpoint(nacosConfigManager.getNacosConfigProperties(),
|
||||
nacosRefreshHistory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(name = "spring.cloud.nacos.config.health.enabled",
|
||||
matchIfMissing = true)
|
||||
public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
|
||||
return new NacosConfigHealthIndicator(nacosConfigManager.getConfigService());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.endpoint;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
|
||||
/**
|
||||
* The {@link HealthIndicator} for Nacos Config.
|
||||
*
|
||||
* @author xiaojing
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public class NacosConfigHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private final ConfigService configService;
|
||||
|
||||
public NacosConfigHealthIndicator(ConfigService configService) {
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
// Just return "UP" or "DOWN"
|
||||
String status = configService.getServerStatus();
|
||||
// Set the status to Builder
|
||||
builder.status(status);
|
||||
switch (status) {
|
||||
case "UP":
|
||||
builder.up();
|
||||
break;
|
||||
case "DOWN":
|
||||
builder.down();
|
||||
break;
|
||||
default:
|
||||
builder.unknown();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author zkz
|
||||
*/
|
||||
public abstract class AbstractNacosDataParser {
|
||||
|
||||
protected static final String DOT = ".";
|
||||
|
||||
protected static final String VALUE = "value";
|
||||
|
||||
protected static final String EMPTY_STRING = "";
|
||||
|
||||
private String extension;
|
||||
|
||||
private AbstractNacosDataParser nextParser;
|
||||
|
||||
protected AbstractNacosDataParser(String extension) {
|
||||
if (StringUtils.isEmpty(extension)) {
|
||||
throw new IllegalArgumentException("extension cannot be empty");
|
||||
}
|
||||
this.extension = extension.toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify dataId extensions.
|
||||
* @param extension file extension. json or xml or yml or yaml or properties
|
||||
* @return valid or not
|
||||
*/
|
||||
public final boolean checkFileExtension(String extension) {
|
||||
if (this.isLegal(extension.toLowerCase())) {
|
||||
return true;
|
||||
}
|
||||
if (this.nextParser == null) {
|
||||
return false;
|
||||
}
|
||||
return this.nextParser.checkFileExtension(extension);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parsing nacos configuration content.
|
||||
* @param data config data from Nacos
|
||||
* @param extension file extension. json or xml or yml or yaml or properties
|
||||
* @return result of Properties
|
||||
* @throws IOException thrown if there is a problem parsing config.
|
||||
*/
|
||||
public final Map<String, Object> parseNacosData(String data, String extension)
|
||||
throws IOException {
|
||||
if (extension == null || extension.length() < 1) {
|
||||
throw new IllegalStateException("The file extension cannot be empty");
|
||||
}
|
||||
if (this.isLegal(extension.toLowerCase())) {
|
||||
return this.doParse(data);
|
||||
}
|
||||
if (this.nextParser == null) {
|
||||
throw new IllegalStateException(getTips(extension));
|
||||
}
|
||||
return this.nextParser.parseNacosData(data, extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* Core logic for parsing.
|
||||
* @param data config from Nacos
|
||||
* @return result of Properties
|
||||
* @throws IOException thrown if there is a problem parsing config.
|
||||
*/
|
||||
protected abstract Map<String, Object> doParse(String data) throws IOException;
|
||||
|
||||
protected AbstractNacosDataParser setNextParser(AbstractNacosDataParser nextParser) {
|
||||
this.nextParser = nextParser;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AbstractNacosDataParser addNextParser(AbstractNacosDataParser nextParser) {
|
||||
if (this.nextParser == null) {
|
||||
this.nextParser = nextParser;
|
||||
}
|
||||
else {
|
||||
this.nextParser.addNextParser(nextParser);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
protected boolean isLegal(String extension) {
|
||||
return this.extension.equalsIgnoreCase(extension)
|
||||
|| this.extension.contains(extension);
|
||||
}
|
||||
|
||||
protected void flattenedMap(Map<String, Object> result, Map<String, Object> dataMap,
|
||||
String parentKey) {
|
||||
Set<Map.Entry<String, Object>> entries = dataMap.entrySet();
|
||||
for (Iterator<Map.Entry<String, Object>> iterator = entries.iterator(); iterator
|
||||
.hasNext();) {
|
||||
Map.Entry<String, Object> entry = iterator.next();
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
|
||||
String fullKey = StringUtils.isEmpty(parentKey) ? key : key.startsWith("[")
|
||||
? parentKey.concat(key) : parentKey.concat(DOT).concat(key);
|
||||
|
||||
if (value instanceof Map) {
|
||||
Map<String, Object> map = (Map<String, Object>) value;
|
||||
flattenedMap(result, map, fullKey);
|
||||
continue;
|
||||
}
|
||||
else if (value instanceof Collection) {
|
||||
int count = 0;
|
||||
Collection<Object> collection = (Collection<Object>) value;
|
||||
for (Object object : collection) {
|
||||
flattenedMap(result,
|
||||
Collections.singletonMap("[" + (count++) + "]", object),
|
||||
fullKey);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
result.put(fullKey, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload the key ending in `value` if need.
|
||||
*/
|
||||
protected Map<String, Object> reloadMap(Map<String, Object> map) {
|
||||
if (map == null || map.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> result = new LinkedHashMap<>(map);
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (key.contains(DOT)) {
|
||||
int idx = key.lastIndexOf(DOT);
|
||||
String suffix = key.substring(idx + 1);
|
||||
if (VALUE.equalsIgnoreCase(suffix)) {
|
||||
result.put(key.substring(0, idx), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String getTips(String fileName) {
|
||||
return String.format(
|
||||
"[%s] must contains file extension with properties|yaml|yml|xml|json",
|
||||
fileName);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author zkz
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
public class NacosDataJsonParser extends AbstractNacosDataParser {
|
||||
|
||||
protected NacosDataJsonParser() {
|
||||
super("json");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> doParse(String data) throws IOException {
|
||||
if (StringUtils.isEmpty(data)) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> map = parseJSON2Map(data);
|
||||
return this.reloadMap(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON to Map.
|
||||
* @param json json data
|
||||
* @return the map convert by json string
|
||||
* @throws IOException thrown if there is a problem parsing config.
|
||||
*/
|
||||
private Map<String, Object> parseJSON2Map(String json) throws IOException {
|
||||
Map<String, Object> result = new LinkedHashMap<>(32);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, Object> nacosDataMap = mapper.readValue(json, LinkedHashMap.class);
|
||||
|
||||
if (CollectionUtils.isEmpty(nacosDataMap)) {
|
||||
return result;
|
||||
}
|
||||
flattenedMap(result, nacosDataMap, EMPTY_STRING);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zkz
|
||||
*/
|
||||
public final class NacosDataParserHandler {
|
||||
|
||||
private AbstractNacosDataParser parser;
|
||||
|
||||
private NacosDataParserHandler() {
|
||||
parser = this.createParser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parsing nacos configuration content.
|
||||
* @param data config from Nacos
|
||||
* @param extension file extension. json or xml or yml or yaml or properties
|
||||
* @return result of LinkedHashMap
|
||||
* @throws IOException thrown if there is a problem parsing config.
|
||||
*/
|
||||
public Map<String, Object> parseNacosData(String data, String extension)
|
||||
throws IOException {
|
||||
if (null == parser) {
|
||||
parser = this.createParser();
|
||||
}
|
||||
return parser.parseNacosData(data, extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* check the validity of file extensions in dataid.
|
||||
* @param dataIdAry array of dataId
|
||||
* @return dataId handle success or not
|
||||
*/
|
||||
public boolean checkDataId(String... dataIdAry) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (String dataId : dataIdAry) {
|
||||
int idx = dataId.lastIndexOf(AbstractNacosDataParser.DOT);
|
||||
if (idx > 0 && idx < dataId.length() - 1) {
|
||||
String extension = dataId.substring(idx + 1);
|
||||
if (parser.checkFileExtension(extension)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// add tips
|
||||
stringBuilder.append(dataId).append(",");
|
||||
}
|
||||
if (stringBuilder.length() > 0) {
|
||||
String result = stringBuilder.substring(0, stringBuilder.length() - 1);
|
||||
throw new IllegalStateException(AbstractNacosDataParser.getTips(result));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private AbstractNacosDataParser createParser() {
|
||||
return new NacosDataPropertiesParser().addNextParser(new NacosDataYamlParser())
|
||||
.addNextParser(new NacosDataXmlParser())
|
||||
.addNextParser(new NacosDataJsonParser());
|
||||
}
|
||||
|
||||
public static NacosDataParserHandler getInstance() {
|
||||
return ParserHandler.HANDLER;
|
||||
}
|
||||
|
||||
private static class ParserHandler {
|
||||
|
||||
private static final NacosDataParserHandler HANDLER = new NacosDataParserHandler();
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.parser;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author zkz
|
||||
*/
|
||||
public class NacosDataPropertiesParser extends AbstractNacosDataParser {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(NacosDataPropertiesParser.class);
|
||||
|
||||
public NacosDataPropertiesParser() {
|
||||
super("properties");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> doParse(String data) throws IOException {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new StringReader(data))) {
|
||||
for (String line = reader.readLine(); line != null; line = reader
|
||||
.readLine()) {
|
||||
String dataLine = line.trim();
|
||||
if (StringUtils.isEmpty(dataLine) || dataLine.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
int index = dataLine.indexOf("=");
|
||||
if (index == -1) {
|
||||
log.warn("the config data is invalid {}", dataLine);
|
||||
continue;
|
||||
}
|
||||
result.put(dataLine.substring(0, index), dataLine.substring(index + 1));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* With relatively few usage scenarios, only simple parsing is performed to reduce jar
|
||||
* dependencies.
|
||||
*
|
||||
* @author zkz
|
||||
*/
|
||||
public class NacosDataXmlParser extends AbstractNacosDataParser {
|
||||
|
||||
public NacosDataXmlParser() {
|
||||
super("xml");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> doParse(String data) throws IOException {
|
||||
if (StringUtils.isEmpty(data)) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> map = parseXml2Map(data);
|
||||
return this.reloadMap(map);
|
||||
}
|
||||
|
||||
private Map<String, Object> parseXml2Map(String xml) throws IOException {
|
||||
xml = xml.replaceAll("\\r", "").replaceAll("\\n", "").replaceAll("\\t", "");
|
||||
Map<String, Object> map = new LinkedHashMap<>(32);
|
||||
try {
|
||||
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder();
|
||||
Document document = documentBuilder
|
||||
.parse(new InputSource(new StringReader(xml)));
|
||||
if (null == document) {
|
||||
return null;
|
||||
}
|
||||
parseNodeList(document.getChildNodes(), map, "");
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IOException("The xml content parse error.", e.getCause());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private void parseNodeList(NodeList nodeList, Map<String, Object> map,
|
||||
String parentKey) {
|
||||
if (nodeList == null || nodeList.getLength() < 1) {
|
||||
return;
|
||||
}
|
||||
parentKey = parentKey == null ? "" : parentKey;
|
||||
for (int i = 0; i < nodeList.getLength(); i++) {
|
||||
Node node = nodeList.item(i);
|
||||
String value = node.getNodeValue();
|
||||
value = value == null ? "" : value.trim();
|
||||
String name = node.getNodeName();
|
||||
name = name == null ? "" : name.trim();
|
||||
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String key = StringUtils.isEmpty(parentKey) ? name : parentKey + DOT + name;
|
||||
NamedNodeMap nodeMap = node.getAttributes();
|
||||
parseNodeAttr(nodeMap, map, key);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) {
|
||||
parseNodeList(node.getChildNodes(), map, key);
|
||||
continue;
|
||||
}
|
||||
if (value.length() < 1) {
|
||||
continue;
|
||||
}
|
||||
map.put(parentKey, value);
|
||||
}
|
||||
}
|
||||
|
||||
private void parseNodeAttr(NamedNodeMap nodeMap, Map<String, Object> map,
|
||||
String parentKey) {
|
||||
if (null == nodeMap || nodeMap.getLength() < 1) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < nodeMap.getLength(); i++) {
|
||||
Node node = nodeMap.item(i);
|
||||
if (null == node) {
|
||||
continue;
|
||||
}
|
||||
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
|
||||
if (StringUtils.isEmpty(node.getNodeName())) {
|
||||
continue;
|
||||
}
|
||||
if (StringUtils.isEmpty(node.getNodeValue())) {
|
||||
continue;
|
||||
}
|
||||
map.put(String.join(DOT, parentKey, node.getNodeName()),
|
||||
node.getNodeValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.parser;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.config.YamlMapFactoryBean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
/**
|
||||
* @author zkz
|
||||
*/
|
||||
public class NacosDataYamlParser extends AbstractNacosDataParser {
|
||||
|
||||
public NacosDataYamlParser() {
|
||||
super(",yml,yaml,");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> doParse(String data) {
|
||||
YamlMapFactoryBean yamlFactory = new YamlMapFactoryBean();
|
||||
yamlFactory.setResources(new ByteArrayResource(data.getBytes()));
|
||||
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
flattenedMap(result, yamlFactory.getObject(), EMPTY_STRING);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.refresh;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySource;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.cloud.endpoint.event.RefreshEvent;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
/**
|
||||
* On application start up, NacosContextRefresher add nacos listeners to all application
|
||||
* level dataIds, when there is a change in the data, listeners will refresh
|
||||
* configurations.
|
||||
*
|
||||
* @author juven.xuxb
|
||||
* @author pbting
|
||||
*/
|
||||
public class NacosContextRefresher
|
||||
implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware {
|
||||
|
||||
private final static Logger log = LoggerFactory
|
||||
.getLogger(NacosContextRefresher.class);
|
||||
|
||||
private static final AtomicLong REFRESH_COUNT = new AtomicLong(0);
|
||||
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
private final boolean isRefreshEnabled;
|
||||
|
||||
private final NacosRefreshHistory nacosRefreshHistory;
|
||||
|
||||
private final ConfigService configService;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private AtomicBoolean ready = new AtomicBoolean(false);
|
||||
|
||||
private Map<String, Listener> listenerMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
public NacosContextRefresher(NacosConfigManager nacosConfigManager,
|
||||
NacosRefreshHistory refreshHistory) {
|
||||
this.nacosConfigProperties = nacosConfigManager.getNacosConfigProperties();
|
||||
this.nacosRefreshHistory = refreshHistory;
|
||||
this.configService = nacosConfigManager.getConfigService();
|
||||
this.isRefreshEnabled = this.nacosConfigProperties.isRefreshEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use
|
||||
* {@link NacosContextRefresher#NacosContextRefresher(NacosConfigManager, NacosRefreshHistory)}.
|
||||
* @param refreshProperties refreshProperties
|
||||
* @param refreshHistory refreshHistory
|
||||
* @param configService configService
|
||||
*/
|
||||
@Deprecated
|
||||
public NacosContextRefresher(NacosRefreshProperties refreshProperties,
|
||||
NacosRefreshHistory refreshHistory, ConfigService configService) {
|
||||
this.isRefreshEnabled = refreshProperties.isEnabled();
|
||||
this.nacosRefreshHistory = refreshHistory;
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||
// many Spring context
|
||||
if (this.ready.compareAndSet(false, true)) {
|
||||
this.registerNacosListenersForApplications();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* register Nacos Listeners.
|
||||
*/
|
||||
private void registerNacosListenersForApplications() {
|
||||
if (isRefreshEnabled()) {
|
||||
for (NacosPropertySource propertySource : NacosPropertySourceRepository
|
||||
.getAll()) {
|
||||
if (!propertySource.isRefreshable()) {
|
||||
continue;
|
||||
}
|
||||
String dataId = propertySource.getDataId();
|
||||
registerNacosListener(propertySource.getGroup(), dataId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerNacosListener(final String groupKey, final String dataKey) {
|
||||
String key = NacosPropertySourceRepository.getMapKey(dataKey, groupKey);
|
||||
Listener listener = listenerMap.computeIfAbsent(key,
|
||||
lst -> new AbstractSharedListener() {
|
||||
@Override
|
||||
public void innerReceive(String dataId, String group,
|
||||
String configInfo) {
|
||||
refreshCountIncrement();
|
||||
nacosRefreshHistory.addRefreshRecord(dataId, group, configInfo);
|
||||
// todo feature: support single refresh for listening
|
||||
applicationContext.publishEvent(
|
||||
new RefreshEvent(this, null, "Refresh Nacos config"));
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(String.format(
|
||||
"Refresh Nacos config group=%s,dataId=%s,configInfo=%s",
|
||||
group, dataId, configInfo));
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
configService.addListener(dataKey, groupKey, listener);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.warn(String.format(
|
||||
"register fail for nacos listener ,dataId=[%s],group=[%s]", dataKey,
|
||||
groupKey), e);
|
||||
}
|
||||
}
|
||||
|
||||
public NacosConfigProperties getNacosConfigProperties() {
|
||||
return nacosConfigProperties;
|
||||
}
|
||||
|
||||
public NacosContextRefresher setNacosConfigProperties(
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
this.nacosConfigProperties = nacosConfigProperties;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isRefreshEnabled() {
|
||||
if (null == nacosConfigProperties) {
|
||||
return isRefreshEnabled;
|
||||
}
|
||||
// Compatible with older configurations
|
||||
if (nacosConfigProperties.isRefreshEnabled() && !isRefreshEnabled) {
|
||||
return false;
|
||||
}
|
||||
return isRefreshEnabled;
|
||||
}
|
||||
|
||||
public static long getRefreshCount() {
|
||||
return REFRESH_COUNT.get();
|
||||
}
|
||||
|
||||
public static void refreshCountIncrement() {
|
||||
REFRESH_COUNT.incrementAndGet();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.refresh;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class NacosRefreshHistory {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(NacosRefreshHistory.class);
|
||||
|
||||
private static final int MAX_SIZE = 20;
|
||||
|
||||
private final LinkedList<Record> records = new LinkedList<>();
|
||||
|
||||
private final ThreadLocal<DateFormat> DATE_FORMAT = ThreadLocal
|
||||
.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
private MessageDigest md;
|
||||
|
||||
public NacosRefreshHistory() {
|
||||
try {
|
||||
md = MessageDigest.getInstance("MD5");
|
||||
}
|
||||
catch (NoSuchAlgorithmException e) {
|
||||
log.error("failed to initialize MessageDigest : ", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* recommend to use
|
||||
* {@link NacosRefreshHistory#addRefreshRecord(java.lang.String, java.lang.String, java.lang.String)}.
|
||||
* @param dataId dataId
|
||||
* @param md5 md5
|
||||
*/
|
||||
@Deprecated
|
||||
public void add(String dataId, String md5) {
|
||||
records.addFirst(
|
||||
new Record(DATE_FORMAT.get().format(new Date()), dataId, "", md5, null));
|
||||
if (records.size() > MAX_SIZE) {
|
||||
records.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
public void addRefreshRecord(String dataId, String group, String data) {
|
||||
records.addFirst(new Record(DATE_FORMAT.get().format(new Date()), dataId, group,
|
||||
md5(data), null));
|
||||
if (records.size() > MAX_SIZE) {
|
||||
records.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
public LinkedList<Record> getRecords() {
|
||||
return records;
|
||||
}
|
||||
|
||||
private String md5(String data) {
|
||||
if (StringUtils.isEmpty(data)) {
|
||||
return null;
|
||||
}
|
||||
if (null == md) {
|
||||
try {
|
||||
md = MessageDigest.getInstance("MD5");
|
||||
}
|
||||
catch (NoSuchAlgorithmException ignored) {
|
||||
return "unable to get md5";
|
||||
}
|
||||
}
|
||||
return new BigInteger(1, md.digest(data.getBytes(StandardCharsets.UTF_8)))
|
||||
.toString(16);
|
||||
}
|
||||
|
||||
static class Record {
|
||||
|
||||
private final String timestamp;
|
||||
|
||||
private final String dataId;
|
||||
|
||||
private final String group;
|
||||
|
||||
private final String md5;
|
||||
|
||||
Record(String timestamp, String dataId, String group, String md5,
|
||||
Map<String, Object> last) {
|
||||
this.timestamp = timestamp;
|
||||
this.dataId = dataId;
|
||||
this.group = group;
|
||||
this.md5 = md5;
|
||||
}
|
||||
|
||||
public String getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public String getMd5() {
|
||||
return md5;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.refresh;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@Deprecated
|
||||
@Component
|
||||
public class NacosRefreshProperties {
|
||||
|
||||
/**
|
||||
* recommend to use {@code NacosConfigProperties#refreshEnabled}
|
||||
* {@link NacosConfigProperties#setRefreshEnabled(boolean)}.
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.config.refresh.enabled:true}")
|
||||
private boolean enabled = true;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,99 @@
|
||||
{
|
||||
"properties": [
|
||||
{
|
||||
"name": "spring.cloud.nacos.server-addr",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "localhost:8848",
|
||||
"description": "nacos server address."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.server-addr",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.server-addr}",
|
||||
"description": "nacos config server address."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.encode",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "UTF-8",
|
||||
"description": "default encode for nacos config content."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.prefix",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.application.name}",
|
||||
"description": "the prefix of dataId, nacos config data meta info. dataId = prefix + '-' + ${spring.active.profile} + `.` + ${spring.cloud.nacos.config.file-extension}."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.file-extension",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "properties",
|
||||
"description": "the suffix of nacos config dataId, also the file extension of config content, only support properties now."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.shared-dataids",
|
||||
"type": "java.lang.String",
|
||||
"description": "the dataids for configurable multiple shared configurations , multiple separated by commas ."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.shared-configs",
|
||||
"type": "java.util.List",
|
||||
"description": "a set of shared configurations .e.g: spring.cloud.nacos.config.shared-configs[0]=xxx ."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.refreshable-dataids",
|
||||
"type": "java.lang.String",
|
||||
"description": "refreshable dataids , multiple separated by commas .Not providing support,the need to refresh is specified by the respective refresh configuration."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.ext-config",
|
||||
"type": "java.util.List",
|
||||
"description": "a set of extended configurations ."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.extension-configs",
|
||||
"type": "java.util.List",
|
||||
"description": "a set of extensional configurations .e.g: spring.cloud.nacos.config.extension-configs[0]=xxx ."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.health.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "the switch for health check, it default enabled(true)."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.refresh-enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "the master switch for refresh configuration, it default opened(true)."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "enable nacos config or not."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.username",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.username}",
|
||||
"description": "nacos config service's userName to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.password",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.password}",
|
||||
"description": "nacos config service's password to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.username",
|
||||
"type": "java.lang.String",
|
||||
"description": "nacos userName to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.password",
|
||||
"type": "java.lang.String",
|
||||
"description": "nacos password to authenticate."
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.alibaba.cloud.nacos.NacosConfigAutoConfiguration,\
|
||||
com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration
|
||||
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
||||
com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer
|
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(
|
||||
classes = NacosConfigPropertiesServerAddressBothLevelTests.TestConfig.class,
|
||||
properties = { "spring.cloud.nacos.config.server-addr=321,321,321,321:8848",
|
||||
"spring.cloud.nacos.server-addr=123.123.123.123:8848" },
|
||||
webEnvironment = RANDOM_PORT)
|
||||
public class NacosConfigPropertiesServerAddressBothLevelTests {
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Test
|
||||
public void testGetServerAddr() {
|
||||
assertThat(properties.getServerAddr()).isEqualTo("321,321,321,321:8848");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(
|
||||
classes = NacosConfigPropertiesServerAddressTopLevelTests.TestConfig.class,
|
||||
properties = { "spring.cloud.nacos.server-addr=123.123.123.123:8848" },
|
||||
webEnvironment = RANDOM_PORT)
|
||||
public class NacosConfigPropertiesServerAddressTopLevelTests {
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Test
|
||||
public void testGetServerAddr() {
|
||||
assertThat(properties.getServerAddr()).isEqualTo("123.123.123.123:8848");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationExtConfigTests.TestConfig.class,
|
||||
properties = { "spring.application.name=myTestService1",
|
||||
"spring.profiles.active=dev,test",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.encode=utf-8",
|
||||
"spring.cloud.nacos.config.timeout=1000",
|
||||
"spring.cloud.nacos.config.file-extension=properties",
|
||||
"spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties",
|
||||
"spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties",
|
||||
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
||||
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
|
||||
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||
"spring.cloud.nacos.config.secretKey=test-secretKey" },
|
||||
webEnvironment = NONE)
|
||||
public class NacosConfigurationExtConfigTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
// when(any(ConfigService.class).getConfig(eq("test-name.properties"),
|
||||
// eq("test-group"), any())).thenReturn("user.name=hello");
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
|
||||
if ("test-name-dev.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user.name=dev";
|
||||
}
|
||||
|
||||
if ("ext-config-common01.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-ext-config1=config1\ntest-ext-config2=config1";
|
||||
}
|
||||
if ("ext-config-common02.properties".equals(args[0])
|
||||
&& "GLOBAL_GROUP".equals(args[1])) {
|
||||
return "test-ext-config2=config2";
|
||||
}
|
||||
|
||||
if ("common1.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common1=common1\ntest-common2=common1";
|
||||
}
|
||||
|
||||
if ("common2.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common2=common2";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private NacosPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertThat(locator).isNotNull();
|
||||
assertThat(properties).isNotNull();
|
||||
|
||||
assertThat("config1").isEqualTo(environment.getProperty("test-ext-config1"));
|
||||
assertThat("config2").isEqualTo(environment.getProperty("test-ext-config2"));
|
||||
assertThat("common1").isEqualTo(environment.getProperty("test-common1"));
|
||||
assertThat("common2").isEqualTo(environment.getProperty("test-common2"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties.Config;
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationNewTest.TestConfig.class, properties = {
|
||||
"spring.application.name=myTestService1", "spring.profiles.active=dev,test",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.namespace=test-namespace",
|
||||
"spring.cloud.nacos.config.encode=utf-8",
|
||||
"spring.cloud.nacos.config.timeout=1000",
|
||||
"spring.cloud.nacos.config.group=test-group",
|
||||
"spring.cloud.nacos.config.name=test-name",
|
||||
"spring.cloud.nacos.config.cluster-name=test-cluster",
|
||||
"spring.cloud.nacos.config.file-extension=properties",
|
||||
"spring.cloud.nacos.config.contextPath=test-contextpath",
|
||||
"spring.cloud.nacos.config.extension-configs[0].data-id=ext-config-common01.properties",
|
||||
"spring.cloud.nacos.config.extension-configs[1].data-id=ext-config-common02.properties",
|
||||
"spring.cloud.nacos.config.extension-configs[1].group=GLOBAL_GROUP",
|
||||
"spring.cloud.nacos.config.shared-configs[0]=common1.properties",
|
||||
"spring.cloud.nacos.config.shared-configs[1]=common2.properties",
|
||||
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
||||
public class NacosConfigurationNewTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
|
||||
if ("test-name-dev.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=dev";
|
||||
}
|
||||
|
||||
if ("ext-config-common01.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-ext-config1=config1\ntest-ext-config2=config1";
|
||||
}
|
||||
if ("ext-config-common02.properties".equals(args[0])
|
||||
&& "GLOBAL_GROUP".equals(args[1])) {
|
||||
return "test-ext-config2=config2";
|
||||
}
|
||||
|
||||
if ("common1.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common1=common1\ntest-common2=common1";
|
||||
}
|
||||
|
||||
if ("common2.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common2=common2";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private NacosPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory refreshHistory;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertThat(locator).isNotNull();
|
||||
assertThat(properties).isNotNull();
|
||||
|
||||
checkoutNacosConfigServerAddr();
|
||||
checkoutNacosConfigNamespace();
|
||||
checkoutNacosConfigClusterName();
|
||||
checkoutNacosConfigAccessKey();
|
||||
checkoutNacosConfigSecrectKey();
|
||||
checkoutNacosConfigName();
|
||||
checkoutNacosConfigGroup();
|
||||
checkoutNacosConfigContextPath();
|
||||
checkoutNacosConfigFileExtension();
|
||||
checkoutNacosConfigTimeout();
|
||||
checkoutNacosConfigEncode();
|
||||
checkShareConfigs();
|
||||
checkExtensionConfigs();
|
||||
|
||||
checkoutEndpoint();
|
||||
checkoutDataLoad();
|
||||
System.out.println(properties.toString());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigServerAddr() {
|
||||
assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8848");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigNamespace() {
|
||||
assertThat(properties.getNamespace()).isEqualTo("test-namespace");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigClusterName() {
|
||||
assertThat(properties.getClusterName()).isEqualTo("test-cluster");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigAccessKey() {
|
||||
assertThat(properties.getAccessKey()).isEqualTo("test-accessKey");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigSecrectKey() {
|
||||
assertThat(properties.getSecretKey()).isEqualTo("test-secretKey");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigContextPath() {
|
||||
assertThat(properties.getContextPath()).isEqualTo("test-contextpath");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigName() {
|
||||
assertThat(properties.getName()).isEqualTo("test-name");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigGroup() {
|
||||
assertThat(properties.getGroup()).isEqualTo("test-group");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigFileExtension() {
|
||||
assertThat(properties.getFileExtension()).isEqualTo("properties");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigTimeout() {
|
||||
assertThat(properties.getTimeout()).isEqualTo(1000);
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigEncode() {
|
||||
assertThat(properties.getEncode()).isEqualTo("utf-8");
|
||||
}
|
||||
|
||||
private void checkoutDataLoad() {
|
||||
assertThat(environment.getProperty("user.name")).isEqualTo("dev");
|
||||
assertThat(environment.getProperty("user.age")).isEqualTo("12");
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties,
|
||||
refreshHistory);
|
||||
Map<String, Object> map = nacosConfigEndpoint.invoke();
|
||||
assertThat(map.get("NacosConfigProperties")).isEqualTo(properties);
|
||||
assertThat(map.get("RefreshHistory")).isEqualTo(refreshHistory.getRecords());
|
||||
}
|
||||
|
||||
private void checkShareConfigs() throws Exception {
|
||||
assertThat(properties.getSharedConfigs() != null);
|
||||
assertThat(properties.getSharedConfigs()).contains(
|
||||
new Config("common1.properties"), new Config("common2.properties"));
|
||||
}
|
||||
|
||||
private void checkExtensionConfigs() throws Exception {
|
||||
assertThat(properties.getExtensionConfigs() != null);
|
||||
assertThat(properties.getExtensionConfigs()).contains(
|
||||
new Config("ext-config-common01.properties"),
|
||||
new Config("ext-config-common02.properties", "GLOBAL_GROUP"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author zkz
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationNoSuffixTest.TestConfig.class, properties = {
|
||||
"spring.application.name=app-no-suffix", "spring.profiles.active=dev",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.namespace=test-namespace",
|
||||
"spring.cloud.nacos.config.encode=utf-8",
|
||||
"spring.cloud.nacos.config.timeout=1000",
|
||||
"spring.cloud.nacos.config.group=test-group",
|
||||
"spring.cloud.nacos.config.name=test-no-suffix-name",
|
||||
"spring.cloud.nacos.config.cluster-name=test-cluster",
|
||||
"spring.cloud.nacos.config.contextPath=test-contextpath",
|
||||
"spring.cloud.nacos.config.ext-config[0].data-id=ext-json-test.json",
|
||||
"spring.cloud.nacos.config.ext-config[1].data-id=ext-common02.properties",
|
||||
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
||||
"spring.cloud.nacos.config.shared-dataids=shared-data1.properties,shared-data2.xml",
|
||||
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
||||
public class NacosConfigurationNoSuffixTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("app-no-suffix".equals(args[0]) && "test-group".equals(args[1])) {
|
||||
return "test-no-suffix=value-no-suffix-1";
|
||||
}
|
||||
if ("app-no-suffix.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "test-no-suffix=value-no-suffix-2";
|
||||
}
|
||||
|
||||
if ("test-no-suffix-name".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "test-no-suffix-assign=assign-value-no-suffix-111";
|
||||
}
|
||||
if ("test-no-suffix-name.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "test-no-suffix-assign=assign-value-no-suffix-222";
|
||||
}
|
||||
if ("test-no-suffix-name-dev.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "test-no-suffix-assign=assign-dev-value-no-suffix-333";
|
||||
}
|
||||
|
||||
if ("ext-json-test.json".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "{\n" + " \"people\":{\n"
|
||||
+ " \"firstName\":\"Brett\",\n"
|
||||
+ " \"lastName\":\"McLaughlin\"\n" + " }\n"
|
||||
+ "}";
|
||||
}
|
||||
|
||||
if ("ext-config-common02.properties".equals(args[0])
|
||||
&& "GLOBAL_GROUP".equals(args[1])) {
|
||||
return "global-ext-config=global-config-value-2";
|
||||
}
|
||||
|
||||
if ("shared-data1.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "shared-name=shared-value-1";
|
||||
}
|
||||
|
||||
if ("shared-data2.xml".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
|
||||
+ " <Service name=\"Catalina\"> \n"
|
||||
+ " <Connector value=\"第二个连接器\"> \n"
|
||||
+ " <open>开启服务</open> \n"
|
||||
+ " <init>初始化一下</init> \n"
|
||||
+ " <process>\n" + " <top>\n"
|
||||
+ " <first>one</first>\n"
|
||||
+ " <sencond value=\"two\">\n"
|
||||
+ " <third>three</third>\n"
|
||||
+ " </sencond>\n"
|
||||
+ " </top>\n" + " </process> \n"
|
||||
+ " <destory>销毁一下</destory> \n"
|
||||
+ " <close>关闭服务</close> \n"
|
||||
+ " </Connector> \n" + " </Service> \n"
|
||||
+ "</Server> ";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private NacosPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory refreshHistory;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertThat(locator).isNotNull();
|
||||
assertThat(properties).isNotNull();
|
||||
|
||||
checkoutNacosConfigServerAddr();
|
||||
checkoutNacosConfigNamespace();
|
||||
checkoutNacosConfigClusterName();
|
||||
checkoutNacosConfigAccessKey();
|
||||
checkoutNacosConfigSecrectKey();
|
||||
checkoutNacosConfigName();
|
||||
checkoutNacosConfigGroup();
|
||||
checkoutNacosConfigContextPath();
|
||||
checkoutNacosConfigFileExtension();
|
||||
checkoutNacosConfigTimeout();
|
||||
checkoutNacosConfigEncode();
|
||||
|
||||
checkoutEndpoint();
|
||||
checkEnvironmentProperties();
|
||||
}
|
||||
|
||||
private void checkEnvironmentProperties() {
|
||||
assertThat(environment.getProperty("test-no-suffix")).isNull();
|
||||
assertThat(environment.getProperty("test-no-suffix-assign"))
|
||||
.isEqualTo("assign-dev-value-no-suffix-333");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigServerAddr() {
|
||||
assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8848");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigNamespace() {
|
||||
assertThat(properties.getNamespace()).isEqualTo("test-namespace");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigClusterName() {
|
||||
assertThat(properties.getClusterName()).isEqualTo("test-cluster");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigAccessKey() {
|
||||
assertThat(properties.getAccessKey()).isEqualTo("test-accessKey");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigSecrectKey() {
|
||||
assertThat(properties.getSecretKey()).isEqualTo("test-secretKey");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigContextPath() {
|
||||
assertThat(properties.getContextPath()).isEqualTo("test-contextpath");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigName() {
|
||||
assertThat(properties.getName()).isEqualTo("test-no-suffix-name");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigGroup() {
|
||||
assertThat(properties.getGroup()).isEqualTo("test-group");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigFileExtension() {
|
||||
assertThat(properties.getFileExtension()).isEqualTo("properties");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigTimeout() {
|
||||
assertThat(properties.getTimeout()).isEqualTo(1000);
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigEncode() {
|
||||
assertThat(properties.getEncode()).isEqualTo("utf-8");
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties,
|
||||
refreshHistory);
|
||||
Map<String, Object> map = nacosConfigEndpoint.invoke();
|
||||
assertThat(properties).isEqualTo(map.get("NacosConfigProperties"));
|
||||
assertThat(refreshHistory.getRecords()).isEqualTo(map.get("RefreshHistory"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,225 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationTests.TestConfig.class, properties = {
|
||||
"spring.application.name=myTestService1", "spring.profiles.active=dev,test",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.namespace=test-namespace",
|
||||
"spring.cloud.nacos.config.encode=utf-8",
|
||||
"spring.cloud.nacos.config.timeout=1000",
|
||||
"spring.cloud.nacos.config.group=test-group",
|
||||
"spring.cloud.nacos.config.name=test-name",
|
||||
"spring.cloud.nacos.config.cluster-name=test-cluster",
|
||||
"spring.cloud.nacos.config.file-extension=properties",
|
||||
"spring.cloud.nacos.config.contextPath=test-contextpath",
|
||||
"spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties",
|
||||
"spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties",
|
||||
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
||||
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
|
||||
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
||||
public class NacosConfigurationTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
|
||||
if ("test-name-dev.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=dev";
|
||||
}
|
||||
|
||||
if ("ext-config-common01.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-ext-config1=config1\ntest-ext-config2=config1";
|
||||
}
|
||||
if ("ext-config-common02.properties".equals(args[0])
|
||||
&& "GLOBAL_GROUP".equals(args[1])) {
|
||||
return "test-ext-config2=config2";
|
||||
}
|
||||
|
||||
if ("common1.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common1=common1\ntest-common2=common1";
|
||||
}
|
||||
|
||||
if ("common2.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common2=common2";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private NacosPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory refreshHistory;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertThat(locator).isNotNull();
|
||||
assertThat(properties).isNotNull();
|
||||
|
||||
checkoutNacosConfigServerAddr();
|
||||
checkoutNacosConfigNamespace();
|
||||
checkoutNacosConfigClusterName();
|
||||
checkoutNacosConfigAccessKey();
|
||||
checkoutNacosConfigSecrectKey();
|
||||
checkoutNacosConfigName();
|
||||
checkoutNacosConfigGroup();
|
||||
checkoutNacosConfigContextPath();
|
||||
checkoutNacosConfigFileExtension();
|
||||
checkoutNacosConfigTimeout();
|
||||
checkoutNacosConfigEncode();
|
||||
|
||||
checkoutEndpoint();
|
||||
checkoutDataLoad();
|
||||
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigServerAddr() {
|
||||
assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8848");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigNamespace() {
|
||||
assertThat(properties.getNamespace()).isEqualTo("test-namespace");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigClusterName() {
|
||||
assertThat(properties.getClusterName()).isEqualTo("test-cluster");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigAccessKey() {
|
||||
assertThat(properties.getAccessKey()).isEqualTo("test-accessKey");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigSecrectKey() {
|
||||
assertThat(properties.getSecretKey()).isEqualTo("test-secretKey");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigContextPath() {
|
||||
assertThat(properties.getContextPath()).isEqualTo("test-contextpath");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigName() {
|
||||
assertThat(properties.getName()).isEqualTo("test-name");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigGroup() {
|
||||
assertThat(properties.getGroup()).isEqualTo("test-group");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigFileExtension() {
|
||||
assertThat(properties.getFileExtension()).isEqualTo("properties");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigTimeout() {
|
||||
assertThat(properties.getTimeout()).isEqualTo(1000);
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigEncode() {
|
||||
assertThat(properties.getEncode()).isEqualTo("utf-8");
|
||||
}
|
||||
|
||||
private void checkoutDataLoad() {
|
||||
assertThat(environment.getProperty("user.name")).isEqualTo("dev");
|
||||
assertThat(environment.getProperty("user.age")).isEqualTo("12");
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties,
|
||||
refreshHistory);
|
||||
Map<String, Object> map = nacosConfigEndpoint.invoke();
|
||||
assertThat(map.get("NacosConfigProperties")).isEqualTo(properties);
|
||||
assertThat(map.get("RefreshHistory")).isEqualTo(refreshHistory.getRecords());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,281 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint;
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author zkz
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class, properties = {
|
||||
"spring.application.name=xmlApp", "spring.profiles.active=dev",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.namespace=test-namespace",
|
||||
"spring.cloud.nacos.config.encode=utf-8",
|
||||
"spring.cloud.nacos.config.timeout=1000",
|
||||
"spring.cloud.nacos.config.group=test-group",
|
||||
"spring.cloud.nacos.config.name=test-name",
|
||||
"spring.cloud.nacos.config.cluster-name=test-cluster",
|
||||
"spring.cloud.nacos.config.file-extension=xml",
|
||||
"spring.cloud.nacos.config.contextPath=test-contextpath",
|
||||
"spring.cloud.nacos.config.ext-config[0].data-id=ext-json-test.json",
|
||||
"spring.cloud.nacos.config.ext-config[1].data-id=ext-common02.properties",
|
||||
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
||||
"spring.cloud.nacos.config.shared-dataids=shared-data1.properties,shared-data.json",
|
||||
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
||||
public class NacosConfigurationXmlJsonTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("xmlApp.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
||||
return "<top>\n" + " <first>one</first>\n"
|
||||
+ " <sencond value=\"two\">\n"
|
||||
+ " <third>three</third>\n" + " </sencond>\n"
|
||||
+ "</top>";
|
||||
}
|
||||
if ("test-name.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
||||
return "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
|
||||
+ " <Service name=\"Catalina\"> \n"
|
||||
+ " <Connector value=\"第二个连接器\"> \n"
|
||||
+ " <open>开启服务</open> \n"
|
||||
+ " <init>初始化一下</init> \n"
|
||||
+ " <process>\n" + " <top>\n"
|
||||
+ " <first>one</first>\n"
|
||||
+ " <sencond value=\"two\">\n"
|
||||
+ " <third>three</third>\n"
|
||||
+ " </sencond>\n"
|
||||
+ " </top>\n" + " </process> \n"
|
||||
+ " <destory>销毁一下</destory> \n"
|
||||
+ " <close>关闭服务</close> \n"
|
||||
+ " </Connector> \n" + " </Service> \n"
|
||||
+ "</Server> ";
|
||||
}
|
||||
|
||||
if ("test-name-dev.xml".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
|
||||
+ " <activity android:name=\".osgViewer\"\n"
|
||||
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
|
||||
+ " <intent-filter>\n"
|
||||
+ " <action android:name=\"android.intent.action.MAIN\" />\n"
|
||||
+ " <category android:name=\"android.intent.category.LAUNCHER\" />\n"
|
||||
+ " </intent-filter>\n" + " </activity>\n"
|
||||
+ "</application>";
|
||||
}
|
||||
|
||||
if ("ext-json-test.json".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "{\n" + " \"people\":{\n"
|
||||
+ " \"firstName\":\"Brett\",\n"
|
||||
+ " \"lastName\":\"McLaughlin\"\n" + " }\n"
|
||||
+ "}";
|
||||
}
|
||||
|
||||
if ("ext-config-common02.properties".equals(args[0])
|
||||
&& "GLOBAL_GROUP".equals(args[1])) {
|
||||
return "global-ext-config=global-config-value-2";
|
||||
}
|
||||
|
||||
if ("shared-data1.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "shared-name=shared-value-1";
|
||||
}
|
||||
|
||||
if ("shared-data.json".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "{\n" + " \"test\" : {\n"
|
||||
+ " \"name\" : \"test\",\n"
|
||||
+ " \"list\" : [\n" + " {\n"
|
||||
+ " \"name\" :\"listname1\",\n"
|
||||
+ " \"age\":1\n" + " },\n"
|
||||
+ " {\n"
|
||||
+ " \"name\" :\"listname2\",\n"
|
||||
+ " \"age\":2\n" + " }\n"
|
||||
+ " ],\n" + " \"metadata\" : {\n"
|
||||
+ " \"intKey\" : 123,\n"
|
||||
+ " \"booleanKey\" : true\n" + " }\n"
|
||||
+ " }\n" + "}";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private NacosPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory refreshHistory;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertThat(locator).isNotNull();
|
||||
assertThat(properties).isNotNull();
|
||||
|
||||
checkoutNacosConfigServerAddr();
|
||||
checkoutNacosConfigNamespace();
|
||||
checkoutNacosConfigClusterName();
|
||||
checkoutNacosConfigAccessKey();
|
||||
checkoutNacosConfigSecrectKey();
|
||||
checkoutNacosConfigName();
|
||||
checkoutNacosConfigGroup();
|
||||
checkoutNacosConfigContextPath();
|
||||
checkoutNacosConfigFileExtension();
|
||||
checkoutNacosConfigTimeout();
|
||||
checkoutNacosConfigEncode();
|
||||
|
||||
checkoutEndpoint();
|
||||
|
||||
checkJsonParser();
|
||||
}
|
||||
|
||||
private void checkJsonParser() {
|
||||
assertThat(environment.getProperty("test.name", String.class)).isEqualTo("test");
|
||||
|
||||
assertThat(environment.getProperty("test.list[0].name", String.class))
|
||||
.isEqualTo("listname1");
|
||||
assertThat(environment.getProperty("test.list[0].age", Integer.class))
|
||||
.isEqualTo(1);
|
||||
|
||||
assertThat(environment.getProperty("test.list[1].name", String.class))
|
||||
.isEqualTo("listname2");
|
||||
assertThat(environment.getProperty("test.list[1].age", Integer.class))
|
||||
.isEqualTo(2);
|
||||
|
||||
assertThat(
|
||||
(Integer) environment.getProperty("test.metadata.intKey", Object.class))
|
||||
.isEqualTo(123);
|
||||
assertThat((Boolean) environment.getProperty("test.metadata.booleanKey",
|
||||
Object.class)).isEqualTo(true);
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigServerAddr() {
|
||||
assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8848");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigNamespace() {
|
||||
assertThat(properties.getNamespace()).isEqualTo("test-namespace");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigClusterName() {
|
||||
assertThat(properties.getClusterName()).isEqualTo("test-cluster");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigAccessKey() {
|
||||
assertThat(properties.getAccessKey()).isEqualTo("test-accessKey");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigSecrectKey() {
|
||||
assertThat(properties.getSecretKey()).isEqualTo("test-secretKey");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigContextPath() {
|
||||
assertThat(properties.getContextPath()).isEqualTo("test-contextpath");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigName() {
|
||||
assertThat(properties.getName()).isEqualTo("test-name");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigGroup() {
|
||||
assertThat(properties.getGroup()).isEqualTo("test-group");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigFileExtension() {
|
||||
assertThat(properties.getFileExtension()).isEqualTo("xml");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigTimeout() {
|
||||
assertThat(properties.getTimeout()).isEqualTo(1000);
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigEncode() {
|
||||
assertThat(properties.getEncode()).isEqualTo("utf-8");
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties,
|
||||
refreshHistory);
|
||||
Map<String, Object> map = nacosConfigEndpoint.invoke();
|
||||
assertThat(properties).isEqualTo(map.get("NacosConfigProperties"));
|
||||
assertThat(refreshHistory.getRecords()).isEqualTo(map.get("RefreshHistory"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosFileExtensionTest.TestConfig.class,
|
||||
properties = { "spring.application.name=test-name",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.file-extension=yaml" },
|
||||
webEnvironment = NONE)
|
||||
public class NacosFileExtensionTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
if ("test-name.yaml".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user:\n name: hello\n age: 12";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
Assert.assertEquals(environment.getProperty("user.name"), "hello");
|
||||
Assert.assertEquals(environment.getProperty("user.age"), "12");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.nacos.endpoint;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration;
|
||||
import com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration;
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigEndpointTests.TestConfig.class,
|
||||
properties = { "spring.application.name=test-name",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.file-extension=properties" },
|
||||
webEnvironment = NONE)
|
||||
public class NacosConfigEndpointTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class,
|
||||
"getServerStatus");
|
||||
MethodProxy.proxy(method, (proxy, method1, args) -> "UP");
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory refreshHistory;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
checkoutEndpoint();
|
||||
checkoutAcmHealthIndicator();
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmHealthIndicator() {
|
||||
try {
|
||||
Builder builder = new Builder();
|
||||
|
||||
NacosConfigHealthIndicator healthIndicator = new NacosConfigHealthIndicator(
|
||||
properties.configServiceInstance());
|
||||
healthIndicator.doHealthCheck(builder);
|
||||
|
||||
Builder builder1 = new Builder();
|
||||
builder1.up();
|
||||
|
||||
assertThat(builder.build()).isEqualTo(builder1.build());
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
NacosConfigEndpoint endpoint = new NacosConfigEndpoint(properties,
|
||||
refreshHistory);
|
||||
Map<String, Object> map = endpoint.invoke();
|
||||
|
||||
assertThat(properties).isEqualTo(map.get("NacosConfigProperties"));
|
||||
assertThat(refreshHistory.getRecords()).isEqualTo(map.get("RefreshHistory"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user