Repository: alibaba/spring-cloud-alibaba
Issue: Setting false on autoRegister of @EnableDiscoveryClient will throw exception and application failed to start

Description:

Field nacosRegistration in org.springframework.cloud.alibaba.nacos.NacosDiscoveryClient required a bean of type 'org.springframework.cloud.alibaba.nacos.registry.NacosRegistration' that could not be found.
	- Bean method 'nacosRegistration' not loaded because @ConditionalOnProperty (spring.cloud.service-registry.auto-registration.enabled) found different value in property 'spring.cloud.service-registry.auto-registration.enabled'

Changed files:
spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java
spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java
spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java
spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java
spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java
spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java
spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java
spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java
spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java
spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java

diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java
index 78c7ddcc..6e087422 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java
@@ -16,7 +16,6 @@
 
 package org.springframework.cloud.alibaba.nacos;
 
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.autoconfigure.AutoConfigureBefore;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -48,12 +47,6 @@ public class NacosDiscoveryAutoConfiguration {
 		return new NacosServiceRegistry();
 	}
 
-	@Bean
-	@ConditionalOnMissingBean
-	public NacosDiscoveryProperties nacosProperties() {
-		return new NacosDiscoveryProperties();
-	}
-
 	@Bean
 	@ConditionalOnBean(AutoServiceRegistrationProperties.class)
 	public NacosRegistration nacosRegistration() {
diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java
index f9acd947..bab4b1a2 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java
@@ -19,13 +19,15 @@ package org.springframework.cloud.alibaba.nacos;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
+import org.springframework.cloud.client.DefaultServiceInstance;
 import org.springframework.cloud.client.ServiceInstance;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
+import org.springframework.core.env.Environment;
 
-import java.net.URI;
 import java.util.*;
 
+import javax.annotation.PostConstruct;
+
 import com.alibaba.nacos.api.naming.NamingService;
 import com.alibaba.nacos.api.naming.pojo.Instance;
 import com.alibaba.nacos.api.naming.pojo.ListView;
@@ -40,52 +42,37 @@ public class NacosDiscoveryClient implements DiscoveryClient {
 	public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client";
 
 	@Autowired
-	private NacosRegistration nacosRegistration;
+	private NacosDiscoveryProperties discoveryProperties;
+
+	@Autowired
+	private Environment environment;
+
+	private NamingService namingService;
 
 	@Override
 	public String description() {
 		return DESCRIPTION;
 	}
 
+	@PostConstruct
+	public void init() {
+		discoveryProperties.overrideFromEnv(environment);
+		namingService = discoveryProperties.getNamingService();
+	}
+
 	@Override
 	public ServiceInstance getLocalServiceInstance() {
-		return new ServiceInstance() {
-			@Override
-			public String getServiceId() {
-				return nacosRegistration.getServiceId();
-			}
-
-			@Override
-			public String getHost() {
-				return nacosRegistration.getHost();
-			}
-
-			@Override
-			public int getPort() {
-				return nacosRegistration.getPort();
-			}
-
-			@Override
-			public boolean isSecure() {
-				return nacosRegistration.isSecure();
-			}
-
-			@Override
-			public URI getUri() {
-				return nacosRegistration.getUri();
-			}
-
-			@Override
-			public Map<String, String> getMetadata() {
-				return nacosRegistration.getMetadata();
-			}
-		};
+		String serviceId = discoveryProperties.getService();
+		String host = discoveryProperties.getIp();
+		int port = discoveryProperties.getPort();
+		boolean secure = discoveryProperties.isSecure();
+		Map<String, String> metadata = discoveryProperties.getMetadata();
+		return new DefaultServiceInstance(serviceId, host, port, secure, metadata);
 	}
 
 	@Override
 	public List<ServiceInstance> getInstances(String serviceId) {
 		try {
-			NamingService namingService = nacosRegistration.getNacosNamingService();
 			List<Instance> instances = namingService.getAllInstances(serviceId);
 			return hostToServiceInstanceList(instances, serviceId);
 		}
@@ -126,7 +113,6 @@ public class NacosDiscoveryClient implements DiscoveryClient {
 	public List<String> getServices() {
 
 		try {
-			NamingService namingService = nacosRegistration.getNacosNamingService();
 			ListView<String> services = namingService.getServicesOfServer(1,
 					Integer.MAX_VALUE);
 			return services.getData();
@@ -137,4 +123,7 @@ public class NacosDiscoveryClient implements DiscoveryClient {
 		}
 	}
 
+	public NamingService getNamingService() {
+		return namingService;
+	}
 }
diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java
index 2563cbe6..05049f4b 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java
@@ -36,4 +36,10 @@ public class NacosDiscoveryClientAutoConfiguration {
 		return new NacosDiscoveryClient();
 	}
 
+	@Bean
+	@ConditionalOnMissingBean
+	public NacosDiscoveryProperties nacosProperties() {
+		return new NacosDiscoveryProperties();
+	}
+
 }
diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java
index 43b6a5c7..aa9271a8 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java
@@ -16,6 +16,8 @@
 
 package org.springframework.cloud.alibaba.nacos;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -33,6 +35,18 @@ import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Properties;
+
+import com.alibaba.nacos.api.NacosFactory;
+import com.alibaba.nacos.api.naming.NamingService;
+import com.alibaba.nacos.client.naming.utils.UtilAndComs;
+
+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.NAMESPACE;
+import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY;
+import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
 
 /**
  * @author dungu.zpf
@@ -42,6 +56,9 @@ import java.util.Objects;
 @ConfigurationProperties("spring.cloud.nacos.discovery")
 public class NacosDiscoveryProperties {
 
+	private static final Logger LOGGER = LoggerFactory
+			.getLogger(NacosDiscoveryProperties.class);
+
 	/**
 	 * nacos discovery server address
 	 */
@@ -333,4 +350,22 @@ public class NacosDiscoveryProperties {
 		}
 	}
 
+	public NamingService getNamingService() {
+		Properties properties = new Properties();
+		properties.put(SERVER_ADDR, serverAddr);
+		properties.put(NAMESPACE, namespace);
+		properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, logName);
+		properties.put(ENDPOINT, endpoint);
+		properties.put(ACCESS_KEY, accessKey);
+		properties.put(SECRET_KEY, secretKey);
+		properties.put(CLUSTER_NAME, clusterName);
+		try {
+			return NacosFactory.createNamingService(properties);
+		}
+		catch (Exception e) {
+			LOGGER.error("create naming service error!properties={},e=,", this, e);
+			return null;
+		}
+	}
+
 }
diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java
index a68f6300..dfa1fbb6 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java
@@ -28,8 +28,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
+import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClient;
 import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
-import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
 
 /**
  * Endpoint for nacos discovery, get nacos properties and subscribed services
@@ -44,7 +44,7 @@ public class NacosDiscoveryEndpoint extends AbstractEndpoint<Map<String, Object>
 	private NacosDiscoveryProperties nacosDiscoveryProperties;
 
 	@Autowired
-	private NacosRegistration nacosRegistration;
+	private NacosDiscoveryClient discoveryClient;
 
 	public NacosDiscoveryEndpoint() {
 		super("nacos_discovery", false);
@@ -58,7 +58,7 @@ public class NacosDiscoveryEndpoint extends AbstractEndpoint<Map<String, Object>
 		Map<String, Object> result = new HashMap<>();
 		result.put("NacosDiscoveryProperties", nacosDiscoveryProperties);
 
-		NamingService namingService = nacosRegistration.getNacosNamingService();
+		NamingService namingService = discoveryClient.getNamingService();
 		List<ServiceInfo> subscribe = Collections.emptyList();
 
 		try {
diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java
index 5278cd3b..eab95411 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java
@@ -26,7 +26,6 @@ import org.springframework.context.annotation.Configuration;
  * @author xiaojing
  */
 @Configuration
