vertx读取配置文件,获得端口号

1:在src/conf目录下创建conf.json

   

{
  "http.port" : 8082
}

2:创建Verticle,

 config().getInteger("http.port", 8080),将会读取配置文件,是否有http.port,没有就使用8080作为默认,

   

package verticleTest;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;

public class ConfigVerticle extends AbstractVerticle{

    public void start(Future<Void> fut) {
          vertx
              .createHttpServer()
              .requestHandler(r -> {
                r.response().end("<h1>Hello from my first " +
                    "Vert.x 3 application</h1>");
              })
              .listen(
                  // Retrieve the port from the configuration,
                  // default to 8080.
                  config().getInteger("http.port", 8080),
                  result -> {
                    if (result.succeeded()) {
                      fut.complete();
                    } else {
                      fut.fail(result.cause());
                    }
                  }
              );
        }
}

 

3:打包,发布。使用 -conf 将配置文件读入。

D:\Android\workspace1\Ali>java -jar target/Ali-0.0.1-SNAPSHOT-fat.jar -conf src/main/conf/conf.json

 

4:访问localhost:8082

   

 

posted @ 2018-02-05 11:10  1367356  阅读(1623)  评论(0编辑  收藏  举报