Undertow Simple HelloWorld不支持Cloud Foundry

时间:2016-05-31 12:18:49

标签: java cloudfoundry httplistener undertow

我按照文档创建一个简单的下载HTTP服务器: http://undertow.io/undertow-docs/undertow-docs-1.3.0/index.html

   public static void main(final String[] args) {
      Undertow server = Undertow.builder()
         .addHttpListener(8080, "0.0.0.0")
         .setHandler(new HttpHandler() {
            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
               exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
               exchange.getResponseSender().send("Hello World");
            }
         }).build();
      server.start();
   }

它在localhost上正常工作。但是,当我将Cloud Foundry作为独立的Java应用程序部署时:

cf push im-gateway -p target\gateway.jar

应用无法启动并在日志中显示此错误:

Instance (index 0) failed to start accepting connections

经过调查,我修改了push命令:

cf push im-gateway -p target\gateway.jar --no-route

此次部署成功,我手动创建路由并尝试访问它但收到错误:

502 Bad Gateway: Registered endpoint failed to handle the request.

我应该听哪个端口? Cloud Foundry如何将请求重定向到我的应用程序?

我感谢任何回复。

1 个答案:

答案 0 :(得分:1)

根据文档,Cloud Foundry动态地为每个应用实例分配一个端口。

https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#PORT

尝试将.addHttpListener(8080, "0.0.0.0")替换为.addHttpListener(System.getenv("PORT"), "0.0.0.0")