diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java index 3a546002d..d7f53aa0e 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java @@ -38,9 +38,6 @@ public class NacosConfigManager { public NacosConfigManager(NacosConfigProperties nacosConfigProperties) { this.nacosConfigProperties = nacosConfigProperties; - // Compatible with older code in NacosConfigProperties,It will be deleted in the - // future. - createConfigService(nacosConfigProperties); } /** diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java index 0044f8b1f..b78deb441 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java @@ -101,90 +101,50 @@ public class NacosConfigProperties { @Autowired @JsonIgnore private Environment environment; - - @PostConstruct - public void init() { - this.overrideFromEnv(); - } - - private void overrideFromEnv() { - if (environment == null) { - return; - } - 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:127.0.0.1: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 @@ -193,65 +153,82 @@ private void overrideFromEnv() { * 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; - /** * role name for aliyun ram. */ private String ramRoleName; - /** * 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 sharedConfigs; - /** * a set of extensional configurations .e.g: * spring.cloud.nacos.config.extension-configs[0]=xxx . */ private List extensionConfigs; - /** * the master switch for refresh configuration, it default opened(true). */ private boolean refreshEnabled = true; + @PostConstruct + public void init() { + this.overrideFromEnv(); + } + + private void overrideFromEnv() { + if (environment == null) { + return; + } + 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:127.0.0.1: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:}")); + } + } + // todo sts support public String getServerAddr() { @@ -456,7 +433,7 @@ public void setRefreshEnabled(boolean refreshEnabled) { public String getSharedDataids() { return null == getSharedConfigs() ? null : getSharedConfigs().stream().map(Config::getDataId) - .collect(Collectors.joining(COMMAS)); + .collect(Collectors.joining(COMMAS)); } /** @@ -486,7 +463,7 @@ public void setSharedDataids(String sharedDataids) { public String getRefreshableDataids() { return null == getSharedConfigs() ? null : getSharedConfigs().stream().filter(Config::isRefresh) - .map(Config::getDataId).collect(Collectors.joining(COMMAS)); + .map(Config::getDataId).collect(Collectors.joining(COMMAS)); } /** @@ -565,7 +542,7 @@ public Properties getConfigServiceProperties() { */ public Properties assembleConfigServiceProperties() { Properties properties = new Properties(); - properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, DEFAULT_ADDRESS)); + 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, "")); @@ -591,6 +568,12 @@ public Properties assembleConfigServiceProperties() { } enrichNacosConfigProperties(properties); + + // set default value when serverAddr and endpoint is empty + if (StringUtils.isEmpty(this.serverAddr) && StringUtils.isEmpty(this.endpoint)) { + properties.put(SERVER_ADDR, DEFAULT_ADDRESS); + } + return properties; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosContextRefresher.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosContextRefresher.java index a832bb837..974ec77f7 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosContextRefresher.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/NacosContextRefresher.java @@ -54,14 +54,12 @@ public class NacosContextRefresher .getLogger(NacosContextRefresher.class); private static final AtomicLong REFRESH_COUNT = new AtomicLong(0); - - private NacosConfigProperties nacosConfigProperties; - private final boolean isRefreshEnabled; - private final NacosRefreshHistory nacosRefreshHistory; + private NacosConfigProperties nacosConfigProperties; + private ConfigService configService; - private final ConfigService configService; + private NacosConfigManager configManager; private ApplicationContext applicationContext; @@ -71,12 +69,20 @@ public class NacosContextRefresher public NacosContextRefresher(NacosConfigManager nacosConfigManager, NacosRefreshHistory refreshHistory) { + this.configManager = nacosConfigManager; this.nacosConfigProperties = nacosConfigManager.getNacosConfigProperties(); this.nacosRefreshHistory = refreshHistory; - this.configService = nacosConfigManager.getConfigService(); this.isRefreshEnabled = this.nacosConfigProperties.isRefreshEnabled(); } + public static long getRefreshCount() { + return REFRESH_COUNT.get(); + } + + public static void refreshCountIncrement() { + REFRESH_COUNT.incrementAndGet(); + } + @Override public void onApplicationEvent(ApplicationReadyEvent event) { // many Spring context @@ -127,6 +133,9 @@ public void innerReceive(String dataId, String group, } }); try { + if (configService == null && configManager != null) { + configService = configManager.getConfigService(); + } configService.addListener(dataKey, groupKey, listener); log.info("[Nacos Config] Listening config: dataId={}, group={}", dataKey, groupKey); @@ -159,12 +168,4 @@ public boolean isRefreshEnabled() { return isRefreshEnabled; } - public static long getRefreshCount() { - return REFRESH_COUNT.get(); - } - - public static void refreshCountIncrement() { - REFRESH_COUNT.incrementAndGet(); - } - } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java index 3e385ec8d..91bd43683 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java @@ -28,7 +28,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; - import com.alibaba.cloud.commons.lang.StringUtils; import com.alibaba.cloud.nacos.event.NacosDiscoveryInfoChangedEvent; import com.alibaba.cloud.nacos.util.InetIPv6Utils; @@ -49,7 +48,6 @@ import org.springframework.core.env.Environment; 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.ENDPOINT; import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT_PORT; import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE; @@ -70,14 +68,12 @@ @ConfigurationProperties("spring.cloud.nacos.discovery") public class NacosDiscoveryProperties { - private static final Logger log = LoggerFactory - .getLogger(NacosDiscoveryProperties.class); - /** * Prefix of {@link NacosDiscoveryProperties}. */ public static final String PREFIX = "spring.cloud.nacos.discovery"; - + private static final Logger log = LoggerFactory + .getLogger(NacosDiscoveryProperties.class); private static final Pattern PATTERN = Pattern.compile("-(\\w)"); private static final String IPV4 = "IPv4"; @@ -134,7 +130,7 @@ public class NacosDiscoveryProperties { /** * cluster name for nacos . */ - private String clusterName = "DEFAULT"; + private String clusterName; /** * group name for nacos. @@ -692,7 +688,8 @@ public Properties getNacosProperties() { properties.put(ACCESS_KEY, accessKey); properties.put(SECRET_KEY, secretKey); - properties.put(CLUSTER_NAME, clusterName); + // only used for instance.setClusterName() +// properties.put(CLUSTER_NAME, clusterName); properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart); enrichNacosDiscoveryProperties(properties);