更改Nacos Config 获取NacosPropertiesSource 列表的方式

pull/200/head
pengbingting 6 years ago
parent 771c2c0f4c
commit 8bd0d8025f

@ -42,11 +42,6 @@ public class NacosConfigAutoConfiguration {
return nacosConfigProperties;
}
@Bean
public NacosPropertySourceRepository nacosPropertySourceRepository() {
return new NacosPropertySourceRepository();
}
@Bean
public NacosRefreshProperties nacosRefreshProperties() {
return new NacosRefreshProperties();

@ -28,9 +28,8 @@ import org.springframework.context.annotation.Configuration;
public class NacosConfigBootstrapConfiguration {
@Bean
public NacosPropertySourceLocator nacosPropertySourceLocator(
NacosConfigProperties nacosConfigProperties) {
return new NacosPropertySourceLocator(nacosConfigProperties);
public NacosPropertySourceLocator nacosPropertySourceLocator() {
return new NacosPropertySourceLocator();
}
@Bean
@ -38,4 +37,10 @@ public class NacosConfigBootstrapConfiguration {
public NacosConfigProperties nacosConfigProperties() {
return new NacosConfigProperties();
}
@Bean
public NacosPropertySourceRepository nacosPropertySourceRepository() {
return new NacosPropertySourceRepository();
}
}

@ -16,54 +16,30 @@
package org.springframework.cloud.alibaba.nacos;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.PropertySource;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author xiaojing
*/
public class NacosPropertySourceRepository implements ApplicationContextAware {
private ApplicationContext applicationContext;
public class NacosPropertySourceRepository {
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
private ConcurrentHashMap<String, NacosPropertySource> nacosPropertySourceRepository = new ConcurrentHashMap<>();
/**
* @return all nacos properties from application context
*/
public List<NacosPropertySource> getAll() {
List<NacosPropertySource> result = new ArrayList<>();
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext;
for (PropertySource p : ctx.getEnvironment().getPropertySources()) {
if (p instanceof NacosPropertySource) {
result.add((NacosPropertySource) p);
}
else if (p instanceof CompositePropertySource) {
collectNacosPropertySources((CompositePropertySource) p, result);
}
}
result.addAll(nacosPropertySourceRepository.values());
return result;
}
private void collectNacosPropertySources(CompositePropertySource composite,
List<NacosPropertySource> result) {
for (PropertySource p : composite.getPropertySources()) {
if (p instanceof NacosPropertySource) {
result.add((NacosPropertySource) p);
}
else if (p instanceof CompositePropertySource) {
collectNacosPropertySources((CompositePropertySource) p, result);
}
}
public void collectNacosPropertySources(NacosPropertySource nacosPropertySource) {
nacosPropertySourceRepository.putIfAbsent(nacosPropertySource.getDataId(),
nacosPropertySource);
}
}

@ -16,17 +16,17 @@
package org.springframework.cloud.alibaba.nacos.client;
import java.io.StringReader;
import java.util.*;
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.factory.config.YamlPropertiesFactoryBean;
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.util.StringUtils;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import java.io.StringReader;
import java.util.*;
/**
* @author xiaojing
@ -35,6 +35,9 @@ import com.alibaba.nacos.api.exception.NacosException;
public class NacosPropertySourceBuilder {
private static final Logger LOGGER = LoggerFactory
.getLogger(NacosPropertySourceBuilder.class);
private static final Properties EMPTY_PROPERTIES = new Properties();
private NacosPropertySourceRepository nacosPropertySourceRepository;
private ConfigService configService;
private long timeout;
@ -67,10 +70,12 @@ public class NacosPropertySourceBuilder {
boolean isRefreshable) {
Properties p = loadNacosData(dataId, group, fileExtension);
if (p == null) {
return null;
p = EMPTY_PROPERTIES;
}
return new NacosPropertySource(group, dataId, propertiesToMap(p), new Date(),
isRefreshable);
NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
propertiesToMap(p), new Date(), isRefreshable);
nacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource);
return nacosPropertySource;
}
private Properties loadNacosData(String dataId, String group, String fileExtension) {
@ -122,4 +127,13 @@ public class NacosPropertySourceBuilder {
}
return result;
}
public NacosPropertySourceRepository getNacosPropertySourceRepository() {
return nacosPropertySourceRepository;
}
public void setNacosPropertySourceRepository(
NacosPropertySourceRepository nacosPropertySourceRepository) {
this.nacosPropertySourceRepository = nacosPropertySourceRepository;
}
}

@ -16,12 +16,12 @@
package org.springframework.cloud.alibaba.nacos.client;
import java.util.Arrays;
import java.util.List;
import com.alibaba.nacos.api.config.ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
@ -29,7 +29,8 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
import com.alibaba.nacos.api.config.ConfigService;
import java.util.Arrays;
import java.util.List;
/**
* @author xiaojing
@ -47,10 +48,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
"yaml", "yml");
@Autowired
private NacosConfigProperties nacosConfigProperties;
public NacosPropertySourceLocator(NacosConfigProperties nacosConfigProperties) {
this.nacosConfigProperties = nacosConfigProperties;
@Autowired
private NacosPropertySourceRepository nacosPropertySourceRepository;
public NacosPropertySourceLocator() {
}
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
@ -68,7 +72,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
long timeout = nacosConfigProperties.getTimeout();
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
timeout);
nacosPropertySourceBuilder
.setNacosPropertySourceRepository(nacosPropertySourceRepository);
String name = nacosConfigProperties.getName();
String nacosGroup = nacosConfigProperties.getGroup();

@ -16,15 +16,9 @@
package org.springframework.cloud.alibaba.nacos.refresh;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
@ -36,9 +30,14 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.util.StringUtils;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* On application start up, NacosContextRefresher add nacos listeners to all application
@ -81,9 +80,8 @@ public class NacosContextRefresher
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// many Spring context
if (this.ready.get()) {
if (this.ready.compareAndSet(true, false)) {
this.registerNacosListenersForApplications();
this.ready.compareAndSet(true, false);
}
}

Loading…
Cancel
Save