1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Merge remote-tracking branch 'upstream/master'

# Conflicts:
#	spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java
This commit is contained in:
gaoyunpeng 2019-01-03 11:05:33 +08:00
commit 8a4a92f03b
13 changed files with 185 additions and 176 deletions

View File

@ -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` 中添加如下配置。 如果需要使用已发布的版本,在 `dependencyManagement` 中添加如下配置。
@ -59,7 +59,7 @@ Spring Cloud 使用 Maven 来构建最快的使用方式是将本项目clone
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId> <artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.0.RELEASE</version> <version>0.2.1.RELEASE</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -34,8 +34,9 @@ public class NacosDiscoveryEndpointAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint @ConditionalOnEnabledEndpoint
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() { public NacosDiscoveryEndpoint nacosDiscoveryEndpoint(
return new NacosDiscoveryEndpoint(); NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosDiscoveryEndpoint(nacosDiscoveryProperties);
} }
} }

View File

@ -18,7 +18,6 @@ package org.springframework.cloud.alibaba.nacos.registry;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
@ -33,7 +32,6 @@ public class NacosAutoServiceRegistration
private static final Logger LOGGER = LoggerFactory private static final Logger LOGGER = LoggerFactory
.getLogger(NacosAutoServiceRegistration.class); .getLogger(NacosAutoServiceRegistration.class);
@Autowired
private NacosRegistration registration; private NacosRegistration registration;
public NacosAutoServiceRegistration( public NacosAutoServiceRegistration(

View File

@ -17,7 +17,6 @@
package org.springframework.cloud.alibaba.nacos.registry; package org.springframework.cloud.alibaba.nacos.registry;
import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; 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.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ManagementServerPortUtils; 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_CONTEXT_PATH = "management.context-path";
private static final String MANAGEMENT_ADDRESS = "management.address"; private static final String MANAGEMENT_ADDRESS = "management.address";
@Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties; private NacosDiscoveryProperties nacosDiscoveryProperties;
@Autowired
private ApplicationContext context; private ApplicationContext context;
public NacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties,
ApplicationContext context) {
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
this.context = context;
}
@PostConstruct @PostConstruct
public void init() { public void init() {

View File

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

View File

@ -18,7 +18,6 @@ package org.springframework.cloud.alibaba.nacos.ribbon;
import com.netflix.client.config.IClientConfig; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerList; import com.netflix.loadbalancer.AbstractServerList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
import java.util.ArrayList; import java.util.ArrayList;
@ -32,7 +31,6 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
*/ */
public class NacosServerList extends AbstractServerList<NacosServer> { public class NacosServerList extends AbstractServerList<NacosServer> {
@Autowired
private NacosDiscoveryProperties discoveryProperties; private NacosDiscoveryProperties discoveryProperties;
private String serviceId; private String serviceId;
@ -40,8 +38,8 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
public NacosServerList() { public NacosServerList() {
} }
public NacosServerList(String serviceId) { public NacosServerList(NacosDiscoveryProperties discoveryProperties) {
this.serviceId = serviceId; this.discoveryProperties = discoveryProperties;
} }
@Override @Override

View File

@ -39,7 +39,7 @@ public class NacosDiscoveryAutoConfigurationTests {
AutoConfigurations.of(NacosDiscoveryTestConfiguration.class, AutoConfigurations.of(NacosDiscoveryTestConfiguration.class,
NacosDiscoveryAutoConfiguration.class, NacosDiscoveryAutoConfiguration.class,
NacosDiscoveryClientAutoConfiguration.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.port=18080")
.withPropertyValues("spring.cloud.nacos.discovery.service=myapp"); .withPropertyValues("spring.cloud.nacos.discovery.service=myapp");
@ -49,7 +49,7 @@ public class NacosDiscoveryAutoConfigurationTests {
NacosDiscoveryProperties properties = context NacosDiscoveryProperties properties = context
.getBean(NacosDiscoveryProperties.class); .getBean(NacosDiscoveryProperties.class);
assertThat(properties.getPort()).isEqualTo(18080); 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"); assertThat(properties.getService()).isEqualTo("myapp");
}); });
} }

View File

@ -25,7 +25,7 @@ public class NacosRibbonClientConfigurationTests {
NacosRibbonClientConfiguration.class, NacosRibbonClientConfiguration.class,
NacosDiscoveryClientAutoConfiguration.class, NacosDiscoveryClientAutoConfiguration.class,
RibbonNacosAutoConfiguration.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.port=18080")
.withPropertyValues("spring.cloud.nacos.discovery.service=myapp"); .withPropertyValues("spring.cloud.nacos.discovery.service=myapp");