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 b6180144..21a8d303 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
@@ -48,12 +48,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 d32a6b0d..59ea1ec7 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,12 +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.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;
@@ -39,7 +42,18 @@ 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;
+
+	@PostConstruct
+	public void init() {
+		discoveryProperties.overrideFromEnv(environment);
+		namingService = discoveryProperties.getNamingService();
+	}
 
 	@Override
 	public String description() {
@@ -49,7 +63,6 @@ public class NacosDiscoveryClient implements DiscoveryClient {
 	@Override
 	public List<ServiceInstance> getInstances(String serviceId) {
 		try {
-			NamingService namingService = nacosRegistration.getNacosNamingService();
 			List<Instance> instances = namingService.getAllInstances(serviceId);
 			return hostToServiceInstanceList(instances, serviceId);
 		}
@@ -79,7 +92,7 @@ public class NacosDiscoveryClient implements DiscoveryClient {
 			List<Instance> instances, String serviceId) {
 		List<ServiceInstance> result = new ArrayList<ServiceInstance>(instances.size());
 		for (Instance instance : instances) {
-			if(instance.isHealthy()) {
+			if (instance.isHealthy()) {
 				result.add(hostToServiceInstance(instance, serviceId));
 			}
 		}
@@ -90,7 +103,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();
@@ -101,4 +113,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 da49d6c2..db384df5 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
@@ -29,8 +29,8 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
 import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
+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
@@ -46,7 +46,7 @@ public class NacosDiscoveryEndpoint {
 	private NacosDiscoveryProperties nacosDiscoveryProperties;
 
 	@Autowired
-	private NacosRegistration nacosRegistration;
+	private NacosDiscoveryClient discoveryClient;
 
 	/**
 	 * @return nacos discovery endpoint
@@ -56,7 +56,7 @@ public class NacosDiscoveryEndpoint {
 		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 2b6c4635..c8716ad4 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
@@ -28,7 +28,6 @@ import org.springframework.context.annotation.Configuration;
  * @author xiaojing
  */
 @Configuration
-@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
 @ConditionalOnClass(Endpoint.class)
 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 4d923fac..639dad98 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..26145662 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,7 @@ 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.registry.NacosRegistration;
+import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClient;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -32,7 +32,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 +55,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 b7e5d801..3d15a8fc 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
@@ -37,7 +37,8 @@ public class NacosDiscoveryAutoConfigurationTests {
 	private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
 			.withConfiguration(
 					AutoConfigurations.of(NacosDiscoveryTestConfiguration.class,
-							NacosDiscoveryAutoConfiguration.class))
+							NacosDiscoveryAutoConfiguration.class,
+							NacosDiscoveryClientAutoConfiguration.class))
 			.withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080")
 			.withPropertyValues("spring.cloud.nacos.discovery.port=18080")
 			.withPropertyValues("spring.cloud.nacos.discovery.service=myapp");
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 8c6da429..ad5da1b2 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
@@ -6,6 +6,7 @@ import org.junit.Test;
 import org.springframework.boot.autoconfigure.AutoConfigurations;
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
+import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClientAutoConfiguration;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 import org.springframework.context.annotation.Bean;
@@ -19,49 +20,43 @@ import static org.assertj.core.api.Assertions.assertThat;
  */
 public class NacosRibbonClientConfigurationTests {
 
-
-    private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
-        .withConfiguration(
-            AutoConfigurations.of(NacosRibbonTestConfiguration.class,
-                NacosRibbonClientConfiguration.class,
-                RibbonNacosAutoConfiguration.class))
-        .withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080")
-        .withPropertyValues("spring.cloud.nacos.discovery.port=18080")
-        .withPropertyValues("spring.cloud.nacos.discovery.service=myapp");
-
-
-
-    @Test
-    public void testProperties() {
-
-        this.contextRunner.run(context -> {
-            NacosServerList serverList = context.getBean(NacosServerList.class);
-            assertThat(serverList.getServiceId()).isEqualTo("myapp");
-        });
-    }
-
-
-    @Configuration
-    @EnableAutoConfiguration
-    @EnableDiscoveryClient
-    static class NacosRibbonTestConfiguration {
-
-
-        @Bean
-        IClientConfig iClientConfig(){
-            //return new IClientConfig.Builder().s.build();
-            DefaultClientConfigImpl config = new DefaultClientConfigImpl();
-            config.setClientName("myapp");
-            return config;
-        }
-
-
-        @Bean
-        @LoadBalanced
-        RestTemplate restTemplate(){
-            return new RestTemplate();
-        }
-
-    }
+	private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
+			.withConfiguration(AutoConfigurations.of(NacosRibbonTestConfiguration.class,
+					NacosRibbonClientConfiguration.class,
+					NacosDiscoveryClientAutoConfiguration.class,
+					RibbonNacosAutoConfiguration.class))
+			.withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080")
+			.withPropertyValues("spring.cloud.nacos.discovery.port=18080")
+			.withPropertyValues("spring.cloud.nacos.discovery.service=myapp");
+
+	@Test
+	public void testProperties() {
+
+		this.contextRunner.run(context -> {
+			NacosServerList serverList = context.getBean(NacosServerList.class);
+			assertThat(serverList.getServiceId()).isEqualTo("myapp");
+		});
+	}
+
+	@Configuration
+	@EnableAutoConfiguration
+	@EnableDiscoveryClient
+	static class NacosRibbonTestConfiguration {
+
+		@Bean
+		IClientConfig iClientConfig() {
+			// return new IClientConfig.Builder().s.build();
+			DefaultClientConfigImpl config = new DefaultClientConfigImpl();
+			config.setClientName("myapp");
+			return config;
+		}
+
+		@Bean
+		@LoadBalanced
+		RestTemplate restTemplate() {
+			return new RestTemplate();
+		}
+
+	}
 
 }