Merge remote-tracking branch 'upstream/master'

# Conflicts:
#	spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java
pull/232/head
gaoyunpeng 6 years ago
commit 8a4a92f03b

@ -50,7 +50,7 @@ Spring Cloud 使用 Maven 来构建最快的使用方式是将本项目clone
## 如何使用
### 如何引入依赖
项目已经发布了第一个版本,版本 0.2.0.RELEASE 对应的是 Spring Cloud Finchley 版本,版本 0.1.0.RELEASE 对应的是 Spring Cloud Edgware 版本。
项目的最新版本是 0.2.1.RELEASE 和 0.1.1.RELEASE版本 0.2.1.RELEASE 对应的是 Spring Cloud Finchley 版本,版本 0.1.1.RELEASE 对应的是 Spring Cloud Edgware 版本。
如果需要使用已发布的版本,在 `dependencyManagement` 中添加如下配置。
@ -59,7 +59,7 @@ Spring Cloud 使用 Maven 来构建最快的使用方式是将本项目clone
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.0.RELEASE</version>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

@ -40,183 +40,184 @@ import java.util.List;
@Order(0)
public class NacosPropertySourceLocator implements PropertySourceLocator {
private static final Logger LOGGER = LoggerFactory
.getLogger(NacosPropertySourceLocator.class);
private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";
private static final String SEP1 = "-";
private static final String DOT = ".";
private static final String SHARED_CONFIG_SEPARATOR_CHAR = "[,]";
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
"yaml", "yml");
@Autowired
private NacosConfigProperties nacosConfigProperties;
public NacosPropertySourceLocator() {
}
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
@Override
public PropertySource<?> locate(Environment env) {
ConfigService configService = nacosConfigProperties.configServiceInstance();
if (null == configService) {
LOGGER.warn(
"no instance of config service found, can't load config from nacos");
return null;
}
long timeout = nacosConfigProperties.getTimeout();
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
timeout);
String name = nacosConfigProperties.getName();
String nacosGroup = nacosConfigProperties.getGroup();
String dataIdPrefix = nacosConfigProperties.getPrefix();
if (StringUtils.isEmpty(dataIdPrefix)) {
dataIdPrefix = name;
}
if (StringUtils.isEmpty(dataIdPrefix)) {
dataIdPrefix = env.getProperty("spring.application.name");
}
List<String> profiles = Arrays.asList(env.getActiveProfiles());
nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0]));
String fileExtension = nacosConfigProperties.getFileExtension();
CompositePropertySource composite = new CompositePropertySource(
NACOS_PROPERTY_SOURCE_NAME);
loadSharedConfiguration(composite);
loadExtConfiguration(composite);
loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension);
return composite;
}
private void loadSharedConfiguration(
CompositePropertySource compositePropertySource) {
String sharedDataIds = nacosConfigProperties.getSharedDataids();
String refreshDataIds = nacosConfigProperties.getRefreshableDataids();
if (sharedDataIds == null || sharedDataIds.trim().length() == 0) {
return;
}
String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR);
checkDataIdFileExtension(sharedDataIdArry);
for (int i = 0; i < sharedDataIdArry.length; i++) {
String dataId = sharedDataIdArry[i];
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds,
sharedDataIdArry[i]);
loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP",
fileExtension, isRefreshable);
}
}
private void loadExtConfiguration(CompositePropertySource compositePropertySource) {
if (nacosConfigProperties.getExtConfig() == null
|| nacosConfigProperties.getExtConfig().isEmpty()) {
return;
}
List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
.getExtConfig();
checkExtConfiguration(extConfigs);
for (NacosConfigProperties.Config config : extConfigs) {
String dataId = config.getDataId();
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
loadNacosDataIfPresent(compositePropertySource, dataId, config.getGroup(),
fileExtension, config.isRefresh());
}
}
private void checkExtConfiguration(List<NacosConfigProperties.Config> extConfigs) {
String[] dataIds = new String[extConfigs.size()];
for (int i = 0; i < extConfigs.size(); i++) {
String dataId = extConfigs.get(i).getDataId();
if (dataId == null || dataId.trim().length() == 0) {
throw new IllegalStateException(String.format(
"the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataid",
i));
}
dataIds[i] = dataId;
}
checkDataIdFileExtension(dataIds);
}
private void loadApplicationConfiguration(
CompositePropertySource compositePropertySource, String nacosGroup,
String dataIdPrefix, String fileExtension) {
loadNacosDataIfPresent(compositePropertySource,
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true);
for (String profile : nacosConfigProperties.getActiveProfiles()) {
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
fileExtension, true);
}
}
private void loadNacosDataIfPresent(final CompositePropertySource composite,
final String dataId, final String group, String fileExtension,
boolean isRefreshable) {
if (NacosContextRefresher.loadCount.get() != 0) {
NacosPropertySource ps;
if (!isRefreshable) {
ps = NacosPropertySourceRepository.getNacosPropertySource(dataId);
} else {
ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true);
}
composite.addFirstPropertySource(ps);
} else {
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
fileExtension, isRefreshable);
composite.addFirstPropertySource(ps);
}
}
private static void checkDataIdFileExtension(String[] sharedDataIdArry) {
StringBuilder stringBuilder = new StringBuilder();
outline:
for (int i = 0; i < sharedDataIdArry.length; i++) {
for (String fileExtension : SUPPORT_FILE_EXTENSION) {
if (sharedDataIdArry[i].indexOf(fileExtension) > 0) {
continue outline;
}
}
stringBuilder.append(sharedDataIdArry[i] + ",");
}
if (stringBuilder.length() > 0) {
String result = stringBuilder.substring(0, stringBuilder.length() - 1);
throw new IllegalStateException(String.format(
"[%s] must contains file extension with properties|yaml|yml",
result));
}
}
private boolean checkDataIdIsRefreshbable(String refreshDataIds,
String sharedDataId) {
if (refreshDataIds == null || "".equals(refreshDataIds)) {
return false;
}
String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR);
for (String refreshDataId : refreshDataIdArry) {
if (refreshDataId.equals(sharedDataId)) {
return true;
}
}
return false;
}
private static final Logger LOGGER = LoggerFactory
.getLogger(NacosPropertySourceLocator.class);
private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";
private static final String SEP1 = "-";
private static final String DOT = ".";
private static final String SHARED_CONFIG_SEPARATOR_CHAR = "[,]";
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
"yaml", "yml");
@Autowired
private NacosConfigProperties nacosConfigProperties;
public NacosPropertySourceLocator() {
}
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
@Override
public PropertySource<?> locate(Environment env) {
ConfigService configService = nacosConfigProperties.configServiceInstance();
if (null == configService) {
LOGGER.warn(
"no instance of config service found, can't load config from nacos");
return null;
}
long timeout = nacosConfigProperties.getTimeout();
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
timeout);
String name = nacosConfigProperties.getName();
String nacosGroup = nacosConfigProperties.getGroup();
String dataIdPrefix = nacosConfigProperties.getPrefix();
if (StringUtils.isEmpty(dataIdPrefix)) {
dataIdPrefix = name;
}
if (StringUtils.isEmpty(dataIdPrefix)) {
dataIdPrefix = env.getProperty("spring.application.name");
}
List<String> profiles = Arrays.asList(env.getActiveProfiles());
nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0]));
String fileExtension = nacosConfigProperties.getFileExtension();
CompositePropertySource composite = new CompositePropertySource(
NACOS_PROPERTY_SOURCE_NAME);
loadSharedConfiguration(composite);
loadExtConfiguration(composite);
loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension);
return composite;
}
private void loadSharedConfiguration(
CompositePropertySource compositePropertySource) {
String sharedDataIds = nacosConfigProperties.getSharedDataids();
String refreshDataIds = nacosConfigProperties.getRefreshableDataids();
if (sharedDataIds == null || sharedDataIds.trim().length() == 0) {
return;
}
String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR);
checkDataIdFileExtension(sharedDataIdArry);
for (int i = 0; i < sharedDataIdArry.length; i++) {
String dataId = sharedDataIdArry[i];
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds,
sharedDataIdArry[i]);
loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP",
fileExtension, isRefreshable);
}
}
private void loadExtConfiguration(CompositePropertySource compositePropertySource) {
if (nacosConfigProperties.getExtConfig() == null
|| nacosConfigProperties.getExtConfig().isEmpty()) {
return;
}
List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
.getExtConfig();
checkExtConfiguration(extConfigs);
for (NacosConfigProperties.Config config : extConfigs) {
String dataId = config.getDataId();
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
loadNacosDataIfPresent(compositePropertySource, dataId, config.getGroup(),
fileExtension, config.isRefresh());
}
}
private void checkExtConfiguration(List<NacosConfigProperties.Config> extConfigs) {
String[] dataIds = new String[extConfigs.size()];
for (int i = 0; i < extConfigs.size(); i++) {
String dataId = extConfigs.get(i).getDataId();
if (dataId == null || dataId.trim().length() == 0) {
throw new IllegalStateException(String.format(
"the [ spring.cloud.nacos.config.ext-config[%s] ] must give a dataid",
i));
}
dataIds[i] = dataId;
}
checkDataIdFileExtension(dataIds);
}
private void loadApplicationConfiguration(
CompositePropertySource compositePropertySource, String nacosGroup,
String dataIdPrefix, String fileExtension) {
loadNacosDataIfPresent(compositePropertySource,
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true);
for (String profile : nacosConfigProperties.getActiveProfiles()) {
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
fileExtension, true);
}
}
private void loadNacosDataIfPresent(final CompositePropertySource composite,
final String dataId, final String group, String fileExtension,
boolean isRefreshable) {
if (NacosContextRefresher.loadCount.get() != 0) {
NacosPropertySource ps;
if (!isRefreshable) {
ps = NacosPropertySourceRepository.getNacosPropertySource(dataId);
}
else {
ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true);
}
composite.addFirstPropertySource(ps);
}
else {
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
fileExtension, isRefreshable);
composite.addFirstPropertySource(ps);
}
}
private static void checkDataIdFileExtension(String[] sharedDataIdArry) {
StringBuilder stringBuilder = new StringBuilder();
outline: for (int i = 0; i < sharedDataIdArry.length; i++) {
for (String fileExtension : SUPPORT_FILE_EXTENSION) {
if (sharedDataIdArry[i].indexOf(fileExtension) > 0) {
continue outline;
}
}
stringBuilder.append(sharedDataIdArry[i] + ",");
}
if (stringBuilder.length() > 0) {
String result = stringBuilder.substring(0, stringBuilder.length() - 1);
throw new IllegalStateException(String.format(
"[%s] must contains file extension with properties|yaml|yml",
result));
}
}
private boolean checkDataIdIsRefreshbable(String refreshDataIds,
String sharedDataId) {
if (refreshDataIds == null || "".equals(refreshDataIds)) {
return false;
}
String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPARATOR_CHAR);
for (String refreshDataId : refreshDataIdArry) {
if (refreshDataId.equals(sharedDataId)) {
return true;
}
}
return false;
}
}

