|
|
|
@ -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 {
|
|
|
|
|
|
|
|
|
|
public NamingService getNamingService() {
|
|
|
|
|
return ServiceHolder.getInstance().getNamingService();
|
|
|
|
|
}
|
|
|
|
|
private static NamingService namingService = null;
|
|
|
|
|
private static NamingMaintainService namingMaintainService = null;
|
|
|
|
|
|
|
|
|
|
public NamingMaintainService getNamingMaintainService() {
|
|
|
|
|
return ServiceHolder.getInstance().getNamingMaintainService();
|
|
|
|
|
}
|
|
|
|
|
@Autowired
|
|
|
|
|
private NacosDiscoveryProperties discoveryProperties;
|
|
|
|
|
|
|
|
|
|
@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());
|
|
|
|
|
public NamingService getNamingService() {
|
|
|
|
|
if (Objects.isNull(namingService)) {
|
|
|
|
|
namingService = discoveryProperties.namingServiceInstance();
|
|
|
|
|
}
|
|
|
|
|
return namingService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
public NamingMaintainService getNamingMaintainService() {
|
|
|
|
|
if (Objects.isNull(namingMaintainService)) {
|
|
|
|
|
namingMaintainService = discoveryProperties.namingMaintainServiceInstance();
|
|
|
|
|
}
|
|
|
|
|
return namingMaintainService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|