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

nacos re-register enhance

This commit is contained in:
yuhuangbin 2020-09-07 21:52:16 +08:00
parent 59b272c005
commit 307935a5d9
2 changed files with 29 additions and 5 deletions

View File

@ -30,6 +30,7 @@ import java.util.regex.Pattern;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.NamingMaintainService;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
@ -210,12 +211,20 @@ public class NacosDiscoveryProperties {
*/ */
private boolean ephemeral = true; private boolean ephemeral = true;
/**
* If the instance needs to be re-registered. The default value is false.
*/
private boolean needNewlyRegister = false;
@Autowired @Autowired
private InetUtils inetUtils; private InetUtils inetUtils;
@Autowired @Autowired
private Environment environment; private Environment environment;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
private NamingService namingService; private NamingService namingService;
private NamingMaintainService namingMaintainService; private NamingMaintainService namingMaintainService;
@ -272,12 +281,16 @@ public class NacosDiscoveryProperties {
Properties properties = getNacosProperties(); Properties properties = getNacosProperties();
this.namingService = createNamingService(properties); this.namingService = createNamingService(properties);
this.namingMaintainService = createMaintainService(properties); this.namingMaintainService = createMaintainService(properties);
if (needNewlyRegister) {
nacosAutoServiceRegistration.start();
}
} }
@PreDestroy @PreDestroy
public void destroy() throws NacosException { public void destroy() throws NacosException {
namingService.shutDown(); shutdownNacosService();
} }
public String getEndpoint() { public String getEndpoint() {
return endpoint; return endpoint;
@ -598,4 +611,9 @@ public class NacosDiscoveryProperties {
return sb.toString(); return sb.toString();
} }
private void shutdownNacosService() throws NacosException {
nacosAutoServiceRegistration.stop();
needNewlyRegister = true;
}
} }

View File

@ -19,6 +19,7 @@ package com.alibaba.cloud.nacos.registry;
import java.util.List; import java.util.List;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties; import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.Instance;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -101,7 +102,12 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
@Override @Override
public void close() { public void close() {
try {
nacosDiscoveryProperties.namingServiceInstance().shutDown();
}
catch (NacosException e) {
log.error("Nacos namingService shutDown failed", e);
}
} }
@Override @Override