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