您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“SpringBoot怎么動(dòng)態(tài)修改yml配置文件”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
記錄下SpringBoot
修改yml
配置文件后無需重啟服務(wù)的方式(打包后生效),效果如下:
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
#端口號(hào) 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容器初始化完成后進(jìn)行一些其他初始化操作 * @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 開啟定時(shí)刷新內(nèi)存中配置文件內(nèi)容 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("實(shí)時(shí)配置mail更新:" + instance.getDynamicMail()); } } catch (Exception e){ log.error("更新配置文件值異常: ", e); } } }
DynamicMailConfig.java
/** * @Description 動(dòng)態(tài)郵箱單例 * @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 動(dòng)態(tài)操作yml配置文件工具類 * 【需要將config配置文件夾和項(xiàng)目jar包放在同級(jí)別目錄下,這樣修改config下的配置文件后,jvm才能及時(shí)得獲取新的配置】 * @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文件夾,則從項(xiàng)目的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
同級(jí)的config
目錄下
“SpringBoot怎么動(dòng)態(tài)修改yml配置文件”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。