mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #200 from pbting/master
change the style for get the list of NacosPropertiesSource
This commit is contained in:
commit
24ead9599d
@ -42,11 +42,6 @@ public class NacosConfigAutoConfiguration {
|
|||||||
return nacosConfigProperties;
|
return nacosConfigProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public NacosPropertySourceRepository nacosPropertySourceRepository() {
|
|
||||||
return new NacosPropertySourceRepository();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public NacosRefreshProperties nacosRefreshProperties() {
|
public NacosRefreshProperties nacosRefreshProperties() {
|
||||||
return new NacosRefreshProperties();
|
return new NacosRefreshProperties();
|
||||||
|
@ -28,9 +28,8 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
public class NacosConfigBootstrapConfiguration {
|
public class NacosConfigBootstrapConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public NacosPropertySourceLocator nacosPropertySourceLocator(
|
public NacosPropertySourceLocator nacosPropertySourceLocator() {
|
||||||
NacosConfigProperties nacosConfigProperties) {
|
return new NacosPropertySourceLocator();
|
||||||
return new NacosPropertySourceLocator(nacosConfigProperties);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -38,4 +37,10 @@ public class NacosConfigBootstrapConfiguration {
|
|||||||
public NacosConfigProperties nacosConfigProperties() {
|
public NacosConfigProperties nacosConfigProperties() {
|
||||||
return new NacosConfigProperties();
|
return new NacosConfigProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public NacosPropertySourceRepository nacosPropertySourceRepository() {
|
||||||
|
return new NacosPropertySourceRepository();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,54 +16,30 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos;
|
package org.springframework.cloud.alibaba.nacos;
|
||||||
|
|
||||||
|
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
public class NacosPropertySourceRepository implements ApplicationContextAware {
|
public class NacosPropertySourceRepository {
|
||||||
|
|
||||||
private ApplicationContext applicationContext;
|
private ConcurrentHashMap<String, NacosPropertySource> nacosPropertySourceRepository = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return all nacos properties from application context
|
* @return all nacos properties from application context
|
||||||
*/
|
*/
|
||||||
public List<NacosPropertySource> getAll() {
|
public List<NacosPropertySource> getAll() {
|
||||||
List<NacosPropertySource> result = new ArrayList<>();
|
List<NacosPropertySource> result = new ArrayList<>();
|
||||||
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext;
|
result.addAll(nacosPropertySourceRepository.values());
|
||||||
for (PropertySource p : ctx.getEnvironment().getPropertySources()) {
|
|
||||||
if (p instanceof NacosPropertySource) {
|
|
||||||
result.add((NacosPropertySource) p);
|
|
||||||
}
|
|
||||||
else if (p instanceof CompositePropertySource) {
|
|
||||||
collectNacosPropertySources((CompositePropertySource) p, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void collectNacosPropertySources(CompositePropertySource composite,
|
public void collectNacosPropertySources(NacosPropertySource nacosPropertySource) {
|
||||||
List<NacosPropertySource> result) {
|
nacosPropertySourceRepository.putIfAbsent(nacosPropertySource.getDataId(),
|
||||||
for (PropertySource p : composite.getPropertySources()) {
|
nacosPropertySource);
|
||||||
if (p instanceof NacosPropertySource) {
|
|
||||||
result.add((NacosPropertySource) p);
|
|
||||||
}
|
|
||||||
else if (p instanceof CompositePropertySource) {
|
|
||||||
collectNacosPropertySources((CompositePropertySource) p, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,17 +16,17 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos.client;
|
package org.springframework.cloud.alibaba.nacos.client;
|
||||||
|
|
||||||
import java.io.StringReader;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
import java.util.*;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||||
|
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
|
||||||
import org.springframework.core.io.ByteArrayResource;
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import java.io.StringReader;
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
@ -35,6 +35,9 @@ import com.alibaba.nacos.api.exception.NacosException;
|
|||||||
public class NacosPropertySourceBuilder {
|
public class NacosPropertySourceBuilder {
|
||||||
private static final Logger LOGGER = LoggerFactory
|
private static final Logger LOGGER = LoggerFactory
|
||||||
.getLogger(NacosPropertySourceBuilder.class);
|
.getLogger(NacosPropertySourceBuilder.class);
|
||||||
|
private static final Properties EMPTY_PROPERTIES = new Properties();
|
||||||
|
|
||||||
|
private NacosPropertySourceRepository nacosPropertySourceRepository;
|
||||||
private ConfigService configService;
|
private ConfigService configService;
|
||||||
private long timeout;
|
private long timeout;
|
||||||
|
|
||||||
@ -67,10 +70,12 @@ public class NacosPropertySourceBuilder {
|
|||||||
boolean isRefreshable) {
|
boolean isRefreshable) {
|
||||||
Properties p = loadNacosData(dataId, group, fileExtension);
|
Properties p = loadNacosData(dataId, group, fileExtension);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
return null;
|
p = EMPTY_PROPERTIES;
|
||||||
}
|
}
|
||||||
return new NacosPropertySource(group, dataId, propertiesToMap(p), new Date(),
|
NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
|
||||||
isRefreshable);
|
propertiesToMap(p), new Date(), isRefreshable);
|
||||||
|
nacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource);
|
||||||
|
return nacosPropertySource;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Properties loadNacosData(String dataId, String group, String fileExtension) {
|
private Properties loadNacosData(String dataId, String group, String fileExtension) {
|
||||||
@ -122,4 +127,13 @@ public class NacosPropertySourceBuilder {
|
|||||||
}
|
}
|
||||||
return result;
|
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;
|
package org.springframework.cloud.alibaba.nacos.client;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
||||||
|
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
|
||||||
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
|
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.core.env.CompositePropertySource;
|
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.core.env.PropertySource;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
@ -47,10 +48,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
|
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
|
||||||
"yaml", "yml");
|
"yaml", "yml");
|
||||||
|
|
||||||
|
@Autowired
|
||||||
private NacosConfigProperties nacosConfigProperties;
|
private NacosConfigProperties nacosConfigProperties;
|
||||||
|
|
||||||
public NacosPropertySourceLocator(NacosConfigProperties nacosConfigProperties) {
|
@Autowired
|
||||||
this.nacosConfigProperties = nacosConfigProperties;
|
private NacosPropertySourceRepository nacosPropertySourceRepository;
|
||||||
|
|
||||||
|
public NacosPropertySourceLocator() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
|
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
|
||||||
@ -68,7 +72,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
long timeout = nacosConfigProperties.getTimeout();
|
long timeout = nacosConfigProperties.getTimeout();
|
||||||
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
|
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
|
||||||
timeout);
|
timeout);
|
||||||
|
nacosPropertySourceBuilder
|
||||||
|
.setNacosPropertySourceRepository(nacosPropertySourceRepository);
|
||||||
String name = nacosConfigProperties.getName();
|
String name = nacosConfigProperties.getName();
|
||||||
|
|
||||||
String nacosGroup = nacosConfigProperties.getGroup();
|
String nacosGroup = nacosConfigProperties.getGroup();
|
||||||
|
@ -16,15 +16,9 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos.refresh;
|
package org.springframework.cloud.alibaba.nacos.refresh;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
import java.math.BigInteger;
|
import com.alibaba.nacos.api.config.listener.Listener;
|
||||||
import java.security.MessageDigest;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
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 org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
@ -36,9 +30,14 @@ import org.springframework.context.ApplicationContextAware;
|
|||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import java.io.UnsupportedEncodingException;
|
||||||
import com.alibaba.nacos.api.config.listener.Listener;
|
import java.math.BigInteger;
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
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
|
* On application start up, NacosContextRefresher add nacos listeners to all application
|
||||||
@ -81,9 +80,8 @@ public class NacosContextRefresher
|
|||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationReadyEvent event) {
|
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||||
// many Spring context
|
// many Spring context
|
||||||
if (this.ready.get()) {
|
if (this.ready.compareAndSet(true, false)) {
|
||||||
this.registerNacosListenersForApplications();
|
this.registerNacosListenersForApplications();
|
||||||
this.ready.compareAndSet(true, false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user