【问题标题】:Having trouble making sense of vert.x config loading无法理解 vert.x 配置加载
【发布时间】: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


    【解决方案1】:

    与往常一样,编写问题可以更好地了解问题。据我了解,解决方案是始终使用名为“config”的键和要传递的 JsonObject 的值创建一个映射。

    部署:

    private void launch( final JsonObject jsonObject )
    {
        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( MainVerticle.class.getName(), options );
    }
    
    @override
    public void start( final Future<Void> startFuture )
    {
        ConfigRetriever cfgRetriever = ConfigRetriever.create( vertx.getDelegate() );
    
        cfgRetriever.getConfig( ar -> {
            try {
                if( ar.succeeded() ) {
                    JsonObject config = ar.result();
    
                    JsonObject cfg = config.getJsonObject( "config" );
    
                    JsonObject chatDbOpts = cfg.getJsonObject( "database" );
                    LOGGER.debug( "Launching ChatDbServiceVerticle with the following config:\n{}",
                                 chatDbOpts.encodePrettily() );
    
                    JsonObject chatHttpOpts = cfg.getJsonObject( "chatListener" );
                    LOGGER.debug( "Launching HttpServerVerticle with the following config:\n{}",
                                 chatHttpOpts.encodePrettily() );
    ...
    

    产生输出:

    Launching ChatDbServiceVerticle with the following config:
    {
      "port" : 5432,
      "host" : "127.0.0.1",
      "name" : "linked",
      "user" : "postgres",
      "passwd" : "postgres",
      "connectionPoolSize" : 5
    }
    Launching HttpServerVerticle with the following config:
    {
      "port" : 8080,
      "host" : "localhost"
    }
    

    但是,如果 config() 忽略任何无法使用特定键检索的对象,那么这就引出了一个关于 DeploymentOptions(JsonObject) 构造函数的问题?它需要单步调试器才能找到它。文档https://vertx.io/blog/vert-x-application-configuration/ 中没有暗示此要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-24
      • 2013-08-10
      • 2016-10-26
      • 2023-02-23
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      相关资源
      最近更新 更多