-@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
 @ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.AbstractEndpoint")
 public class NacosDiscoveryEndpointAutoConfiguration {
 
diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java
index 1fc12d7f..872d7dd8 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java
@@ -28,15 +28,10 @@ import org.springframework.util.StringUtils;
 
 import java.net.URI;
 import java.util.Map;
-import java.util.Properties;
 
 import javax.annotation.PostConstruct;
 
-import com.alibaba.nacos.api.NacosFactory;
 import com.alibaba.nacos.api.naming.NamingService;
-import com.alibaba.nacos.client.naming.utils.UtilAndComs;
-
-import static com.alibaba.nacos.api.PropertyKeyConst.*;
 
 /**
  * @author xiaojing
@@ -60,22 +55,7 @@ public class NacosRegistration implements Registration, ServiceInstance {
 
 		Environment env = context.getEnvironment();
 		nacosDiscoveryProperties.overrideFromEnv(context.getEnvironment());
-
-		Properties properties = new Properties();
-		properties.put(SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
-		properties.put(NAMESPACE, nacosDiscoveryProperties.getNamespace());
-		properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME,
-				nacosDiscoveryProperties.getLogName());
-		properties.put(ENDPOINT, nacosDiscoveryProperties.getEndpoint());
-		properties.put(ACCESS_KEY, nacosDiscoveryProperties.getAccessKey());
-		properties.put(SECRET_KEY, nacosDiscoveryProperties.getSecretKey());
-		properties.put(CLUSTER_NAME, nacosDiscoveryProperties.getClusterName());
-		try {
-			nacosNamingService = NacosFactory.createNamingService(properties);
-		}
-		catch (Exception e) {
-
-		}
+		nacosNamingService = nacosDiscoveryProperties.getNamingService();
 
 		Integer managementPort = ManagementServerPortUtils.getPort(context);
 		if (null != managementPort) {
diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java
index 3eb43bbc..c85339c3 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java
@@ -19,7 +19,9 @@ package org.springframework.cloud.alibaba.nacos.ribbon;
 import com.netflix.client.config.IClientConfig;
 import com.netflix.loadbalancer.AbstractServerList;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClient;
 import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
+import org.springframework.cloud.client.discovery.DiscoveryClient;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -32,7 +34,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
 public class NacosServerList extends AbstractServerList<NacosServer> {
 
 	@Autowired
-	private NacosRegistration registration;
+	private NacosDiscoveryClient discoveryClient;
 
 	private String serviceId;
 
@@ -55,7 +57,7 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
 
 	private List<NacosServer> getServers() {
 		try {
-			List<Instance> instances = registration.getNacosNamingService()
+			List<Instance> instances = discoveryClient.getNamingService()
 					.getAllInstances(serviceId);
 			return instancesToServerList(instances);
 		}
diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java
index 1fe9fdaa..4be0dd92 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java
@@ -42,6 +42,7 @@ public class NacosDiscoveryAutoConfigurationTests {
 	@Before
 	public void setUp() throws Exception {
 		this.context = new SpringApplicationBuilder(NacosDiscoveryTestConfiguration.class,
+				NacosDiscoveryClientAutoConfiguration.class,
 				NacosDiscoveryAutoConfiguration.class).web(false).run(
 						"--spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080",
 						"--spring.cloud.nacos.discovery.port=18080",
diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java
index f0354b34..c0f808d9 100644
--- a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java
+++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java
@@ -9,6 +9,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.builder.SpringApplicationBuilder;
 import org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration;
+import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClientAutoConfiguration;
 import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
@@ -33,6 +34,7 @@ public class NacosRibbonClientConfigurationTests {
 	public void setUp() throws Exception {
 		this.context = new SpringApplicationBuilder(NacosRibbonTestConfiguration.class,
 				NacosDiscoveryAutoConfiguration.class,
+				NacosDiscoveryClientAutoConfiguration.class,
 				NacosRibbonClientConfiguration.class, RibbonNacosAutoConfiguration.class)
 						.web(false).run("--server.port=18080",
 								"--spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080",