mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #920 from chuntaojun/fix_issue_859
fix(nacos): fix issue #859
This commit is contained in:
commit
7c452f6f7f
@ -88,10 +88,14 @@ class SampleController {
|
||||
String userName;
|
||||
|
||||
@Value("${user.age:25}")
|
||||
int age;
|
||||
Integer age;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
@RequestMapping("/user")
|
||||
public String simple() {
|
||||
return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!";
|
||||
return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!"
|
||||
+ nacosConfigManager.getConfigService();
|
||||
}
|
||||
}
|
@ -19,36 +19,36 @@ package com.alibaba.cloud.nacos;
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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 NacosConfigManager implements ApplicationContextAware {
|
||||
public class NacosConfigManager {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(NacosConfigManager.class);
|
||||
private ConfigService configService;
|
||||
|
||||
private static ConfigService service = null;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
public ConfigService getConfigService() {
|
||||
return configService;
|
||||
if (Objects.isNull(service)) {
|
||||
try {
|
||||
service = NacosFactory
|
||||
.createConfigService(properties.getConfigServiceProperties());
|
||||
properties.initConfigService(service);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.error("create config service error!properties={},e=,", properties, e);
|
||||
}
|
||||
}
|
||||
return service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
NacosConfigProperties properties = applicationContext
|
||||
.getBean(NacosConfigProperties.class);
|
||||
try {
|
||||
configService = NacosFactory
|
||||
.createConfigService(properties.getConfigServiceProperties());
|
||||
properties.initConfigService(configService);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.error("create config service error!properties={},e=,", properties, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,32 +18,37 @@ 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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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 NamingService namingService;
|
||||
private NamingMaintainService namingMaintainService;
|
||||
private static final Logger log = LoggerFactory.getLogger(NacosNamingManager.class);
|
||||
|
||||
private static NamingService namingService = null;
|
||||
private static NamingMaintainService namingMaintainService = null;
|
||||
|
||||
@Autowired
|
||||
private NacosDiscoveryProperties discoveryProperties;
|
||||
|
||||
public NamingService getNamingService() {
|
||||
if (Objects.isNull(namingService)) {
|
||||
namingService = discoveryProperties.namingServiceInstance();
|
||||
}
|
||||
return namingService;
|
||||
}
|
||||
|
||||
public NamingMaintainService getNamingMaintainService() {
|
||||
if (Objects.isNull(namingMaintainService)) {
|
||||
namingMaintainService = discoveryProperties.namingMaintainServiceInstance();
|
||||
}
|
||||
return namingMaintainService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
NacosDiscoveryProperties properties = applicationContext
|
||||
.getBean(NacosDiscoveryProperties.class);
|
||||
namingService = properties.namingServiceInstance();
|
||||
namingMaintainService = properties.namingMaintainServiceInstance();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user