成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

顛覆傳統!SpringBoot 配置文件熱加載實戰,省下 50% 重啟時間

開發 前端
在實際項目迭代中,頻繁調整配置是常態,而頻繁重啟服務則是浪費。通過本方案,我們可以大幅提升配置變更效率,節省無謂等待時間,提高服務的穩定性與響應速度。

在構建和運維基于 Spring Boot 的應用過程中,配置文件(如 application.yml 或 application.properties)充當著不可替代的角色。它們承載了項目的環境、服務參數、版本標識等關鍵信息。然而,傳統配置管理方式下,任何一次配置修改都需重啟服務,這在多環境部署和頻繁調試場景中尤顯低效。

為了從根本上改善這一痛點,本文將系統介紹如何基于 Spring Boot 3.4.5 + Spring Cloud Context 實現無需重啟即可動態更新配置文件的機制,覆蓋原理解析、實戰代碼、注意事項,助你提升至少 50% 的配置變更效率。

技術方案概覽

我們選用 Spring Cloud 提供的 ContextRefresher 作為核心實現,結合 Apache Commons IO 實現本地配置文件變更監聽,構建完整的熱加載能力:

  • 監聽配置文件變化;
  • 檢測到變化后刷新 Spring 容器中的 Bean;
  • 生效的配置必須綁定為 @ConfigurationProperties 且標注 @RefreshScope。

引入依賴(pom.xml)

在 pom.xml 中添加如下依賴:

<!-- 文件變更監聽 -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>


<!-- 動態配置刷新 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-context</artifactId>
    <version>4.1.0</version>
</dependency>

編寫基礎配置文件

路徑:src/main/resources/application.yml

app:
  name: auto-config-demo
  version: 1.0.0
  description: 示例應用 - 支持配置動態更新

編寫配置綁定類

文件路徑:/src/main/java/com/icoderoad/config/ProjectConfig.java

package com.icoderoad.config;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;


@Component
@RefreshScope
@ConfigurationProperties(prefix = "app")
public class ProjectConfig {
    private String name;
    private String version;
    private String description;


    // Getter & Setter
}

說明:

  • @RefreshScope 是核心,保證配置類可在刷新時重新加載;
  • 使用 @ConfigurationProperties 綁定配置前綴。

編寫文件監聽器

文件路徑:/src/main/java/com/icoderoad/listener/ConfigFileChangeListener.java

package com.icoderoad.listener;


import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;


import java.io.File;
import java.net.URL;
import java.nio.file.*;


@Slf4j
@Component
public class ConfigFileChangeListener {


    @Autowired
    private ApplicationContext context;


    @Autowired
    private ContextRefresher refresher;


    @PostConstruct
    public void init() throws Exception {
        String configPath = resolveConfigPath();
        if (configPath == null) return;


        File configFile = new File(configPath);
        FileAlterationObserver observer = new FileAlterationObserver(configFile.getParentFile());
        observer.addListener(new FileAlterationListenerAdaptor() {
            @Override
            public void onFileChange(File file) {
                if (file.getName().equals(configFile.getName())) {
                    log.info("配置文件變更:{}", file.getAbsolutePath());
                    refresher.refresh();
                    log.info("刷新成功!");
                }
            }
        });


        FileAlterationMonitor monitor = new FileAlterationMonitor(1000, observer);
        monitor.start();
        log.info("配置監聽器啟動成功,監聽路徑:{}", configPath);
    }


    private String resolveConfigPath() throws Exception {
        Environment env = context.getEnvironment();
        String configLocation = env.getProperty("spring.config.location");
        if (configLocation != null && !configLocation.isEmpty()) {
            return configLocation;
        }


        String defaultPath = "classpath:application.yml";
        if (context.getResource(defaultPath).exists()) {
            if (isRunningInJar()) {
                Path externalConfig = exportToExternal(defaultPath);
                return externalConfig.toString();
            } else {
                return context.getResource(defaultPath).getFile().getAbsolutePath();
            }
        }


        log.warn("未找到有效配置文件路徑");
        return null;
    }


    private boolean isRunningInJar() {
        try {
            URL location = getClass().getProtectionDomain().getCodeSource().getLocation();
            return location.getPath().endsWith(".jar");
        } catch (Exception e) {
            return false;
        }
    }


