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:
parent
d2684c2882
commit
5162d02b4e
@ -90,11 +90,8 @@ class SampleController {
|
|||||||
@Value("${user.age:25}")
|
@Value("${user.age:25}")
|
||||||
Integer age;
|
Integer age;
|
||||||
|
|
||||||
private final NacosConfigManager nacosConfigManager;
|
@Autowired
|
||||||
|
private NacosConfigManager nacosConfigManager;
|
||||||
public SampleController(NacosConfigManager nacosConfigManager) {
|
|
||||||
this.nacosConfigManager = nacosConfigManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping("/user")
|
@RequestMapping("/user")
|
||||||
public String simple() {
|
public String simple() {
|
||||||
|
@ -17,52 +17,25 @@
|
|||||||
package com.alibaba.cloud.nacos;
|
package com.alibaba.cloud.nacos;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
|
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
|
||||||
*/
|
*/
|
||||||
public class NacosConfigManager implements ApplicationContextAware {
|
public class NacosConfigManager {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NacosConfigProperties properties;
|
||||||
|
|
||||||
|
private static ConfigService service = null;
|
||||||
|
|
||||||
public ConfigService getConfigService() {
|
public ConfigService getConfigService() {
|
||||||
return ServiceHolder.getInstance().getService();
|
if (Objects.isNull(service)) {
|
||||||
}
|
service = properties.configServiceInstance();
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext)
|
|
||||||
throws BeansException {
|
|
||||||
NacosConfigProperties properties = applicationContext
|
|
||||||
.getBean(NacosConfigProperties.class);
|
|
||||||
ServiceHolder holder = ServiceHolder.getInstance();
|
|
||||||
if (!holder.alreadyInit) {
|
|
||||||
ServiceHolder.getInstance().setService(properties.configServiceInstance());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class ServiceHolder {
|
|
||||||
private ConfigService service = null;
|
|
||||||
|
|
||||||
private boolean alreadyInit = false;
|
|
||||||
|
|
||||||
private static final ServiceHolder holder = new ServiceHolder();
|
|
||||||
|
|
||||||
ServiceHolder() {
|
|
||||||
}
|
|
||||||
|
|
||||||
static ServiceHolder getInstance() {
|
|
||||||
return holder;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setService(ConfigService service) {
|
|
||||||
alreadyInit = true;
|
|
||||||
this.service = service;
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigService getService() {
|
|
||||||
return service;
|
|
||||||
}
|
}
|
||||||
|
return service;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,68 +18,33 @@ package com.alibaba.cloud.nacos;
|
|||||||
|
|
||||||
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;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
|
* @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() {
|
public NamingService getNamingService() {
|
||||||
return ServiceHolder.getInstance().getNamingService();
|
if (Objects.isNull(namingService)) {
|
||||||
|
namingService = discoveryProperties.namingServiceInstance();
|
||||||
|
}
|
||||||
|
return namingService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NamingMaintainService getNamingMaintainService() {
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user