我按照文档创建一个简单的Underwow 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();
   }


它可以在本地主机上正常工作。但是,当我作为独立的Java应用程序部署在Cloud Foundry上时:

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如何将请求重定向到我的应用程序?

感谢您的答复。

最佳答案

根据文档,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")

09-11 19:40