pull/1722/head
yuhuangbin 4 years ago
parent 702831cb8f
commit e9a6eaa025

@ -29,7 +29,8 @@ import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import com.alibaba.cloud.nacos.event.NacosDiscoveryInfoInitializedEvent;
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;
@ -212,6 +213,9 @@ public class NacosDiscoveryProperties {
@Autowired
private Environment environment;
@Autowired
private NacosServiceManager nacosServiceManager;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@ -263,9 +267,19 @@ public class NacosDiscoveryProperties {
}
this.overrideFromEnv(environment);
if (nacosServiceManager.isNacosDiscoveryInfoChanged(this)) {
applicationEventPublisher
.publishEvent(new NacosDiscoveryInfoChangedEvent(this));
}
}
applicationEventPublisher
.publishEvent(new NacosDiscoveryInfoInitializedEvent(this));
/**
* recommend to use {@link NacosServiceManager#getNamingService(Properties)}.
* @return NamingService
*/
@Deprecated
public NamingService namingServiceInstance() {
return nacosServiceManager.getNamingService(this.getNacosProperties());
}
public String getEndpoint() {

@ -17,17 +17,15 @@
package com.alibaba.cloud.nacos;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import com.alibaba.cloud.nacos.event.NacosDiscoveryInfoInitializedEvent;
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
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.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.event.EventListener;
@ -41,15 +39,14 @@ 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;
private NamingMaintainService namingMaintainService;
@Autowired
private Optional<NacosAutoServiceRegistration> nacosAutoServiceRegistrationOptional;
public NamingService getNamingService(Properties properties) {
if (Objects.isNull(this.namingService)) {
buildNamingService(properties);
@ -114,13 +111,10 @@ public class NacosServiceManager {
}
}
private void reBuildNacosService(Properties nacosProperties) {
namingService = createNewNamingService(nacosProperties);
namingMaintainService = createNamingMaintainService(nacosProperties);
}
public void nacosServiceShutDown() throws NacosException {
this.namingService.shutDown();
namingService = null;
namingMaintainService = null;
}
@EventListener
@ -137,19 +131,4 @@ public class NacosServiceManager {
}
}
@EventListener
public void onNacosDiscoveryInfoInitializedEvent(
NacosDiscoveryInfoInitializedEvent nacosDiscoveryInfoInitializedEvent) {
NacosDiscoveryProperties nacosDiscoveryProperties = nacosDiscoveryInfoInitializedEvent
.getSource();
if (isNacosDiscoveryInfoChanged(nacosDiscoveryProperties)
&& nacosAutoServiceRegistrationOptional.isPresent()) {
NacosAutoServiceRegistration nacosAutoServiceRegistration = nacosAutoServiceRegistrationOptional
.get();
nacosAutoServiceRegistration.stop();
reBuildNacosService(nacosDiscoveryProperties.getNacosProperties());
nacosAutoServiceRegistration.start();
}
}
}

@ -23,9 +23,9 @@ import org.springframework.context.ApplicationEvent;
/**
* @author yuhuangbin
*/
public class NacosDiscoveryInfoInitializedEvent extends ApplicationEvent {
public class NacosDiscoveryInfoChangedEvent extends ApplicationEvent {
public NacosDiscoveryInfoInitializedEvent(
public NacosDiscoveryInfoChangedEvent(
NacosDiscoveryProperties nacosDiscoveryProperties) {
super(nacosDiscoveryProperties);
}

@ -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();
}
}

@ -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);
}

@ -57,8 +57,7 @@ public class NacosServerListTests {
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();
}
@ -87,8 +86,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);
@ -125,8 +123,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);
@ -167,8 +164,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();

Loading…
Cancel
Save