Created
August 14, 2018 06:13
-
-
Save chenyhd/069b31067c0bcefcfc503b6161a0ec4f to your computer and use it in GitHub Desktop.
非Springboot项目从config-server读取配置文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.application.name=XXapplicaiton | |
spring.cloud.config.url=http://config-server.com:1212/ | |
#值可以是 空,local,以及远程配置文件的后缀 | |
#空和local会读取本地配置文件,云端配置文件读取失败也会读取本地配置文件 | |
spring.cloud.config.profile= | |
#spring.cloud.config.failFast=false #如果读取远程配置失败项目直接宕掉 | |
#读取分支默认master | |
spring.cloud.config.label=master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CloudEnvironment extends StandardServletEnvironment { | |
static Logger logger = LoggerFactory.getLogger(CloudEnvironment.class); | |
@Override | |
protected void customizePropertySources(MutablePropertySources propertySources) { | |
super.customizePropertySources(propertySources); | |
try { | |
//用来添加应用名到environment中 | |
propertySources.addLast(initResourcePropertySourceLocator("cloud-config-context.properties")); | |
//添加config Server的配置 | |
PropertySource<?> source = initConfigServicePropertySourceLocator(this); | |
if (source == null) { | |
logger.error("******Unable to read the remote configuration file, will read the configuration file from local******"); | |
logger.error("*************local-config.properties*************"); | |
propertySources.addLast(initResourcePropertySourceLocator("local-config.properties")); | |
return; | |
} | |
propertySources.addLast(source); | |
} catch (Exception ex) { | |
logger.warn("failed to initialize cloud config environment", ex); | |
} | |
} | |
private PropertySource<?> initConfigServicePropertySourceLocator(Environment environment) { | |
// cloud-config-context.properties 中的属性并不会自动封装到ConfigClientProperties中, | |
// 所以采用手动封装的方式 | |
String profile = environment.getProperty(ConfigClientProperties.PREFIX + ".profile"); | |
if (StringUtils.isBlank(profile)) { | |
logger.error("No configuration file specified,spring.cloud.config.profile is null"); | |
return null; | |
} | |
if (StringUtils.equals(profile, "local")) { | |
logger.error("Specify a local profile"); | |
return null; | |
} | |
ConfigClientProperties configClientProperties = new ConfigClientProperties(environment); | |
configClientProperties.setProfile(profile); | |
configClientProperties.setUri(environment.getProperty(ConfigClientProperties.PREFIX + ".url", | |
"http://config-center.com:1212/")); | |
configClientProperties.setLabel(environment.getProperty( | |
ConfigClientProperties.PREFIX + ".label", "master")); | |
configClientProperties.setFailFast(Boolean.parseBoolean(environment.getProperty( | |
ConfigClientProperties.PREFIX + ".failFast", "false"))); | |
ConfigServicePropertySourceLocator configServicePropertySourceLocator = | |
new ConfigServicePropertySourceLocator(configClientProperties); | |
return configServicePropertySourceLocator.locate(environment); | |
} | |
private PropertySource<?> initResourcePropertySourceLocator(String location) { | |
ResourcePropertySource resourcePropertySource = null; | |
try { | |
Resource resource = new DefaultResourceLoader(this.getClass().getClassLoader()).getResource( | |
location); | |
resourcePropertySource = new ResourcePropertySource(resource); | |
} catch (Exception e) { | |
logger.error(" can not find " + location); | |
} | |
return resourcePropertySource; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment