SpringBoot应用获取远程自动配置参数的简便方法
1.问题的抛出
SpringBoot已经成为当前java应用开发的首选框架,它将程序员从以往繁琐的spring mvc配置文件中解脱出来,使用自动配置方式构建spring应用的IOC容器,十分方便。
SpringBoot的使用者往往将需要自动配置的类的属性放入application.yml文件中,SpringBoot应用在启动时会按一定规则读取应用配置文件,并把配置文件中的属性放入应用的环境变量中,应用中部署的自动配置类会从应用的环境变量中获取这些属性作为自动配置的参数完成对象的注入。
在实际应用中,配置在application.yml的属性参数往往需要动态获取,比较典型的如使用spring cloud config和k8s的configmap从配置中心处获取配置数据。
如何在SpringBoot应用启动后动态获取属性参数并且还让自动配置类来使用这些属性参数,网上有很多文章。本文介绍一种比较简单的方法。
2.SpringBoot的自动配置过程分析
首先我们来分析一下SpringBoot的应用启动过程,看看IOC容器是如何初始化的,在初始化的过程中可供程序员实现哪些扩展点。
SpringApplication的run方法如下所示:
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
这个过程中使用prepareEnvironment函数初始化环境变量environment,并在prepareContext函数中为context中的bean注入做准备,在prepareContext函数中:
private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
context.setEnvironment(environment);
postProcessApplicationContext(context);
applyInitializers(context);
listeners.contextPrepared(context);
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
// Add boot specific singleton beans
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
listeners.contextLoaded(context);
}
applyInitializers(context)函数会执行用户自定义的ApplicationContextInitializer,我们可以在用户自定义的ApplicationContextInitializer中加入从远程获取的配置参数放入环境变量中覆盖掉从application.yml中读取的配置参数,这样自动配置类从环境变量中获取的配置参数就是我们从远程获取的配置参数。
3 代码实例
假设配置文件application.yml如下:
server:
port: 9000
spring:
application:
name: zk-test1
datasource:
password: zhangkai
url: jdbc:mysql://172.16.249.205:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
mvc:
view:
prefix: /WEB-INF/
suffix: .jsp
我们希望从远程获取datasource的url地址,首先我们定义一个ApplicationContextInitializer接口的实现类ZkApplicationContextInitializer并加入到SpringApplication的Initializers中,代码如下:
public class ZktestApplication implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ZktestApplication.class);
app.addInitializers(new ZkApplicationContextInitializer());
ApplicationContext ctx = app.run( args);
}
}
ZkApplicationContextInitializer定义如下:
@Slf4j
public class ZkApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment ce = applicationContext.getEnvironment();
log.info("spring.datasource.url before:{}", ce.getProperty("spring.datasource.url"));
Properties properties = new Properties();
properties.put("spring.datasource.url", getRemoteConfigedDbUrl("spring.datasource.url"));
PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("remote", properties);
ce.getPropertySources().addFirst(propertiesPropertySource);
PropertySource ps1 = ce.getPropertySources().get("applicationConfig: [classpath:/application.yml]");
log.info("applicationConfig:{}", ps1.getProperty("spring.datasource.url").toString());
PropertySource ps2 = ce.getPropertySources().get("remote");
log.info("remote:{}", ps2.getProperty("spring.datasource.url").toString());
log.info("spring.datasource.url after:{}", ce.getProperty("spring.datasource.url"));
}
private String getRemoteConfigedDbUrl(String propertname) {
return "jdbc:mysql://localhost:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
}
}
在这段代码中,我们使用getRemoteConfigedDbUrl模拟向远程获取datasource的操作。在 initialize函数中,首先使用applicationContext.getEnvironment()获取当前应用的环境ce,当前环境中有多个PropertySource,其中名为“applicationConfig: [classpath:/application.yml]”的PropertySource中存放的配置参数就是从application.yml中获取的配置参数。这里我们建立一个名为"remote"的PropertySource,并放在ce的PropertySource列表中。注意这里我们使用addFirst将"remote"的PropertySource放在列表的最前面,是因为如果多个PropertySource中如果有相同的属性,则ce.getProperty()函数获取的是最前面的PropertySource中的属性值。
4 运行结果
运行ZktestApplication,可从运行结果中看到数据库的Datasource对象使用的自动配置参数为使用getRemoteConfigedDbUrl()函数获取的url。其中initialize函数的执行结果如下:
2020-01-17 15:48:47.004 INFO 7672 --- [ restartedMain] c.k.z.ZkApplicationContextInitializer : spring.datasource.url before:jdbc:mysql://172.16.249.205:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2020-01-17 15:48:47.008 INFO 7672 --- [ restartedMain] c.k.z.ZkApplicationContextInitializer : applicationConfig:jdbc:mysql://172.16.249.205:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2020-01-17 15:48:47.008 INFO 7672 --- [ restartedMain] c.k.z.ZkApplicationContextInitializer : remote:jdbc:mysql://localhost:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2020-01-17 15:48:47.008 INFO 7672 --- [ restartedMain] c.k.z.ZkApplicationContextInitializer : spring.datasource.url after:jdbc:mysql://localhost:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2020-01-17 15:48:47.012 INFO 7672 --- [ restartedMain] com.kedacom.zktest.ZktestApplication : Starting ZktestApplication on zhangkai_kedacom with PID 7672 (E:\javacode\springworkspace\zktest\target\classes started by Administrator in E:\javacode\springworkspace\zktest)