Vert.x Config提供了一种配置Vert.x应用程序的方法。它:
提供多种配置语法(JSON,属性,Yaml(扩展名),Hocon(扩展名)...
提供多种配置存储,例如文件,目录,HTTP,git(扩展名),Redis(扩展名),系统属性和环境属性。
让您定义处理顺序和重载
支持运行时重新配置
Vert.x应用程序实例化并使用了Config Retriever。它配置了一组配置存储区 配置存储区定义了从中读取配置数据的位置以及格式(默认情况下为JSON)
该配置将作为JSON对象检索。
1、引入依赖
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-config</artifactId> <version>4.0.3</version> </dependency>
2、完成后,您首先需要实例化ConfigRetriever
:
ConfigRetriever retriever = ConfigRetriever.create(vertx);
默认情况下,配置检索器配置有以下存储(按此顺序):
Vert.x垂直 config()
系统属性
环境变量
一个conf/config.json
文件。可以使用vertx-config-path
系统属性或 VERTX_CONFIG_PATH
环境变量来覆盖此路径。
3、示例,ConfigVerticle程序
package vertx; import io.vertx.config.ConfigRetriever; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Router; import io.vertx.mysqlclient.MySQLConnectOptions; import io.vertx.mysqlclient.MySQLPool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.SqlConnection; import io.vertx.sqlclient.Tuple; import java.util.ArrayList; public class ConfigVerticle extends AbstractVerticle { //声明Router Router router; //配置连接参数 MySQLConnectOptions connectOptions; //配置连接池 Pool options PoolOptions poolOptions = new PoolOptions() .setMaxSize(5); //Create the client pool MySQLPool client; @Override public void start(Promise<Void> startPromise) throws Exception { //第一步 加入这行代码 ConfigRetriever retriever = ConfigRetriever.create(vertx); retriever.getConfig(ar -> { if (ar.failed()) { // Failed to retrieve the configuration } else { JsonObject config = ar.result(); connectOptions = new MySQLConnectOptions() .setPort(config.getInteger("port")) .setHost(config.getString("host")) .setDatabase(config.getString("database")) .setUser(config.getString("user")) .setPassword(config.getString("password")); client = MySQLPool.pool(vertx, connectOptions, poolOptions); //初始化Router router = Router.router(vertx); //配置Router解析url router.route("/").handler( req -> { req.response() .putHeader("content-type", "text/plain") .end("Hello from Vert.x!"); } ); //配置Router解析url router.route("/test/list").handler( req -> { var page = (Integer.valueOf(req.request().getParam("page")) - 1) * 10; var size = Integer.valueOf(req.request().getParam("size")); //Get a connection from the pool client.getConnection(ar1 -> { if(ar1.succeeded()){ System.out.println("Connected"); //Obtain our connection SqlConnection conn = ar1.result(); //All operations execute on the same connection conn .preparedQuery("select id, name, age, info from person limit ?, ?") .execute(Tuple.of(page, size), ar2 -> { //Release the connection to the pool conn.close(); if(ar2.succeeded()){ var list = new ArrayList<JsonObject>(); ar2.result().forEach(item -> { var json = new JsonObject(); json.put("id", item.getValue("id")); json.put("name", item.getValue("name")); json.put("age", item.getValue("age")); json.put("info", item.getValue("info")); list.add(json); }); req.response() .putHeader("content-type", "application/json") .end(list.toString()); }else{ req.response() .putHeader("content-type", "text/plain") .end(ar2.cause().toString()); } }); }else{ System.out.println("Could not connect:" + ar1.cause().getMessage()); } }); } ); //将Router与vertx HttpServer 绑定 vertx.createHttpServer().requestHandler(router).listen(8888, http -> { if (http.succeeded()) { startPromise.complete(); System.out.println("HTTP server started on port 8888"); } else { startPromise.fail(http.cause()); } }); } }); } }
在resources新建conf文件夹,并新建config.json文件:
{ "port": 3306, "host": "127.0.0.1", "database": "db", "user": "root", "password": "password" }
4、启动测试