您好,登錄后才能下訂單哦!
本篇內容介紹了“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: 31091 spring: 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; /** * @Description spring容器初始化完成后進行一些其他初始化操作 * @date Mar 24, 2022 * @version 1.0 */ @Slf4j @Component public 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
/** * @Description 動態郵箱單例 * @date Mar 24, 2022 * @version 1.0 */ 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; /** * @Description 動態操作yml配置文件工具類 * 【需要將config配置文件夾和項目jar包放在同級別目錄下,這樣修改config下的配置文件后,jvm才能及時得獲取新的配置】 * @date Mar 24, 2022 * @version 1.0 */ 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 @RestController public class TestController { @GetMapping("/getDynamicMail") public String getDynamicMail() { String dynamicMail = DynamicMailConfig.getInstance().getDynamicMail(); log.info("getDynamicMail: " + dynamicMail); return dynamicMail; } }
打包后配置文件放在與jar
同級的config
目錄下
“SpringBoot怎么動態修改yml配置文件”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。