Heroku 嵌入式 Jetty 端口绑定超时

Heroku embedded Jetty port binding timeout

我正在尝试使用嵌入式 Jetty 创建一个小型 Web 服务器。我使用 maven 的 shade 插件将它构建到 "uberjar" 中。这是我的 Startup class 的主要功能:

BasicConfigurator.configure();

logger.info("Starting server");

URI baseUri = UriBuilder.fromUri("http://localhost").port(8080).build();
ResourceConfig config = new ResourceConfig(WindowAction.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);

server.start();
server.join();
logger.info("Server started");

到目前为止它在本地工作。我能够在本地独立构建和 运行 服务器:

java -jar target/server-0.jar

服务器响应 localhost:8080 的请求。惊人的。 当我 运行:

heroku local web

一样,服务器响应正常。 但是,当我将它推送到 Heroku 远程时,出现端口绑定超时错误:

2017-08-08 22:12:52.396:INFO:oejs.Server:main: jetty-9.2.z-SNAPSHOT
2017-08-08T22:12:52.464157+00:00 app[web.1]: 2017-08-08 22:12:52.460:INFO:oejs.ServerConnector:main: Started ServerConnector@5890e879{HTTP/1.1}{0.0.0.0:8080}
2017-08-08T22:12:52.464414+00:00 app[web.1]: 2017-08-08 22:12:52.464:INFO:oejs.Server:main: Started @2891ms
2017-08-08T22:14:17.656011+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch
2017-08-08T22:14:17.656011+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-08-08T22:14:17.792987+00:00 heroku[web.1]: Process exited with status 137
2017-08-08T22:14:17.810279+00:00 heroku[web.1]: State changed from starting to crashed

我试过将端口放在 Procfile 中 java 的命令行参数中,如下所示:

$JAVA_OPTS -Dserver.port=$PORT -jar target/myserver.jar

我也在运行时间尝试获取服务器端口:

System.getenv("port")

计算结果为空字符串。也许 Heroku 不喜欢被告知嵌入式服务器将使用 "localhost" 但 JettyHttpContainerFactory.createServer 需要一个 URI。谁能告诉我让他们玩得开心的正确方法?

环境变量 PORT 全部大写:

System.getenv("PORT")

这是一个完整的例子:https://github.com/kissaten/java-minimal/blob/master/src/main/java/Main.java#L63