@ -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;
@ -27,6 +26,7 @@ import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
import org.springframework.cloud.alibaba.nacos.registry.NacosServiceRegistry;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -50,8 +50,10 @@ public class NacosDiscoveryAutoConfiguration {
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosRegistration nacosRegistration() {
return new NacosRegistration();
public NacosRegistration nacosRegistration(
NacosDiscoveryProperties nacosDiscoveryProperties,
ApplicationContext context) {
return new NacosRegistration(nacosDiscoveryProperties, context);
}
@Bean

@ -20,7 +20,6 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
@ -36,9 +35,12 @@ public class NacosDiscoveryClient implements DiscoveryClient {
.getLogger(NacosDiscoveryClient.class);
public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client";
@Autowired
private NacosDiscoveryProperties discoveryProperties;
public NacosDiscoveryClient(NacosDiscoveryProperties discoveryProperties) {
this.discoveryProperties = discoveryProperties;
}
@Override
public String description() {
return DESCRIPTION;

@ -32,8 +32,9 @@ import org.springframework.context.annotation.Configuration;
public class NacosDiscoveryClientAutoConfiguration {
@Bean
public DiscoveryClient nacosDiscoveryClient() {
return new NacosDiscoveryClient();
public DiscoveryClient nacosDiscoveryClient(
NacosDiscoveryProperties discoveryProperties) {
return new NacosDiscoveryClient(discoveryProperties);
}
@Bean

@ -26,7 +26,6 @@ import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import org.slf4j.Logger;
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.NacosDiscoveryProperties;
@ -41,9 +40,12 @@ public class NacosDiscoveryEndpoint {
private static final Logger LOGGER = LoggerFactory
.getLogger(NacosDiscoveryEndpoint.class);
@Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties;
public NacosDiscoveryEndpoint(NacosDiscoveryProperties nacosDiscoveryProperties) {
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
}
/**
* @return nacos discovery endpoint
*/

@ -20,7 +20,7 @@ import org.springframework.boot.actuate.autoconfigure.endpoint.condition.Conditi
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -34,8 +34,9 @@ public class NacosDiscoveryEndpointAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {
return new NacosDiscoveryEndpoint();
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint(
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosDiscoveryEndpoint(nacosDiscoveryProperties);
}
}

@ -18,7 +18,6 @@ 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.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
@ -33,7 +32,6 @@ public class NacosAutoServiceRegistration
private static final Logger LOGGER = LoggerFactory
.getLogger(NacosAutoServiceRegistration.class);
@Autowired
private NacosRegistration registration;
public NacosAutoServiceRegistration(

@ -17,7 +17,6 @@
package org.springframework.cloud.alibaba.nacos.registry;
import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ManagementServerPortUtils;
@ -42,12 +41,16 @@ public class NacosRegistration implements Registration, ServiceInstance {
private static final String MANAGEMENT_CONTEXT_PATH = "management.context-path";
private static final String MANAGEMENT_ADDRESS = "management.address";
@Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties;
@Autowired
private ApplicationContext context;
public NacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties,
ApplicationContext context) {
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
this.context = context;
}
@PostConstruct
public void init() {

@ -19,6 +19,7 @@ package org.springframework.cloud.alibaba.nacos.ribbon;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -32,8 +33,8 @@ public class NacosRibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config) {
NacosServerList serverList = new NacosServerList();
public ServerList<?> ribbonServerList(IClientConfig config, NacosDiscoveryProperties nacosDiscoveryProperties) {
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(config);
return serverList;
}

@ -18,7 +18,6 @@ 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.NacosDiscoveryProperties;
import java.util.ArrayList;
@ -32,7 +31,6 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
*/
public class NacosServerList extends AbstractServerList<NacosServer> {
@Autowired
private NacosDiscoveryProperties discoveryProperties;
private String serviceId;
@ -40,8 +38,8 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
public NacosServerList() {
}
public NacosServerList(String serviceId) {
this.serviceId = serviceId;
public NacosServerList(NacosDiscoveryProperties discoveryProperties) {
this.discoveryProperties = discoveryProperties;
}
@Override

@ -39,7 +39,7 @@ public class NacosDiscoveryAutoConfigurationTests {
AutoConfigurations.of(NacosDiscoveryTestConfiguration.class,
NacosDiscoveryAutoConfiguration.class,
NacosDiscoveryClientAutoConfiguration.class))
.withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080")
.withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848")
.withPropertyValues("spring.cloud.nacos.discovery.port=18080")
.withPropertyValues("spring.cloud.nacos.discovery.service=myapp");
@ -49,7 +49,7 @@ public class NacosDiscoveryAutoConfigurationTests {
NacosDiscoveryProperties properties = context
.getBean(NacosDiscoveryProperties.class);
assertThat(properties.getPort()).isEqualTo(18080);
assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8080");
assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8848");
assertThat(properties.getService()).isEqualTo("myapp");
});
}

@ -25,7 +25,7 @@ public class NacosRibbonClientConfigurationTests {
NacosRibbonClientConfiguration.class,
NacosDiscoveryClientAutoConfiguration.class,
RibbonNacosAutoConfiguration.class))
.withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080")
.withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848")
.withPropertyValues("spring.cloud.nacos.discovery.port=18080")
.withPropertyValues("spring.cloud.nacos.discovery.service=myapp");

Loading…
Cancel
Save