From 879e689390b3779ff9014a2d3e8c5e7824c4a1b9 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Tue, 18 Sep 2018 14:58:50 +0800 Subject: [PATCH 1/5] update NacosAutoServiceRegistration, update pom.xml, and format code --- spring-cloud-alibaba-nacos-discovery/pom.xml | 6 +- .../NacosDiscoveryAutoConfiguration.java | 6 +- .../alibaba/nacos/NacosDiscoveryClient.java | 26 ++++--- .../nacos/NacosDiscoveryProperties.java | 75 +++++++++---------- ...cosDiscoveryEndpointAutoConfiguration.java | 1 - .../NacosAutoServiceRegistration.java | 71 ++++++++++-------- .../nacos/registry/NacosRegistration.java | 19 ++--- .../alibaba/nacos/ribbon/NacosServerList.java | 10 ++- 8 files changed, 113 insertions(+), 101 deletions(-) diff --git a/spring-cloud-alibaba-nacos-discovery/pom.xml b/spring-cloud-alibaba-nacos-discovery/pom.xml index b69c9401..ff061d2f 100644 --- a/spring-cloud-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-alibaba-nacos-discovery/pom.xml @@ -34,7 +34,6 @@ spring-cloud-starter-netflix-ribbon - org.springframework.boot spring-boot-actuator @@ -62,13 +61,12 @@ provided true + org.springframework.boot spring-boot-starter-web - provided - true + test - org.springframework.boot spring-boot-starter-test 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 f629b5bb..78c7ddcc 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 @@ -63,8 +63,10 @@ public class NacosDiscoveryAutoConfiguration { @Bean @ConditionalOnBean(AutoServiceRegistrationProperties.class) public NacosAutoServiceRegistration nacosAutoServiceRegistration( - NacosServiceRegistry registry, AutoServiceRegistrationProperties properties, + NacosServiceRegistry registry, + AutoServiceRegistrationProperties autoServiceRegistrationProperties, NacosRegistration registration) { - return new NacosAutoServiceRegistration(registry, properties, registration); + return new NacosAutoServiceRegistration(registry, + autoServiceRegistrationProperties, registration); } } \ No newline at end of file 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 c1e3eaeb..16d22540 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 @@ -35,7 +35,8 @@ import com.alibaba.nacos.api.naming.pojo.ListView; */ public class NacosDiscoveryClient implements DiscoveryClient { - private static final Logger LOGGER = LoggerFactory.getLogger(NacosDiscoveryClient.class); + private static final Logger LOGGER = LoggerFactory + .getLogger(NacosDiscoveryClient.class); public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client"; @Autowired @@ -85,7 +86,7 @@ public class NacosDiscoveryClient implements DiscoveryClient { public List getInstances(String serviceId) { try { NamingService namingService = nacosRegistration.getNacosNamingService(); - List instances = namingService.selectInstances(serviceId,true); + List instances = namingService.selectInstances(serviceId, true); return hostToServiceInstanceList(instances, serviceId); } catch (Exception e) { @@ -94,25 +95,26 @@ public class NacosDiscoveryClient implements DiscoveryClient { } } - private static ServiceInstance hostToServiceInstance(Instance instance, String serviceId) { + private static ServiceInstance hostToServiceInstance(Instance instance, + String serviceId) { NacosServiceInstance nacosServiceInstance = new NacosServiceInstance(); nacosServiceInstance.setHost(instance.getIp()); nacosServiceInstance.setPort(instance.getPort()); nacosServiceInstance.setServiceId(serviceId); Map metadata = new HashMap(); metadata.put("instanceId", instance.getInstanceId()); - metadata.put("weight", instance.getWeight()+""); - metadata.put("healthy", instance.isHealthy()+""); - metadata.put("cluster", instance.getCluster()+""); + metadata.put("weight", instance.getWeight() + ""); + metadata.put("healthy", instance.isHealthy() + ""); + metadata.put("cluster", instance.getCluster() + ""); metadata.putAll(instance.getMetadata()); nacosServiceInstance.setMetadata(metadata); return nacosServiceInstance; } - private static List hostToServiceInstanceList(List instances, - String serviceId) { + private static List hostToServiceInstanceList( + List instances, String serviceId) { List result = new ArrayList(instances.size()); - for (Instance instance: instances) { + for (Instance instance : instances) { result.add(hostToServiceInstance(instance, serviceId)); } return result; @@ -123,9 +125,11 @@ public class NacosDiscoveryClient implements DiscoveryClient { try { NamingService namingService = nacosRegistration.getNacosNamingService(); - ListView services = namingService.getServicesOfServer(1, Integer.MAX_VALUE); + ListView services = namingService.getServicesOfServer(1, + Integer.MAX_VALUE); return services.getData(); - }catch( Exception e){ + } + catch (Exception e) { LOGGER.error("get service name from nacos server fail,", e); return Collections.emptyList(); } 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 f042e4bc..43b6a5c7 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,7 +16,6 @@ package org.springframework.cloud.alibaba.nacos; -import com.fasterxml.jackson.annotation.JsonIgnore; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -49,7 +48,8 @@ public class NacosDiscoveryProperties { private String serverAddr; /** - * the domain name of a service, through which the server address can be dynamically obtained. + * the domain name of a service, through which the server address can be dynamically + * obtained. */ private String endpoint; @@ -91,8 +91,8 @@ public class NacosDiscoveryProperties { private boolean registerEnabled = true; /** - * The ip address your want to register for your service instance, needn't to set it if - * the auto detect ip works well + * The ip address your want to register for your service instance, needn't to set it + * if the auto detect ip works well. */ private String ip; @@ -123,7 +123,6 @@ public class NacosDiscoveryProperties { private String secretKey; @Autowired - @JsonIgnore private InetUtils inetUtils; @PostConstruct @@ -190,10 +189,6 @@ public class NacosDiscoveryProperties { this.logName = logName; } - public InetUtils getInetUtils() { - return inetUtils; - } - public void setInetUtils(InetUtils inetUtils) { this.inetUtils = inetUtils; } @@ -296,47 +291,45 @@ public class NacosDiscoveryProperties { @Override public String toString() { - return "NacosDiscoveryProperties{" + - "serverAddr='" + serverAddr + '\'' + - ", endpoint='" + endpoint + '\'' + - ", namespace='" + namespace + '\'' + - ", logName='" + logName + '\'' + - ", service='" + service + '\'' + - ", weight=" + weight + - ", clusterName='" + clusterName + '\'' + - ", metadata=" + metadata + - ", registerEnabled=" + registerEnabled + - ", ip='" + ip + '\'' + - ", networkInterface='" + networkInterface + '\'' + - ", port=" + port + - ", secure=" + secure + - ", accessKey='" + accessKey + '\'' + - ", secretKey='" + secretKey + '\'' + - '}'; + return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\'' + + ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\'' + + ", logName='" + logName + '\'' + ", service='" + service + '\'' + + ", weight=" + weight + ", clusterName='" + clusterName + '\'' + + ", metadata=" + metadata + ", registerEnabled=" + registerEnabled + + ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\'' + + ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey + + '\'' + ", secretKey='" + secretKey + '\'' + '}'; } - public void overrideFromEnv(Environment env){ + public void overrideFromEnv(Environment env) { - if(StringUtils.isEmpty(this.getServerAddr())) { - this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}")); + if (StringUtils.isEmpty(this.getServerAddr())) { + this.setServerAddr(env + .resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}")); } - if(StringUtils.isEmpty(this.getNamespace())) { - this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}")); + if (StringUtils.isEmpty(this.getNamespace())) { + this.setNamespace(env + .resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}")); } - if(StringUtils.isEmpty(this.getAccessKey())) { - this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}")); + if (StringUtils.isEmpty(this.getAccessKey())) { + this.setAccessKey(env + .resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}")); } - if(StringUtils.isEmpty(this.getSecretKey())) { - this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}")); + if (StringUtils.isEmpty(this.getSecretKey())) { + this.setSecretKey(env + .resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}")); } - if(StringUtils.isEmpty(this.getLogName())) { - this.setLogName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}")); + if (StringUtils.isEmpty(this.getLogName())) { + this.setLogName( + env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}")); } - if(StringUtils.isEmpty(this.getClusterName())) { - this.setClusterName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.clusterName-name:}")); + if (StringUtils.isEmpty(this.getClusterName())) { + this.setClusterName(env.resolvePlaceholders( + "${spring.cloud.nacos.discovery.clusterName-name:}")); } - if(StringUtils.isEmpty(this.getEndpoint())) { - this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}")); + if (StringUtils.isEmpty(this.getEndpoint())) { + this.setEndpoint( + env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}")); } } 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 b107dc1d..5278cd3b 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 @@ -30,7 +30,6 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.AbstractEndpoint") public class NacosDiscoveryEndpointAutoConfiguration { - @Bean @ConditionalOnMissingBean public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() { diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java index 5db714d8..66df7944 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java @@ -19,12 +19,11 @@ package org.springframework.cloud.alibaba.nacos.registry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; -import org.springframework.context.ApplicationContext; -import org.springframework.context.event.EventListener; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * @author xiaojing @@ -37,20 +36,26 @@ public class NacosAutoServiceRegistration @Autowired private NacosRegistration registration; - @Autowired - private ApplicationContext context; - public NacosAutoServiceRegistration( ServiceRegistry serviceRegistry, - AutoServiceRegistrationProperties properties, + AutoServiceRegistrationProperties autoServiceRegistrationProperties, NacosRegistration registration) { - super(serviceRegistry, properties); + super(serviceRegistry, autoServiceRegistrationProperties); this.registration = registration; } + @Deprecated + public void setPort(int port) { + getPort().set(port); + } + @Override protected NacosRegistration getRegistration() { - return registration; + if (this.registration.getPort() < 0 && this.getPort().get() > 0) { + this.registration.setPort(this.getPort().get()); + } + Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set"); + return this.registration; } @Override @@ -58,6 +63,27 @@ public class NacosAutoServiceRegistration return null; } + @Override + protected void register() { + if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) { + LOGGER.debug("Registration disabled."); + return; + } + if (this.registration.getPort() < 0) { + this.registration.setPort(getPort().get()); + } + super.register(); + } + + @Override + protected void registerManagement() { + if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) { + return; + } + super.registerManagement(); + + } + @Override protected int getConfiguredPort() { return this.getPort().get(); @@ -70,34 +96,19 @@ public class NacosAutoServiceRegistration @Override protected Object getConfiguration() { - return null; + return this.registration.getNacosDiscoveryProperties(); } @Override protected boolean isEnabled() { - return true; - } - - /** - * Register the local service with the {@link ServiceRegistry} - */ - @Override - protected void register() { - this.registration.setPort(this.getPort().get()); - this.getServiceRegistry().register(getRegistration()); + return this.registration.getNacosDiscoveryProperties().isRegisterEnabled(); } @Override - @EventListener(EmbeddedServletContainerInitializedEvent.class) - public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { - if (context.equals(event.getApplicationContext())) { - int localPort = event.getEmbeddedServletContainer().getPort(); - if (this.getPort().get() == 0) { - LOGGER.info("Updating port to " + localPort); - this.getPort().compareAndSet(0, localPort); - start(); - } - } + @SuppressWarnings("deprecation") + protected String getAppName() { + String appName = registration.getNacosDiscoveryProperties().getService(); + return StringUtils.isEmpty(appName) ? super.getAppName() : appName; } } 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 fe895cb2..10620415 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 @@ -34,6 +34,7 @@ import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.client.naming.utils.UtilAndComs; import static com.alibaba.nacos.api.PropertyKeyConst.*; + /** * @author xiaojing */ @@ -54,11 +55,12 @@ public class NacosRegistration implements Registration, ServiceInstance { 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(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()); + properties.put(ACCESS_KEY, nacosDiscoveryProperties.getAccessKey()); + properties.put(SECRET_KEY, nacosDiscoveryProperties.getSecretKey()); + properties.put(CLUSTER_NAME, nacosDiscoveryProperties.getClusterName()); try { nacosNamingService = NacosFactory.createNamingService(properties); } @@ -128,15 +130,14 @@ public class NacosRegistration implements Registration, ServiceInstance { } public void setNacosDiscoveryProperties( - NacosDiscoveryProperties nacosDiscoveryProperties) { + NacosDiscoveryProperties nacosDiscoveryProperties) { this.nacosDiscoveryProperties = nacosDiscoveryProperties; } @Override public String toString() { - return "NacosRegistration{" + - "nacosDiscoveryProperties=" + nacosDiscoveryProperties + - ", nacosNamingService=" + nacosNamingService + - '}'; + return "NacosRegistration{" + "nacosDiscoveryProperties=" + + nacosDiscoveryProperties + ", nacosNamingService=" + nacosNamingService + + '}'; } } 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 0d4fdbb8..59555865 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 @@ -36,7 +36,8 @@ public class NacosServerList extends AbstractServerList { private String serviceId; - public NacosServerList(){} + public NacosServerList() { + } public NacosServerList(String serviceId) { this.serviceId = serviceId; @@ -54,11 +55,14 @@ public class NacosServerList extends AbstractServerList { private List getServers() { try { - List instances = registration.getNacosNamingService().selectInstances(serviceId, true); + List instances = registration.getNacosNamingService() + .selectInstances(serviceId, true); return instancesToServerList(instances); } catch (Exception e) { - throw new IllegalStateException("Can not get service instances from nacos, serviceId=" + serviceId, e); + throw new IllegalStateException( + "Can not get service instances from nacos, serviceId=" + serviceId, + e); } } From 4931aef5fdad74c0da0f567c354a263878a3acbb Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Wed, 19 Sep 2018 11:46:45 +0800 Subject: [PATCH 2/5] remove unnecessary comment --- .../cloud/alibaba/nacos/client/NacosPropertySourceLocator.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index 9f356a29..24c276df 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -84,7 +84,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { configService = NacosFactory.createConfigService(properties); } catch (NacosException e) { - // todo 为什么这里是 checkException,什么时候会抛出,什么时候需要关心 logger.error("create config service error, nacosConfigProperties:{}, ", properties, e); return null; } From 16d2c1b903867e593e51e3670180e6a71e84a8cc Mon Sep 17 00:00:00 2001 From: flystar32 Date: Thu, 20 Sep 2018 11:32:30 +0800 Subject: [PATCH 3/5] update nacos version to 0.2.1-RC and format code --- spring-cloud-alibaba-dependencies/pom.xml | 2 +- .../src/main/resources/bootstrap.properties | 2 +- .../src/main/resources/application.properties | 2 +- .../src/main/resources/application.properties | 2 +- .../client/NacosPropertySourceLocator.java | 19 +++++++++++-------- .../main/resources/META-INF/spring.factories | 2 +- .../alibaba/nacos/ribbon/NacosServer.java | 2 +- .../alibaba/nacos/ribbon/NacosServerList.java | 2 +- 8 files changed, 18 insertions(+), 15 deletions(-) diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 7707c60e..35244bb0 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -18,7 +18,7 @@ 0.1.1 3.1.0 - 0.2.0 + 0.2.1-RC1 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties index aca756fe..8d94d562 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties @@ -1 +1 @@ -spring.cloud.nacos.config.server-addr=127.0.0.1:8080 \ No newline at end of file +spring.cloud.nacos.config.server-addr=127.0.0.1:8848 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/resources/application.properties index bbd50591..1df160fc 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/resources/application.properties @@ -1,4 +1,4 @@ spring.application.name=service-consumer server.port=18083 management.endpoints.web.exposure.include=* -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080 \ No newline at end of file +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/resources/application.properties index 04496402..66100d79 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/resources/application.properties @@ -1,4 +1,4 @@ server.port=18082 spring.application.name=service-provider -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080 +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 management.endpoints.web.exposure.include=* \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index 24c276df..7f9033c1 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -16,7 +16,6 @@ package org.springframework.cloud.alibaba.nacos.client; - import java.util.Properties; import org.slf4j.Logger; @@ -81,13 +80,14 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { Properties properties = getPropertiesFromEnv(env); try { - configService = NacosFactory.createConfigService(properties); + configService = NacosFactory.createConfigService(properties); } catch (NacosException e) { - logger.error("create config service error, nacosConfigProperties:{}, ", properties, e); + logger.error("create config service error, nacosConfigProperties:{}, ", + properties, e); return null; } - + beanFactory.registerSingleton("configService", configService); if (null == configService) { @@ -96,7 +96,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { return null; } long timeout = nacosConfigProperties.getTimeout(); - nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); + nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, + timeout); String applicationName = env.getProperty("spring.application.name"); logger.info("Initialize spring.application.name '" + applicationName + "'."); @@ -112,7 +113,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); - loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix, contentType); + loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix, + contentType); return composite; } @@ -131,8 +133,9 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { } private void loadNacosDataIfPresent(final CompositePropertySource composite, - final String dataId, final String group,String contentType) { - NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, contentType); + final String dataId, final String group, String contentType) { + NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, + contentType); if (ps != null) { composite.addFirstPropertySource(ps); } diff --git a/spring-cloud-alibaba-nacos-config/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-nacos-config/src/main/resources/META-INF/spring.factories index 3d6be049..ea1ce500 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-nacos-config/src/main/resources/META-INF/spring.factories @@ -4,4 +4,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration,\ org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration org.springframework.boot.diagnostics.FailureAnalyzer=\ -org.springframework.cloud.alibaba.nacos..analyzer.NacosConnectionFailureAnalyzerdiagnostics \ No newline at end of file +org.springframework.cloud.alibaba.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServer.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServer.java index 050edc7c..9fbaf231 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServer.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServer.java @@ -37,7 +37,7 @@ public class NacosServer extends Server { this.metaInfo = new MetaInfo() { @Override public String getAppName() { - return instance.getService().getName(); + return instance.serviceName(); } @Override 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 59555865..3eb43bbc 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 @@ -56,7 +56,7 @@ public class NacosServerList extends AbstractServerList { private List getServers() { try { List instances = registration.getNacosNamingService() - .selectInstances(serviceId, true); + .getAllInstances(serviceId); return instancesToServerList(instances); } catch (Exception e) { From 21dfd637b3427e91f7442899f8c85d2eef73d023 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Thu, 20 Sep 2018 11:42:55 +0800 Subject: [PATCH 4/5] update example readme nacos port to 8848 --- .../nacos-example/nacos-config-example/readme-zh.md | 6 +++--- .../nacos-example/nacos-config-example/readme.md | 6 +++--- .../nacos-example/nacos-discovery-example/readme-zh.md | 4 ++-- .../nacos-example/nacos-discovery-example/readme.md | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md index bc79f6f8..d4f26d7f 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md @@ -21,7 +21,7 @@ 2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 地址 - spring.cloud.nacos.config.server-addr=127.0.0.1:8080 + spring.cloud.nacos.config.server-addr=127.0.0.1:8848 3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能 @@ -51,7 +51,7 @@ 3. 在命令行执行如下命令,向 Nacos Server 中添加一条配置。 - curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17" + curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17" **注:你也可以使用其他方式添加,遵循 HTTP API 规范即可,若您使用的 Nacos 版本自带控制台,建议直接使用控制台进行配置** @@ -89,7 +89,7 @@ #### 验证动态刷新 1. 执行如下命令,修改 Nacos Server 端的配置数据 - curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18" + curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18" 2. 在浏览器地址栏输入 `http://127.0.0.1:18084/user`,并点击调转,可以看到应用从 Nacos Server 中获取了最新的数据,age 变成了 18。 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md index 4d72ef94..7c4a614c 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md @@ -21,7 +21,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl 2. Add Nacos server address configurations to file /src/main/resources/bootstrap.properties - spring.cloud.nacos.config.server-addr=127.0.0.1:8080 + spring.cloud.nacos.config.server-addr=127.0.0.1:8848 3. After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh . @RefreshScope @@ -50,7 +50,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl 3. Execute the following command to add a configuration to Nacos Server. - curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17" + curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17" **Note: You can also add it in other ways. If you are using the Nacos version with its own console, it is recommended to configure it directly using the console.** @@ -89,7 +89,7 @@ Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to, #### Dynamic Refresh 1. Run the following command to modify the configuration data on the Nacos Server side. - curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18" + curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18" 2. Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to, We can see that the app got the latest data from Nacos Server and the age becomes 18. diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md index 33d4682c..91d78244 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md @@ -21,7 +21,7 @@ 2. 在应用的 /src/main/resources/application.properties 配置文件中配置 Nacos Server 地址 - spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080 + spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 3. 使用 @EnableDiscoveryClient 注解开启服务注册与发现功能 @@ -70,7 +70,7 @@ ### 验证 #### 查询服务 -在浏览器输入此地址 `http://127.0.0.1:8080/nacos/v1/ns/instances?serviceName=service-provider`,并点击跳转,可以看到服务节点已经成功注册到 Nacos Server。 +在浏览器输入此地址 `http://127.0.0.1:8848/nacos/v1/ns/instances?serviceName=service-provider`,并点击跳转,可以看到服务节点已经成功注册到 Nacos Server。 ![查询服务](https://cdn.nlark.com/lark/0/2018/png/54319/1536986288092-5cf96af9-9a26-466b-85f6-39ad1d92dfdc.png) diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md index cd6e4bcf..9bb926f8 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md @@ -20,7 +20,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl 2. Add Nacos server address configurations to file /src/main/resources/application.properties. - spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080 + spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 3. Use the @EnableDiscoveryClient annotation to turn on service registration and discovery. @@ -72,7 +72,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl #### Query Service -Enter `http://127.0.0.1:8080/nacos/v1/ns/instances?serviceName=service-provider` in the browser address bar and click Go to, we can see that the service node has been successfully registered to Nacos Server. +Enter `http://127.0.0.1:8848/nacos/v1/ns/instances?serviceName=service-provider` in the browser address bar and click Go to, we can see that the service node has been successfully registered to Nacos Server. ![查询服务](https://cdn.nlark.com/lark/0/2018/png/54319/1536986288092-5cf96af9-9a26-466b-85f6-39ad1d92dfdc.png) From bfa8f24cc88ac871624a7147a0baea628af852db Mon Sep 17 00:00:00 2001 From: flystar32 Date: Thu, 20 Sep 2018 14:30:00 +0800 Subject: [PATCH 5/5] update nacos config properties , content-type to file-extension --- .../nacos-config-example/readme-zh.md | 8 +- .../nacos-config-example/readme.md | 8 +- .../nacos-discovery-example/readme-zh.md | 2 +- .../nacos-discovery-example/readme.md | 2 +- .../alibaba/nacos/NacosConfigProperties.java | 85 ++++++++++--------- .../client/NacosPropertySourceBuilder.java | 8 +- .../client/NacosPropertySourceLocator.java | 18 ++-- ...itional-spring-configuration-metadata.json | 6 +- .../NacosConfigAutoConfigurationTests.java | 2 +- 9 files changed, 71 insertions(+), 68 deletions(-) diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md index d4f26d7f..b9ec18f0 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md @@ -112,15 +112,15 @@ Nacos Client 从 Nacos Server 端获取数据时,调用的是此接口 `Config 在 Nacos Config Starter 中,dataId 的拼接格式如下 - ${prefix} - ${spring.active.profile} . ${content-type} + ${prefix} - ${spring.active.profile} . ${file-extension} * `prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。 * `spring.active.profile` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles) - **注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${context.type}`** + **注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${file-extension}`** -* `content-type` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.content-type`来配置。 +* `file-extension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.file-extension`来配置。 目前只支持 `properties` 类型。 #### group @@ -168,7 +168,7 @@ Spring Boot 2.x 可以通过访问 http://127.0.0.1:18084/actuator/nacos-config 服务端地址|spring.cloud.nacos.config.server-addr|| DataId前缀|spring.cloud.nacos.config.prefix||spring.application.name Group|spring.cloud.nacos.config.group|DEFAULT_GROUP| -dataID后缀及数据格式|spring.cloud.nacos.config.content-type|properties|目前只支持 properties +dataID后缀及内容文件格式|spring.cloud.nacos.config.file-extension|properties|dataId的后缀,同时也是配置内容的文件格式,目前只支持 properties 配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码 获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms 配置的命名空间|spring.cloud.nacos.config.namespace||常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md index 7c4a614c..a2f8f3e8 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md @@ -113,15 +113,15 @@ Nacos Client gets data from Nacos Server through this method. `ConfigService.get In Nacos Config Starter, the splicing format of dataId is as follows - ${prefix} - ${spring.active.profile} . ${content-type} + ${prefix} - ${spring.active.profile} . ${file-extension} * `prefix` default value is `spring.application.name` value, which can also be configured via the configuration item `spring.cloud.nacos.config.prefix`. * `spring.active.profile` is the profile corresponding to the current environment. For details, please refer to [Spring Boot Doc](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles) - **Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${context.type}`** + **Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${file-extension}`** -* `content-type` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.content-type`. +* `file-extension` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.file-extension`. Currently only the `properties` type is supported. #### group @@ -172,7 +172,7 @@ Configuration item|key|default value|Description server address|spring.cloud.nacos.config.server-addr|| DataId prefix|spring.cloud.nacos.config.prefix||spring.application.name Group|spring.cloud.nacos.config.group|DEFAULT_GROUP| -dataID content type|spring.cloud.nacos.config.content-type|properties|currently only support properties +dataID suffix|spring.cloud.nacos.config.file-extension|properties|the suffix of nacos config dataId, also the file extension of config content. encoding |spring.cloud.nacos.config.encode|UTF-8|Content encoding timeout|spring.cloud.nacos.config.timeout|3000|Get the configuration timeout period,unit is ms namespace|spring.cloud.nacos.config.namespace||One of the common scenarios is the separation of the configuration of different environments, such as the development of the test environment and the resource isolation of the production environment. diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md index 91d78244..7be882bd 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md @@ -197,7 +197,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key|| SecretKey|spring.cloud.nacos.discovery.secret-key|| Metadata|spring.cloud.nacos.discovery.metadata||使用Map格式配置 日志文件名|spring.cloud.nacos.discovery.log-name|| -接入点|spring.cloud.nacos.discovery.enpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址 +接入点|spring.cloud.nacos.discovery.endpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址 是否集成Ribbon|ribbon.nacos.enabled|true| diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md index 9bb926f8..7136933d 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md @@ -205,7 +205,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key|| SecretKey|spring.cloud.nacos.discovery.secret-key|| Metadata|spring.cloud.nacos.discovery.metadata||Extended data, Configure using Map format log name|spring.cloud.nacos.discovery.log-name|| -endpoint|spring.cloud.nacos.discovery.enpoint||The domain name of a service, through which the server address can be dynamically obtained. +endpoint|spring.cloud.nacos.discovery.endpoint||The domain name of a service, through which the server address can be dynamically obtained. Integration Ribbon|ribbon.nacos.enabled|true| diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java index f445ffe7..446483d6 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java @@ -49,9 +49,9 @@ public class NacosConfigProperties { */ private String prefix; /** - * the content type of nacos config content. + * the suffix of nacos config dataId, also the file extension of config content. */ - private String contentType = "properties"; + private String fileExtension = "properties"; /** * timeout for get config from nacos. @@ -59,7 +59,8 @@ public class NacosConfigProperties { private int timeout = 3000; /** - * endpoint for Nacos, the domain name of a service, through which the server address can be dynamically obtained. + * endpoint for Nacos, the domain name of a service, through which the server address + * can be dynamically obtained. */ private String endpoint; @@ -88,7 +89,7 @@ public class NacosConfigProperties { */ private String clusterName; - //todo sts support + // todo sts support public String getServerAddr() { return serverAddr; @@ -106,12 +107,12 @@ public class NacosConfigProperties { this.prefix = prefix; } - public String getContentType() { - return contentType; + public String getFileExtension() { + return fileExtension; } - public void setContentType(String contentType) { - this.contentType = contentType; + public void setFileExtension(String fileExtension) { + this.fileExtension = fileExtension; } public String getGroup() { @@ -188,50 +189,52 @@ public class NacosConfigProperties { @Override public String toString() { - return "NacosConfigProperties{" + - "serverAddr='" + serverAddr + '\'' + - ", encode='" + encode + '\'' + - ", group='" + group + '\'' + - ", prefix='" + prefix + '\'' + - ", contentType='" + contentType + '\'' + - ", timeout=" + timeout + - ", endpoint='" + endpoint + '\'' + - ", namespace='" + namespace + '\'' + - ", accessKey='" + accessKey + '\'' + - ", secretKey='" + secretKey + '\'' + - ", contextPath='" + contextPath + '\'' + - ", clusterName='" + clusterName + '\'' + - '}'; + return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\'' + + ", encode='" + encode + '\'' + ", group='" + group + '\'' + ", prefix='" + + prefix + '\'' + ", fileExtension='" + fileExtension + '\'' + + ", timeout=" + timeout + ", endpoint='" + endpoint + '\'' + + ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\'' + + ", secretKey='" + secretKey + '\'' + ", contextPath='" + contextPath + + '\'' + ", clusterName='" + clusterName + '\'' + '}'; } - public void overrideFromEnv(Environment env){ + public void overrideFromEnv(Environment env) { - if(StringUtils.isEmpty(this.getServerAddr())) { - this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}")); + if (StringUtils.isEmpty(this.getServerAddr())) { + this.setServerAddr( + env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}")); } - if(StringUtils.isEmpty(this.getEncode())) { - this.setEncode(env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}")); + if (StringUtils.isEmpty(this.getEncode())) { + this.setEncode( + env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}")); } - if(StringUtils.isEmpty(this.getNamespace())) { - this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}")); + if (StringUtils.isEmpty(this.getNamespace())) { + this.setNamespace( + env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}")); } - if(StringUtils.isEmpty(this.getAccessKey())) { - this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.config.access-key:}")); + if (StringUtils.isEmpty(this.getAccessKey())) { + this.setAccessKey( + env.resolvePlaceholders("${spring.cloud.nacos.config.access-key:}")); } - if(StringUtils.isEmpty(this.getSecretKey())) { - this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.config.secret-key:}")); + if (StringUtils.isEmpty(this.getSecretKey())) { + this.setSecretKey( + env.resolvePlaceholders("${spring.cloud.nacos.config.secret-key:}")); } - if(StringUtils.isEmpty(this.getContextPath())) { - this.setContextPath(env.resolvePlaceholders("${spring.cloud.nacos.config.context-path:}")); + if (StringUtils.isEmpty(this.getContextPath())) { + this.setContextPath(env + .resolvePlaceholders("${spring.cloud.nacos.config.context-path:}")); } - if(StringUtils.isEmpty(this.getClusterName())) { - this.setClusterName(env.resolvePlaceholders("${spring.cloud.nacos.config.cluster-name:}")); + if (StringUtils.isEmpty(this.getClusterName())) { + this.setClusterName(env + .resolvePlaceholders("${spring.cloud.nacos.config.cluster-name:}")); } - if(StringUtils.isEmpty(this.getEndpoint())) { - this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}")); + if (StringUtils.isEmpty(this.getEndpoint())) { + this.setEndpoint( + env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}")); } - if(StringUtils.isEmpty(this.getPrefix())) { - this.setPrefix(env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}")); + if (StringUtils.isEmpty(this.getPrefix())) { + this.setPrefix( + env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}")); } } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java index 7a721dd2..5abddd80 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java @@ -69,19 +69,19 @@ public class NacosPropertySourceBuilder { * @param dataId Nacos dataId * @param group Nacos group */ - NacosPropertySource build(String dataId, String group, String contentType) { - Properties p = loadNacosData(dataId, group, contentType); + NacosPropertySource build(String dataId, String group, String fileExtension) { + Properties p = loadNacosData(dataId, group, fileExtension); if (p == null) { return null; } return new NacosPropertySource(dataId, propertiesToMap(p), new Date()); } - private Properties loadNacosData(String dataId, String group, String contentType) { + private Properties loadNacosData(String dataId, String group, String fileExtension) { String data = null; try { data = configService.getConfig(dataId, group, timeout); - // todo add content type yaml support + // todo add file extension yaml support if (!StringUtils.isEmpty(data)) { Properties properties = new Properties(); logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'", diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index 7f9033c1..f507f152 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -108,34 +108,34 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { dataIdPrefix = applicationName; } - String contentType = nacosConfigProperties.getContentType(); + String fileExtension = nacosConfigProperties.getFileExtension(); CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix, - contentType); + fileExtension); return composite; } private void loadApplicationConfiguration( CompositePropertySource compositePropertySource, Environment environment, - String nacosGroup, String dataIdPrefix, String contentType) { - loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + contentType, - nacosGroup, contentType); + String nacosGroup, String dataIdPrefix, String fileExtension) { + loadNacosDataIfPresent(compositePropertySource, + dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension); for (String profile : environment.getActiveProfiles()) { - String dataId = dataIdPrefix + SEP1 + profile + DOT + contentType; + String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, - contentType); + fileExtension); } // todo multi profile active order and priority } private void loadNacosDataIfPresent(final CompositePropertySource composite, - final String dataId, final String group, String contentType) { + final String dataId, final String group, String fileExtension) { NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, - contentType); + fileExtension); if (ps != null) { composite.addFirstPropertySource(ps); } diff --git a/spring-cloud-alibaba-nacos-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alibaba-nacos-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 189ae565..60fd7f47 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-alibaba-nacos-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -9,12 +9,12 @@ "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.content-type}." + "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.content-type", + "name": "spring.cloud.nacos.config.file-extension", "type": "java.lang.String", "defaultValue": "properties", - "description": "the content type of nacos config content, only support properties now." + "description": "the suffix of nacos config dataId, also the file extension of config content, only support properties now." } ]} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java index b3907b67..0b3d8263 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java @@ -71,7 +71,7 @@ public class NacosConfigAutoConfigurationTests { } NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class); - assertThat(nacosConfigProperties.getContentType()).isEqualTo("properties"); + assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp"); }