    private Path exportToExternal(String classpath) throws Exception {
        Path configDir = Paths.get(System.getProperty("user.dir"), "config");
        Files.createDirectories(configDir);


        String filename = classpath.substring("classpath:".length());
        Path configFile = configDir.resolve(filename);
        if (!Files.exists(configFile)) {
            try (var in = context.getResource(classpath).getInputStream()) {
                Files.copy(in, configFile);
            }
        }


        return configFile;
    }
}

提供測試接口

文件路徑:/src/main/java/com/icoderoad/controller/ConfigController.java

package com.icoderoad.controller;


import com.icoderoad.config.ProjectConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/config")
public class ConfigController {


    @Autowired
    private ProjectConfig config;


    @GetMapping("/info")
    public ProjectConfig getInfo() {
        return config;
    }
}

調用 /config/info 接口可實時查看當前配置是否變更成功。

注意事項

  • 配置類必須使用 @ConfigurationProperties 和 @RefreshScope
  • 數據源配置、端口、Redis連接等啟動相關配置變更后不支持熱刷新;
  • 推薦將配置文件外置(配合 --spring.config.location)提高運維靈活性;
  • 熱加載僅對非結構性配置(如描述、開關、調試參數)更有意義。

寫在最后

在實際項目迭代中,頻繁調整配置是常態,而頻繁重啟服務則是浪費。通過本方案,我們可以大幅提升配置變更效率,節省無謂等待時間,提高服務的穩定性與響應速度。Spring Boot 配合 Spring Cloud Context + Commons IO,即可輕松實現配置熱加載,優雅應對變更。

代碼路徑統一為 /src/main/java/com/icoderoad/... 配置文件推薦放置于 /config 目錄,并通過 --spring.config.location 顯式聲明。

責任編輯:武曉燕 來源: 路條編程
相關推薦

2021-04-22 07:43:24

后端技術SpringBootdevtools

2009-09-25 10:49:25

Hibernate加載

2022-03-23 07:31:34

SpringBoot加載配置

2022-04-20 20:27:51

Hydra配置文件開發工具

2021-04-01 10:23:45

SpringBootbootstrapapplication

2014-03-12 10:31:32

大數據

2021-08-02 18:23:01

Spring隱私數據

2010-02-02 18:19:52

Linux mplay

2017-04-18 11:33:03

人工智能云計算醫療

2024-04-23 14:13:38

開發配置文件

2015-05-11 11:06:43

統一基礎架構系統華三通信UIS

2017-12-20 11:55:38

RAID2.0傳統方式

2018-09-10 11:00:21

區塊鏈互聯網新聞行業

2013-08-06 17:38:20

2011-01-19 14:00:21

2011-01-13 16:27:26

Linux配置文件

2016-11-21 14:32:19

華為物聯網

2024-02-28 12:23:12

2016-01-22 13:49:27

管理軟件協同

2015-06-04 15:55:00

升騰資訊/云教室
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99热.com| 久久久久91 | 国产欧美精品一区二区色综合朱莉 | 久久久精品网站 | 日本电影韩国电影免费观看 | 天天躁天天操 | 中文字幕动漫成人 | 精品一区二区三区日本 | 欧美成人精品一区二区男人看 | 久久久激情 | 日本a视频 | 精品国产精品一区二区夜夜嗨 | 久久精品欧美一区二区三区麻豆 | 免费二区| 欧美视频在线一区 | 国产精品久久久久久久久久久免费看 | 亚洲啪啪 | 一级欧美黄色片 | 九九热re | 99亚洲精品 | 国产精品精品视频一区二区三区 | 黄色免费三级 | 91免费在线 | 久久午夜精品 | 亚洲国产精品人人爽夜夜爽 | 韩日精品在线观看 | 日本三级全黄三级a | 亚洲视频欧美视频 | 精品久久久久久久久久久 | 国产精品久久久久久久久久了 | 久久99精品视频 | 久久69精品久久久久久国产越南 | 夜夜骚视频 | 日韩欧美在线观看视频网站 | 久久久久久国产精品免费免费狐狸 | 国产精品成人一区二区 | 亚洲一区在线播放 | 我要看免费一级毛片 | 草比av| 欧美在线视频网 | 国产乱码精品一区二区三区忘忧草 |