fix: ConfigUtil 读取配置失败时自动尝试 resources/ 目录

当文件系统直接读取 app.yml 失败时(如 Docker 卷挂载场景),
ConfigUtil.readConfig 现在会自动尝试 resources/ 子目录作为
fallback,确保配置文件在各种部署方式下都能被正确加载。
This commit is contained in:
yukaidi
2026-05-29 11:54:13 +08:00
parent 4cfcdfa1f8
commit 3461532679

View File

@@ -10,6 +10,8 @@ import io.vertx.core.json.JsonObject;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* 异步读取配置工具类
@@ -67,10 +69,30 @@ public class ConfigUtil {
retriever.close();
})
.onFailure(err -> {
// 配置读取失败,直接返回失败 Future
promise.fail(new RuntimeException(
"读取配置文件失败: " + path, err));
retriever.close();
// 读取失败时,尝试从 resources/ 子目录读取(兼容 Docker 卷挂载场景)
String resourcesPath = "resources/" + path;
if (!path.startsWith("resources/") && Files.exists(Path.of(resourcesPath))) {
ConfigStoreOptions fallbackStore = new ConfigStoreOptions()
.setType("file")
.setFormat(format)
.setConfig(new JsonObject().put("path", resourcesPath));
ConfigRetriever fallbackRetriever = ConfigRetriever
.create(vertx, new ConfigRetrieverOptions().addStore(fallbackStore));
fallbackRetriever.getConfig()
.onSuccess(config -> {
promise.complete(config);
fallbackRetriever.close();
})
.onFailure(e2 -> {
promise.fail(new RuntimeException(
"读取配置文件失败: " + path + " (也尝试了 " + resourcesPath + ")", e2));
fallbackRetriever.close();
});
} else {
promise.fail(new RuntimeException(
"读取配置文件失败: " + path, err));
}
});
return promise.future();