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

Compare commits

...

35 Commits

Author SHA1 Message Date
Mercy Ma 2835024113 Merge pull request #1747 from mercyblitz/master
2.2.3.RELEASE
2020-09-17 22:05:41 +08:00
mercyblitz b4abf6db15 Update the version to be 2.2.3.RELEASE 2020-09-17 21:31:59 +08:00
Mercy Ma de9f5a97c0 Merge pull request #1743 from mercyblitz/master
[Infrastructure] Upgrading the dependencies
2020-09-17 17:41:47 +08:00
mercyblitz ea3f76c77f Remove the useless properties 2020-09-17 17:41:33 +08:00
mercyblitz 24de18a00c Polish alibaba/spring-cloud-alibaba#1727 : rollback rocketmq-spring-boot-starter 2020-09-17 17:27:33 +08:00
mercyblitz 9842296af5 Merge remote-tracking branch 'upstream/master' 2020-09-17 16:47:58 +08:00
Mercy Ma fbc8191d87 Merge pull request #1737 from mostcool/master
update pom.xml
2020-09-17 16:47:46 +08:00
mercyblitz f40fe58dc9 Merge remote-tracking branch 'upstream/master' 2020-09-17 16:41:48 +08:00
mercyblitz bb9dbba7d1 Polish alibaba/spring-cloud-alibaba#1741 : [Infrastructure] Upgrading the Nacos Client 2020-09-17 16:41:07 +08:00
mercyblitz 0e77fce012 Polish alibaba/spring-cloud-alibaba#1727 : upgrade rocketmq-spring-boot-starter 2020-09-17 15:09:33 +08:00
Mercy Ma d6aff2e60d Merge pull request #1736 from mercyblitz/master
[Issue] NPE issue on Alibaba's spring-context-support
2020-09-17 15:08:52 +08:00
K 64c8ebf536 update spring-cloud-dependencies-parent version
update spring-cloud-dependencies-parent version
2020-09-17 15:01:15 +08:00
K c3afdad2b1 remove useless properties
remove useless properties
2020-09-17 15:00:25 +08:00
mercyblitz b23dd033fb Polish alibaba/spring-cloud-alibaba#1723 : 2.2.2.RELEASE undertow stop shows "Failed to stop bean 'nacosWatch'" 2020-09-17 14:43:30 +08:00
mercyblitz 8c5e745d1b Upgrade the version to be 2.2.3-SNAPSHOT 2020-09-17 14:37:43 +08:00
K fa21bc19ce Merge pull request #7 from alibaba/master
sync
2020-09-15 08:44:23 +08:00
Mercy Ma c0b056a90e Merge pull request #1729 from yuhuangbin/master
[Enhancement] NacosWatch keep publish HeartbeatEvent
2020-09-15 08:08:52 +08:00
yuhuangbin f284172fbb fix 2020-09-14 21:31:54 +08:00
yuhuangbin 15ffaccc75 enhance 2020-09-14 15:30:10 +08:00
Mercy Ma 51baa35139 Merge pull request #1724 from yuhuangbin/master
[Enhancement] NacosDiscoveryClient always getNamingservice from Nacosmanager
2020-09-14 14:43:20 +08:00
K 6880e4bd7b Merge pull request #6 from alibaba/master
sync
2020-09-14 14:38:40 +08:00
yuhuangbin c5d68bb60f enhance 2020-09-14 13:03:25 +08:00
Mercy Ma 4b06a6178d Merge pull request #1722 from yuhuangbin/2.2.2
Fix some bugs for 2.2.2.RELEASE
2020-09-14 10:11:36 +08:00
yuhuangbin b638480cf7 fix test 2020-09-14 09:40:19 +08:00
yuhuangbin 5b0d53388b sentinel NacosDataSourceFactoryBean enhance 2020-09-14 08:57:08 +08:00
yuhuangbin df156d4fa2 fix 2020-09-13 21:57:54 +08:00
yuhuangbin e9a6eaa025 fix 2020-09-13 19:24:25 +08:00
yuhuangbin 702831cb8f fix 2020-09-13 18:02:36 +08:00
余黄彬 2114262e47 Merge pull request #2 from alibaba/master
update
2020-09-13 17:39:26 +08:00
Mercy Ma 44e541d67d Merge pull request #1714 from alibaba/2.2.2.RELEASE
2.2.2.release
2020-09-12 18:48:04 +08:00
K c4cf70182f Merge pull request #5 from alibaba/master
sync
2020-09-09 17:38:43 +08:00
K cb00aeb6dc Merge pull request #4 from alibaba/master
sync
2020-08-28 08:23:03 +08:00
K ba2d96edcd Merge pull request #3 from alibaba/master
sync
2020-08-11 16:23:55 +08:00
K 1e301b2168 Merge pull request #2 from alibaba/master
sync
2020-08-11 15:29:02 +08:00
K 5066b92378 Merge pull request #1 from alibaba/master
sync codes
2020-07-27 17:43:28 +08:00
17 changed files with 158 additions and 88 deletions
+2 -6
View File
@@ -80,7 +80,7 @@
<properties>
<!-- Project revision -->
<revision>2.2.2.RELEASE</revision>
<revision>2.2.3-SNAPSHOT</revision>
<!-- Dependency Versions -->
<spring-cloud-commons.version>2.2.5.RELEASE</spring-cloud-commons.version>
@@ -93,10 +93,6 @@
<spring-cloud-config.version>2.2.5.RELEASE</spring-cloud-config.version>
<spring-cloud-zookeeper.version>2.2.3.RELEASE</spring-cloud-zookeeper.version>
<junit.version>4.12</junit.version>
<javax-servlet-api>3.0</javax-servlet-api>
<slf4j-api.version>1.7.25</slf4j-api.version>
<!-- Apache Dubbo -->
<dubbo.version>2.7.8</dubbo.version>
<curator.version>4.0.1</curator.version>
@@ -449,4 +445,4 @@
</profile>
</profiles>
</project>
</project>
+5 -6
View File
@@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-cloud-dependencies-parent</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>2.2.0.RELEASE</version>
<artifactId>spring-cloud-dependencies-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
@@ -18,13 +18,12 @@
<description>Spring Cloud Alibaba Dependencies</description>
<properties>
<revision>2.2.2.RELEASE</revision>
<revision>2.2.3.RELEASE</revision>
<sentinel.version>1.8.0</sentinel.version>
<oss.version>3.1.0</oss.version>
<seata.version>1.3.0</seata.version>
<nacos.client.version>1.3.2</nacos.client.version>
<nacos.client.version>1.3.3</nacos.client.version>
<nacos.config.version>0.8.0</nacos.config.version>
<spring.context.support.version>1.0.9</spring.context.support.version>
<spring.context.support.version>1.0.10</spring.context.support.version>
<!-- Maven Plugin Versions -->
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
@@ -10,7 +10,7 @@ Spring Cloud Alibaba BOM 包含了它所使用的所有依赖的版本。
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<version>2.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@@ -8,7 +8,7 @@ If youre a Maven Central user, add our BOM to your pom.xml <dependencyManagem
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<version>2.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@@ -67,8 +67,12 @@ public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource>
if (!StringUtils.isEmpty(this.namespace)) {
properties.setProperty(PropertyKeyConst.NAMESPACE, this.namespace);
}
properties.setProperty(PropertyKeyConst.USERNAME, this.username);
properties.setProperty(PropertyKeyConst.PASSWORD, this.password);
if (!StringUtils.isEmpty(this.username)) {
properties.setProperty(PropertyKeyConst.USERNAME, this.username);
}
if (!StringUtils.isEmpty(this.password)) {
properties.setProperty(PropertyKeyConst.PASSWORD, this.password);
}
return new NacosDataSource(properties, groupId, dataId, converter);
}
@@ -23,14 +23,14 @@ import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.alibaba.cloud.nacos.event.NacosDiscoveryInfoChangedEvent;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.spring.util.PropertySourcesUtils;
@@ -41,6 +41,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
@@ -216,7 +217,7 @@ public class NacosDiscoveryProperties {
private NacosServiceManager nacosServiceManager;
@Autowired
private Optional<NacosAutoServiceRegistration> nacosAutoServiceRegistrationOptional;
private ApplicationEventPublisher applicationEventPublisher;
@PostConstruct
public void init() throws Exception {
@@ -266,14 +267,19 @@ public class NacosDiscoveryProperties {
}
this.overrideFromEnv(environment);
if (nacosServiceManager.isNacosDiscoveryInfoChanged(this)) {
applicationEventPublisher
.publishEvent(new NacosDiscoveryInfoChangedEvent(this));
}
}
nacosAutoServiceRegistrationOptional.ifPresent(nacosAutoServiceRegistration -> {
if (nacosServiceManager.isNacosDiscoveryInfoChanged(this)) {
nacosAutoServiceRegistration.stop();
nacosServiceManager.reBuildNacosService(getNacosProperties());
nacosAutoServiceRegistration.start();
}
});
/**
* recommend to use {@link NacosServiceManager#getNamingService(Properties)}.
* @return NamingService
*/
@Deprecated
public NamingService namingServiceInstance() {
return nacosServiceManager.getNamingService(this.getNacosProperties());
}
public String getEndpoint() {
@@ -23,6 +23,8 @@ import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingMaintainService;
import com.alibaba.nacos.api.naming.NamingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent;
import org.springframework.cloud.client.serviceregistry.Registration;
@@ -37,6 +39,8 @@ import static org.springframework.beans.BeanUtils.copyProperties;
*/
public class NacosServiceManager {
private static final Logger log = LoggerFactory.getLogger(NacosServiceManager.class);
private NacosDiscoveryProperties nacosDiscoveryPropertiesCache;
private NamingService namingService;
@@ -107,13 +111,10 @@ public class NacosServiceManager {
}
}
public void reBuildNacosService(Properties nacosProperties) {
namingService = createNewNamingService(nacosProperties);
namingMaintainService = createNamingMaintainService(nacosProperties);
}
public void nacosServiceShutDown() throws NacosException {
this.namingService.shutDown();
namingService = null;
namingMaintainService = null;
}
@EventListener
@@ -20,6 +20,7 @@ import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -31,6 +32,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
/**
* @author xiaojing
@@ -56,8 +58,10 @@ public class NacosDiscoveryClientConfiguration {
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled",
matchIfMissing = true)
public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties);
NacosDiscoveryProperties nacosDiscoveryProperties,
ObjectProvider<TaskScheduler> taskExecutorObjectProvider) {
return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties,
taskExecutorObjectProvider);
}
}
@@ -38,13 +38,12 @@ public class NacosServiceDiscovery {
private NacosDiscoveryProperties discoveryProperties;
private NamingService namingService;
private NacosServiceManager nacosServiceManager;
public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties,
NacosServiceManager nacosServiceManager) {
this.discoveryProperties = discoveryProperties;
this.namingService = nacosServiceManager
.getNamingService(discoveryProperties.getNacosProperties());
this.nacosServiceManager = nacosServiceManager;
}
/**
@@ -55,7 +54,8 @@ public class NacosServiceDiscovery {
*/
public List<ServiceInstance> getInstances(String serviceId) throws NacosException {
String group = discoveryProperties.getGroup();
List<Instance> instances = namingService.selectInstances(serviceId, group, true);
List<Instance> instances = namingService().selectInstances(serviceId, group,
true);
return hostToServiceInstanceList(instances, serviceId);
}
@@ -66,7 +66,7 @@ public class NacosServiceDiscovery {
*/
public List<String> getServices() throws NacosException {
String group = discoveryProperties.getGroup();
ListView<String> services = namingService.getServicesOfServer(1,
ListView<String> services = namingService().getServicesOfServer(1,
Integer.MAX_VALUE, group);
return services.getData();
}
@@ -111,4 +111,9 @@ public class NacosServiceDiscovery {
return nacosServiceInstance;
}
private NamingService namingService() {
return nacosServiceManager
.getNamingService(discoveryProperties.getNacosProperties());
}
}
@@ -21,6 +21,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
@@ -35,10 +36,13 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.SmartLifecycle;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/**
* @author xiaojing
@@ -56,14 +60,27 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
private ApplicationEventPublisher publisher;
private ScheduledFuture<?> watchFuture;
private NacosServiceManager nacosServiceManager;
private final NacosDiscoveryProperties properties;
private final TaskScheduler taskScheduler;
public NacosWatch(NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties properties) {
NacosDiscoveryProperties properties,
ObjectProvider<TaskScheduler> taskScheduler) {
this.nacosServiceManager = nacosServiceManager;
this.properties = properties;
this.taskScheduler = taskScheduler.getIfAvailable(NacosWatch::getTaskScheduler);
}
private static ThreadPoolTaskScheduler getTaskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setBeanName("Nacos-Watch-Task-Scheduler");
taskScheduler.initialize();
return taskScheduler;
}
@Override
@@ -97,8 +114,6 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
instanceOptional.ifPresent(currentInstance -> {
resetIfNeeded(currentInstance);
});
publisher.publishEvent(new HeartbeatEvent(NacosWatch.this,
nacosWatchIndex.getAndIncrement()));
}
}
});
@@ -112,6 +127,9 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
catch (Exception e) {
log.error("namingService subscribe failed, properties:{}", properties, e);
}
this.watchFuture = this.taskScheduler.scheduleWithFixedDelay(
this::nacosServicesWatch, this.properties.getWatchDelay());
}
}
@@ -135,10 +153,17 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
@Override
public void stop() {
if (this.running.compareAndSet(true, false)) {
if (this.watchFuture != null) {
// shutdown current user-thread,
// then the other daemon-threads will terminate automatic.
((ThreadPoolTaskScheduler) this.taskScheduler).shutdown();
this.watchFuture.cancel(true);
}
EventListener eventListener = listenerMap.get(buildKey());
NamingService namingService = nacosServiceManager
.getNamingService(properties.getNacosProperties());
try {
NamingService namingService = nacosServiceManager
.getNamingService(properties.getNacosProperties());
namingService.unsubscribe(properties.getService(), properties.getGroup(),
Arrays.asList(properties.getClusterName()), eventListener);
}
@@ -159,4 +184,12 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
return 0;
}
public void nacosServicesWatch() {
// nacos doesn't support watch now , publish an event every 30 seconds.
this.publisher.publishEvent(
new HeartbeatEvent(this, nacosWatchIndex.getAndIncrement()));
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.nacos.event;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import org.springframework.context.ApplicationEvent;
/**
* @author yuhuangbin
*/
public class NacosDiscoveryInfoChangedEvent extends ApplicationEvent {
public NacosDiscoveryInfoChangedEvent(
NacosDiscoveryProperties nacosDiscoveryProperties) {
super(nacosDiscoveryProperties);
}
@Override
public NacosDiscoveryProperties getSource() {
return (NacosDiscoveryProperties) super.getSource();
}
}
@@ -16,6 +16,7 @@
package com.alibaba.cloud.nacos.registry;
import com.alibaba.cloud.nacos.event.NacosDiscoveryInfoChangedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -23,6 +24,7 @@ import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegis
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.event.EventListener;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -102,4 +104,14 @@ public class NacosAutoServiceRegistration
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
}
@EventListener
public void onNacosDiscoveryInfoChangedEvent(NacosDiscoveryInfoChangedEvent event) {
restart();
}
private void restart() {
this.stop();
this.start();
}
}
@@ -27,6 +27,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.util.StringUtils;
@@ -44,12 +45,11 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
private final NacosDiscoveryProperties nacosDiscoveryProperties;
@Autowired
private NacosServiceManager nacosServiceManager;
public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties,
NacosServiceManager nacosServiceManager) {
public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
this.nacosServiceManager = nacosServiceManager;
}
@Override
@@ -20,7 +20,6 @@ import java.util.List;
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
import org.springframework.beans.factory.ObjectProvider;
@@ -51,9 +50,8 @@ public class NacosServiceRegistryAutoConfiguration {
@Bean
public NacosServiceRegistry nacosServiceRegistry(
NacosDiscoveryProperties nacosDiscoveryProperties,
NacosServiceManager nacosServiceManager) {
return new NacosServiceRegistry(nacosDiscoveryProperties, nacosServiceManager);
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosServiceRegistry(nacosDiscoveryProperties);
}
@Bean
@@ -17,7 +17,6 @@
package com.alibaba.cloud.nacos.ribbon;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
@@ -43,15 +42,13 @@ public class NacosRibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config,
NacosDiscoveryProperties nacosDiscoveryProperties,
NacosServiceManager nacosServiceManager) {
NacosDiscoveryProperties nacosDiscoveryProperties) {
if (this.propertiesFactory.isSet(ServerList.class, config.getClientName())) {
ServerList serverList = this.propertiesFactory.get(ServerList.class, config,
config.getClientName());
return serverList;
}
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties,
nacosServiceManager);
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(config);
return serverList;
}
@@ -20,7 +20,6 @@ import java.util.ArrayList;
import java.util.List;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.netflix.client.config.IClientConfig;
@@ -34,14 +33,10 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
private NacosDiscoveryProperties discoveryProperties;
private NacosServiceManager nacosServiceManager;
private String serviceId;
public NacosServerList(NacosDiscoveryProperties discoveryProperties,
NacosServiceManager nacosServiceManager) {
public NacosServerList(NacosDiscoveryProperties discoveryProperties) {
this.discoveryProperties = discoveryProperties;
this.nacosServiceManager = nacosServiceManager;
}
@Override
@@ -57,8 +52,7 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
private List<NacosServer> getServers() {
try {
String group = discoveryProperties.getGroup();
List<Instance> instances = nacosServiceManager
.getNamingService(discoveryProperties.getNacosProperties())
List<Instance> instances = discoveryProperties.namingServiceInstance()
.selectInstances(serviceId, group, true);
return instancesToServerList(instances);
}
@@ -23,7 +23,6 @@ import java.util.List;
import java.util.stream.Collectors;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.cloud.nacos.test.NacosMockTest;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
@@ -49,16 +48,12 @@ public class NacosServerListTests {
NacosDiscoveryProperties.class);
NamingService namingService = mock(NamingService.class);
NacosServiceManager nacosServiceManager = mock(NacosServiceManager.class);
when(nacosServiceManager
.getNamingService(nacosDiscoveryProperties.getNacosProperties()))
.thenReturn(namingService);
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
when(namingService.selectInstances(anyString(), eq("DEFAULT"), eq(true)))
.thenReturn(null);
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties,
nacosServiceManager);
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
List<NacosServer> servers = serverList.getInitialListOfServers();
assertThat(servers).isEmpty();
}
@@ -73,13 +68,10 @@ public class NacosServerListTests {
NacosDiscoveryProperties nacosDiscoveryProperties = mock(
NacosDiscoveryProperties.class);
NacosServiceManager nacosServiceManager = mock(NacosServiceManager.class);
NamingService namingService = mock(NamingService.class);
when(nacosServiceManager
.getNamingService(nacosDiscoveryProperties.getNacosProperties()))
.thenReturn(namingService);
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
@@ -87,8 +79,7 @@ public class NacosServerListTests {
IClientConfig clientConfig = mock(IClientConfig.class);
when(clientConfig.getClientName()).thenReturn("test-service");
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties,
nacosServiceManager);
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(clientConfig);
List<NacosServer> servers = serverList.getInitialListOfServers();
assertThat(servers).hasSize(1);
@@ -111,13 +102,10 @@ public class NacosServerListTests {
NacosDiscoveryProperties nacosDiscoveryProperties = mock(
NacosDiscoveryProperties.class);
NacosServiceManager nacosServiceManager = mock(NacosServiceManager.class);
NamingService namingService = mock(NamingService.class);
when(nacosServiceManager
.getNamingService(nacosDiscoveryProperties.getNacosProperties()))
.thenReturn(namingService);
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
.thenReturn(instances.stream().filter(Instance::isHealthy)
@@ -125,8 +113,7 @@ public class NacosServerListTests {
IClientConfig clientConfig = mock(IClientConfig.class);
when(clientConfig.getClientName()).thenReturn("test-service");
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties,
nacosServiceManager);
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(clientConfig);
List<NacosServer> servers = serverList.getInitialListOfServers();
assertThat(servers).hasSize(1);
@@ -153,13 +140,10 @@ public class NacosServerListTests {
NacosDiscoveryProperties nacosDiscoveryProperties = mock(
NacosDiscoveryProperties.class);
NacosServiceManager nacosServiceManager = mock(NacosServiceManager.class);
NamingService namingService = mock(NamingService.class);
when(nacosServiceManager
.getNamingService(nacosDiscoveryProperties.getNacosProperties()))
.thenReturn(namingService);
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
.thenReturn(instances.stream().filter(Instance::isHealthy)
@@ -167,8 +151,7 @@ public class NacosServerListTests {
IClientConfig clientConfig = mock(IClientConfig.class);
when(clientConfig.getClientName()).thenReturn("test-service");
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties,
nacosServiceManager);
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(clientConfig);
List<NacosServer> servers = serverList.getUpdatedListOfServers();