问题
How to read data from property files? I understand vertx uses '.json' files for storing data but not sure how to create JsonObject from property/config file.
I would like to keep, server port, connection url, db name etc. in to a config file.
回答1:
you should run your verticle over io.vertx.core.Launcher
, then you could add conf option -conf config.panser.json
回答2:
Create the src/main/conf/my-application-conf.json with the following content:
{
"http.port" : 8082
}
And now, to use this configuration just launch your application with:
java -jar target/my-first-app-1.0-SNAPSHOT-fat.jar -conf src/main/conf/my-application-conf.json
To get the values use
config().getInteger("http.port", 8080)
Above code is now requesting the configuration and check whether the http.port property is set. If not, the port 8080 is used as fall-back.
Copied from : http://vertx.io/blog/vert-x-application-configuration/index.html
回答3:
You can take a look at https://github.com/vert-x3/vertx-service-factory to create a default configuration for a verticle.
回答4:
Use -conf
argument with the relative path (src/.../config.json
) to your configuration file.
E.g.
java -jar target/vertx-practice-1.0-SNAPSHOT-fat.jar -conf src/conf/vertx-practice-conf.json
run com.ashenlive.vertx.MyFirstVerticle -conf src/conf/vertx-practice-conf.json
来源:https://stackoverflow.com/questions/32438767/vertx-reading-from-property-config-file