本篇内容介绍了“SpringBoot怎么动态修改yml配置文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
前言
记录下SpringBoot
修改yml
配置文件后无需重启服务的方式(打包后生效),效果如下:
具体实现
实现代码
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency></dependencies>
application.yml
#端口号server: port: 31091spring: profiles: active: dev
application-dev.yml
coisini: mail: maggieq8324@gmail.com
InitializationConfig.java
import lombok.extern.slf4j.Slf4j;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.stereotype.Component;import java.util.Map;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;@Slf4j@Componentpublic class InitializationConfig implements ApplicationRunner { private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); private static String profile; @Override public void run(ApplicationArguments applicationArguments) throws Exception { scheduleUpdateConf(); } private void scheduleUpdateConf() { try { Map lhm = YmlUtil.loadYaml("application.yml"); profile = (String) YmlUtil.getValByKey(lhm, "spring.profiles.active"); } catch (Exception e) { log.error("加载配置文件application.yml异常"); } // TODO 开启定时刷新内存中配置文件内容 log.info("refresh config file start"); executorService.scheduleAtFixedRate(InitializationConfig::updateConfVal, 0, 10, TimeUnit.SECONDS); log.info("refresh config file end"); } private static void updateConfVal(){ try{ Map lhm = YmlUtil.loadYaml("application-" + profile + ".yml"); String mail = YmlUtil.getValByKey(lhm,"coisini.mail").toString(); DynamicMailConfig instance = DynamicMailConfig.getInstance(); if (!instance.getDynamicMail().equals(mail)) { instance.setDynamicMail(mail); log.info("实时配置mail更新:" + instance.getDynamicMail()); } } catch (Exception e){ log.error("更新配置文件值异常: ", e); } }}
DynamicMailConfig.java
public class DynamicMailConfig { private static String mail; private final static DynamicMailConfig dynamic; static { dynamic = new DynamicMailConfig(); } private DynamicMailConfig() { mail = ""; } public static DynamicMailConfig getInstance() { return dynamic; } public String getDynamicMail() { return mail; } public void setDynamicMail(String mail) { this.mail = mail; }}
YmlUtil.java
import org.yaml.snakeyaml.Yaml;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.util.LinkedHashMap;import java.util.Map;public class YmlUtil { public static LinkedHashMap loadYaml(String fileName) throws Exception{ String path = System.getProperty("user.dir"); File file = new File(path + "/config/" + fileName); InputStream in; if (file.exists()) { in = new FileInputStream(path + "/config/" + fileName); } else { // TODO 如果没有config文件夹,则从项目的resources目录下找 in = YmlUtil.class.getClassLoader().getResourceAsStream(fileName); } LinkedHashMap lhm = new Yaml().loadAs(in, LinkedHashMap.class); return lhm; } public static Object getValByKey(Map lhm, String key) throws Exception{ String[] keys = key.split("[.]"); Map ymlInfo = lhm; for (int i = 0; i < keys.length; i++) { Object value = ymlInfo.get(keys[i]); if (i < keys.length - 1) { ymlInfo = (Map) value; } else if (value == null) { throw new Exception("key不存在"); } else { return value; } } return null; }}
测试
TestController.java
@Slf4j@RestControllerpublic class TestController { @GetMapping("/getDynamicMail") public String getDynamicMail() { String dynamicMail = DynamicMailConfig.getInstance().getDynamicMail(); log.info("getDynamicMail: " + dynamicMail); return dynamicMail; }}
打包后配置文件放在与jar
同级的config
目录下
“SpringBoot怎么动态修改yml配置文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!