【发布时间】:2019-05-18 16:46:33
【问题描述】:
我正在尝试使用 config.json 创建一个 Verticle,但没有通过阅读文档来体验我所期望的。我将尽我所能解释我所采取的步骤,但我已经尝试了我的垂直启动步骤的许多变化,所以我可能不是 100% 准确。这是使用 vert.x 3.7.0。
首先,当我将配置文件包含在预期位置 conf/config.json 中时,我已经成功地使用我的配置来启动我的 Verticle:
{
"database" : {
"port" : 5432,
"host" : "127.0.0.1",
"name" : "linked",
"user" : "postgres",
"passwd" : "postgres",
"connectionPoolSize" : 5
},
"chatListener" : {
"port" : 8080,
"host" : "localhost"
}
}
并使用启动器传递配置以启动verticle(伪代码):
public static void main(String[] args){
//preprocessing
Launcher.executeCommand("run", "MyVerticle")
...
和
public static void main(String[] args){
//preprocessing
Launcher.executeCommand("run", "MyVerticle -config conf/config.json")
...
两者都能正常工作。我的配置已加载,我可以从 Verticle 内的 config() 中提取数据:
JsonObject chatDbOpts = new JsonObject().put( "config", config.getJsonObject( "database" ) );
....
但是当我将不是默认位置的文件引用传递给启动器时,
$ java -jar vert.jar -config /path/to/config.json
它忽略它并使用内置配置,它是空的,忽略我的配置。然而 vertx 配置加载器的调试输出表明它正在使用默认位置:
conf/config.json
它实际上并没有这样做,因为我的配置文件在那里。因此,当在 CLI 上指定不同的配置时,配置加载器不会从默认位置加载。
所以我更改了代码以消化 main 中的配置并验证可以找到和读取 json 文件。然后将文件引用传递给启动器,但得到了相同的行为。于是我改为使用带有 deployVerticle 的 DeploymentOptions 对象。
加载配置并将其转换为 JsonObject 的预处理器步骤的输出:
Command line arguments: [-conf, d:/cygwin64/home/rcoe/conf/config.json]
Launching application with the following config:
{
"database" : {
"port" : 5432,
"host" : "127.0.0.1",
"name" : "linked",
"user" : "postgres",
"passwd" : "postgres",
"connectionPoolSize" : 5
},
"chatListener" : {
"port" : 8080,
"host" : "localhost"
}
}
这个 JsonObject 用于创建一个 DeploymentOptions 引用:
DeploymentOptions options = new DeploymentOptions(jsonCfg);
Vertx.vertx().deployVerticle( getClass().getName(), options );
没用。
然后我尝试创建一个空的 DeploymentOptions 引用并设置配置:
DeploymentOptions options = new DeploymentOptions();
Map<String,Object> m = new HashMap<>();
m.put("config", jsonObject);
JsonObject cfg = new JsonObject(m);
options.setConfig( cfg );
Vertx.vertx().deployVerticle( getClass().getName(), options );
这也未能通过我想要的配置。相反,它使用默认位置的配置。
这是垂直启动时的输出。它正在使用 conf/config.json 文件,
Config file path: conf\config.json, format:json
-Dio.netty.buffer.checkAccessible: true
-Dio.netty.buffer.checkBounds: true
Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@552c2b11
Config options:
{
"port" : 5432,
"host" : "not-a-real-host",
"name" : "linked",
"user" : "postgres",
"passwd" : "postgres",
"connectionPoolSize" : 5
}
与提供给 DeploymentOptions 参考的配置相比:
Launching application with the following config:
{
"database" : {
"port" : 5432,
"host" : "127.0.0.1",
"name" : "linked",
"user" : "postgres",
"passwd" : "postgres",
"connectionPoolSize" : 5
},
...
无论如何,希望这些步骤有意义并表明我已经尝试了多种方法来加载自定义配置。我已经看到我的配置被传递到负责调用 verticles 的 vert.x 代码中,但是当我的 verticle 的 start() 方法被调用时,我的配置已经消失了。
谢谢。
【问题讨论】:
标签: vert.x