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 | Date | |
|---|---|---|---|
| 2835024113 | |||
| b4abf6db15 | |||
| de9f5a97c0 | |||
| ea3f76c77f | |||
| 24de18a00c | |||
| 9842296af5 | |||
| fbc8191d87 | |||
| f40fe58dc9 | |||
| bb9dbba7d1 | |||
| 0e77fce012 | |||
| d6aff2e60d | |||
| 64c8ebf536 | |||
| c3afdad2b1 | |||
| b23dd033fb | |||
| 8c5e745d1b | |||
| fa21bc19ce | |||
| c0b056a90e | |||
| f284172fbb | |||
| 15ffaccc75 | |||
| 51baa35139 | |||
| 6880e4bd7b | |||
| c5d68bb60f | |||
| 4b06a6178d | |||
| b638480cf7 | |||
| 5b0d53388b | |||
| df156d4fa2 | |||
| e9a6eaa025 | |||
| 702831cb8f | |||
| 2114262e47 | |||
| 44e541d67d | |||
| c4cf70182f | |||
| cb00aeb6dc | |||
| ba2d96edcd | |||
| 1e301b2168 | |||
| 5066b92378 |
@@ -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>
|
||||
|
||||
@@ -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 you’re 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>
|
||||
|
||||
+6
-2
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+16
-10
@@ -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() {
|
||||
|
||||
+6
-5
@@ -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
|
||||
|
||||
+6
-2
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
-5
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+38
-5
@@ -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()));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+38
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -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
|
||||
|
||||
+2
-4
@@ -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
|
||||
|
||||
+2
-5
@@ -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;
|
||||
}
|
||||
|
||||
+2
-8
@@ -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);
|
||||
}
|
||||
|
||||
+8
-25
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user