如何在 Vertx 中设置活动配置文件,类似于 Spring Boot
How to set active profile in Vertx similar to Spring Boot
我设置了一个 vertx-config-git verticle,它跟踪 github 中的配置回购设置。该存储库包含不同文件夹中特定于 DEV、QA、STATE 和 PROD 的配置(json 文件)。但是由于 Vertx Config 提取了所有配置文件,配置被从 repo 读取的最后一个 json 文件覆盖。有没有办法根据部署期间传递的 env 变量仅获取特定于环境的配置?
@Override
public void start(Promise<Void> startPromise) throws Exception {
ConfigStoreOptions env = new ConfigStoreOptions().setType("env");
ConfigStoreOptions git = new ConfigStoreOptions()
.setType("git")
.setConfig(new JsonObject()
.put("url", "https://github.com/kaushik-Das/vertx-config")
.put("path", "local")
.put("filesets",
new JsonArray().add(new JsonObject().put("pattern", "*.json"))));
ConfigRetrieverOptions options = new ConfigRetrieverOptions().addStore(env).addStore(git);
ConfigRetriever retriever = ConfigRetriever.create(vertx,
options.setScanPeriod(1000));
retriever.getConfig().setHandler(ar -> {
if (ar.failed()) {
System.out.println(ar.cause());
} else {
JsonObject config = ar.result();
System.out.println(config.encodePrettily());
}
});
retriever.listen(listener -> {
JsonObject newConfiguration = listener.getNewConfiguration();
System.out.println(newConfiguration);
});
}
从 vert.x v.3.8.5 开始,不支持在 Spring 世界中理解的配置文件。
但是,文件集的模式 follows the Ant style。所以这样的事情应该是可能的:
String stageName = getStageName(); // get stage identifier, e.g. from an environment variable
ConfigStoreOptions env = new ConfigStoreOptions().setType("env");
ConfigStoreOptions git = new ConfigStoreOptions()
.setType("git")
.setConfig(new JsonObject()
.put("url", "https://github.com/kaushik-Das/vertx-config")
.put("path", "local")
.put("filesets",
new JsonArray().add(new JsonObject().put("pattern", "**/" + stageName + "/*.json"))));
如果 stageName
是 "DEV"
,这将导致模式 "**/DEV/*.json"
仅包括名称为 DEV
的子文件夹中的 json 个文件。
我设置了一个 vertx-config-git verticle,它跟踪 github 中的配置回购设置。该存储库包含不同文件夹中特定于 DEV、QA、STATE 和 PROD 的配置(json 文件)。但是由于 Vertx Config 提取了所有配置文件,配置被从 repo 读取的最后一个 json 文件覆盖。有没有办法根据部署期间传递的 env 变量仅获取特定于环境的配置?
@Override
public void start(Promise<Void> startPromise) throws Exception {
ConfigStoreOptions env = new ConfigStoreOptions().setType("env");
ConfigStoreOptions git = new ConfigStoreOptions()
.setType("git")
.setConfig(new JsonObject()
.put("url", "https://github.com/kaushik-Das/vertx-config")
.put("path", "local")
.put("filesets",
new JsonArray().add(new JsonObject().put("pattern", "*.json"))));
ConfigRetrieverOptions options = new ConfigRetrieverOptions().addStore(env).addStore(git);
ConfigRetriever retriever = ConfigRetriever.create(vertx,
options.setScanPeriod(1000));
retriever.getConfig().setHandler(ar -> {
if (ar.failed()) {
System.out.println(ar.cause());
} else {
JsonObject config = ar.result();
System.out.println(config.encodePrettily());
}
});
retriever.listen(listener -> {
JsonObject newConfiguration = listener.getNewConfiguration();
System.out.println(newConfiguration);
});
}
从 vert.x v.3.8.5 开始,不支持在 Spring 世界中理解的配置文件。
但是,文件集的模式 follows the Ant style。所以这样的事情应该是可能的:
String stageName = getStageName(); // get stage identifier, e.g. from an environment variable
ConfigStoreOptions env = new ConfigStoreOptions().setType("env");
ConfigStoreOptions git = new ConfigStoreOptions()
.setType("git")
.setConfig(new JsonObject()
.put("url", "https://github.com/kaushik-Das/vertx-config")
.put("path", "local")
.put("filesets",
new JsonArray().add(new JsonObject().put("pattern", "**/" + stageName + "/*.json"))));
如果 stageName
是 "DEV"
,这将导致模式 "**/DEV/*.json"
仅包括名称为 DEV
的子文件夹中的 json 个文件。