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 632448dbd..8a664cb48 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 @@ -19,16 +19,13 @@ package com.alibaba.cloud.nacos; import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.Properties; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.PostConstruct; +import com.alibaba.cloud.commons.lang.StringUtils; import com.alibaba.cloud.nacos.event.NacosDiscoveryInfoChangedEvent; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.PreservedMetadataKeys; @@ -44,18 +41,8 @@ import org.springframework.cloud.commons.util.InetUtils; import org.springframework.context.ApplicationEventPublisher; 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.ENDPOINT; -import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT_PORT; -import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE; -import static com.alibaba.nacos.api.PropertyKeyConst.NAMING_LOAD_CACHE_AT_START; -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; + +import static com.alibaba.nacos.api.PropertyKeyConst.*; /** * @author dungu.zpf @@ -63,6 +50,7 @@ import static com.alibaba.nacos.api.PropertyKeyConst.USERNAME; * @author Mercy * @author lyuzb * @author eshun + * @author freeman */ @ConfigurationProperties("spring.cloud.nacos.discovery") public class NacosDiscoveryProperties { @@ -207,6 +195,12 @@ public class NacosDiscoveryProperties { */ private boolean ephemeral = true; + /** + * Whether to enable nacos failure tolerance. If enabled, nacos will return cached + * values when exceptions occur. + */ + private boolean failureToleranceEnabled = true; + @Autowired private InetUtils inetUtils; @@ -486,6 +480,14 @@ public class NacosDiscoveryProperties { this.ephemeral = ephemeral; } + public boolean isFailureToleranceEnabled() { + return failureToleranceEnabled; + } + + public void setFailureToleranceEnabled(boolean failureToleranceEnabled) { + this.failureToleranceEnabled = failureToleranceEnabled; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -495,7 +497,12 @@ public class NacosDiscoveryProperties { return false; } NacosDiscoveryProperties that = (NacosDiscoveryProperties) o; - return Objects.equals(serverAddr, that.serverAddr) + return watchDelay == that.watchDelay && Float.compare(that.weight, weight) == 0 + && registerEnabled == that.registerEnabled && port == that.port + && secure == that.secure && instanceEnabled == that.instanceEnabled + && ephemeral == that.ephemeral + && failureToleranceEnabled == that.failureToleranceEnabled + && Objects.equals(serverAddr, that.serverAddr) && Objects.equals(username, that.username) && Objects.equals(password, that.password) && Objects.equals(endpoint, that.endpoint) @@ -503,8 +510,9 @@ public class NacosDiscoveryProperties { && Objects.equals(logName, that.logName) && Objects.equals(service, that.service) && Objects.equals(clusterName, that.clusterName) - && Objects.equals(group, that.group) && Objects.equals(ip, that.ip) - && Objects.equals(port, that.port) + && Objects.equals(group, that.group) + && Objects.equals(namingLoadCacheAtStart, that.namingLoadCacheAtStart) + && Objects.equals(metadata, that.metadata) && Objects.equals(ip, that.ip) && Objects.equals(networkInterface, that.networkInterface) && Objects.equals(accessKey, that.accessKey) && Objects.equals(secretKey, that.secretKey) @@ -517,14 +525,15 @@ public class NacosDiscoveryProperties { public int hashCode() { return Objects.hash(serverAddr, username, password, endpoint, namespace, watchDelay, logName, service, weight, clusterName, group, - namingLoadCacheAtStart, registerEnabled, ip, networkInterface, port, - secure, accessKey, secretKey, heartBeatInterval, heartBeatTimeout, - ipDeleteTimeout, instanceEnabled, ephemeral); + namingLoadCacheAtStart, metadata, registerEnabled, ip, networkInterface, + port, secure, accessKey, secretKey, heartBeatInterval, heartBeatTimeout, + ipDeleteTimeout, instanceEnabled, ephemeral, failureToleranceEnabled); } @Override public String toString() { return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\'' + + ", username='" + username + '\'' + ", password='" + password + '\'' + ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\'' + ", watchDelay=" + watchDelay + ", logName='" + logName + '\'' + ", service='" + service + '\'' + ", weight=" + weight @@ -535,7 +544,9 @@ public class NacosDiscoveryProperties { + ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey + '\'' + ", secretKey='" + secretKey + '\'' + ", heartBeatInterval=" + heartBeatInterval + ", heartBeatTimeout=" + heartBeatTimeout - + ", ipDeleteTimeout=" + ipDeleteTimeout + '}'; + + ", ipDeleteTimeout=" + ipDeleteTimeout + ", instanceEnabled=" + + instanceEnabled + ", ephemeral=" + ephemeral + + ", failureToleranceEnabled=" + failureToleranceEnabled + '}'; } public void overrideFromEnv(Environment env) { diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json index c59be14b3..12a9c531c 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -74,11 +74,5 @@ "type": "java.lang.Boolean", "defaultValue": false, "description": "Integrate LoadBalancer or not." - }, - { - "name": "spring.cloud.nacos.discovery.failure-tolerance-enabled", - "type": "java.lang.Boolean", - "defaultValue": true, - "description": "Whether to enable nacos failure tolerance. If enabled, nacos will return cached values when exceptions occur." } ]}