我试图在本地和Heroku上启动一个vert.x示例(使用Procfile):
java -Dhttp.port=$PORT -jar myapp.jar
我遇到属性 (http.port) 未设置,因此我的程序无法访问。
使用 System.getenv() 读取 PORT 环境变量是有效的,但不是"最佳实践"。
为什么?我能做什么? 欧维
正如@dpr指出的那样,ConfigRetriever是要走的路。
这是我最终做的事情:
// Get the system property store (type = sys)
val sysPropsStore = ConfigStoreOptions().setType("sys")
// Add system property store to the config retriever options
val options = ConfigRetrieverOptions().addStore(sysPropsStore)
// And create a ConfigRetriever
val retriever = ConfigRetriever.create(vertx, options)
// Set the default port
var httpPort: Int = 8080
retriever.getConfig { ar ->
if (ar.failed()) {
// Failed to retrieve the configuration
} else {
val config = ar.result()
if (config.containsKey("http.port"))
httpPort = config.getInteger("http.port")
}
}
最好的方法是定义一个 json 文件并在其中添加所有与配置相关的 vertx。
在启动应用程序时将 config.json 作为参数传递。
这是一个例子:
Config.json 文件
{
"port": 8090
// you can add other stuff over here
}
在运行应用程序时传递 config.json 作为参数
Java -jar myapp.jar --conf=/path to your config.json
所以你可以在你的主垂直点中读取这个json文件
public class MainVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) throws Exception {
JsonObject data = this.config();
// read port and other configuration related stuff from JsonObject
}
}
我希望这对你有所帮助:)