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

fix(nacos): fix issue #859

This commit is contained in:
chuntaojun
2019-09-12 20:20:02 +08:00
parent d2684c2882
commit 5162d02b4e
3 changed files with 32 additions and 97 deletions

View File

@@ -18,68 +18,33 @@ package com.alibaba.cloud.nacos;
import com.alibaba.nacos.api.naming.NamingMaintainService;
import com.alibaba.nacos.api.naming.NamingService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Objects;
/**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
*/
public class NacosNamingManager implements ApplicationContextAware {
public class NacosNamingManager {
private static NamingService namingService = null;
private static NamingMaintainService namingMaintainService = null;
@Autowired
private NacosDiscoveryProperties discoveryProperties;
public NamingService getNamingService() {
return ServiceHolder.getInstance().getNamingService();
if (Objects.isNull(namingService)) {
namingService = discoveryProperties.namingServiceInstance();
}
return namingService;
}
public NamingMaintainService getNamingMaintainService() {
return ServiceHolder.getInstance().getNamingMaintainService();
if (Objects.isNull(namingMaintainService)) {
namingMaintainService = discoveryProperties.namingMaintainServiceInstance();
}
return namingMaintainService;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
NacosDiscoveryProperties properties = applicationContext
.getBean(NacosDiscoveryProperties.class);
ServiceHolder holder = ServiceHolder.getInstance();
if (!holder.alreadyInit[0]) {
holder.setNamingService(properties.namingServiceInstance());
}
if (!holder.alreadyInit[1]) {
holder.setNamingMaintainService(properties.namingMaintainServiceInstance());
}
}
static class ServiceHolder {
private NamingService namingService = null;
private NamingMaintainService namingMaintainService = null;
final boolean[] alreadyInit = new boolean[2];
private static final ServiceHolder HOLDER = new ServiceHolder();
ServiceHolder() {
}
static ServiceHolder getInstance() {
return HOLDER;
}
public NamingService getNamingService() {
return namingService;
}
void setNamingService(NamingService namingService) {
alreadyInit[0] = true;
this.namingService = namingService;
}
NamingMaintainService getNamingMaintainService() {
return namingMaintainService;
}
void setNamingMaintainService(NamingMaintainService namingMaintainService) {
alreadyInit[1] = true;
this.namingMaintainService = namingMaintainService;
}
}
}