[英]Undertow Simple HelloWorld not working on Cloud Foundry
Im creating a simple undertow HTTP server following the documentation: http://undertow.io/undertow-docs/undertow-docs-1.3.0/index.html 我根据文档创建了一个简单的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();
}
It works normally on localhost. 它可以在本地主机上正常工作。 But when I deploy on Cloud Foundry as a standalone java app:
但是,当我作为独立的Java应用程序部署在Cloud Foundry上时:
cf push im-gateway -p target\gateway.jar
The app fails to start and show this error in the log: 该应用无法启动,并在日志中显示此错误:
Instance (index 0) failed to start accepting connections
After investigation, I modify the push command: 经过调查,我修改了push命令:
cf push im-gateway -p target\gateway.jar --no-route
The deployment this time is successful, i create the route manually and try to access it but got the error: 这次部署成功,我手动创建了路由并尝试访问它,但收到错误消息:
502 Bad Gateway: Registered endpoint failed to handle the request.
Which port should I listen on ? 我应该在哪个端口上监听? How Cloud Foundry redirect the request to my app ?
Cloud Foundry如何将请求重定向到我的应用程序?
I appreciate any reply. 感谢您的答复。
According to the docs Cloud Foundry dynamically assigns a port to each app instance. 根据文档,Cloud Foundry为每个应用程序实例动态分配一个端口。
https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#PORT https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#PORT
Try replacing .addHttpListener(8080, "0.0.0.0")
with .addHttpListener(System.getenv("PORT"), "0.0.0.0")
尝试将
.addHttpListener(8080, "0.0.0.0")
替换为.addHttpListener(System.getenv("PORT"), "0.0.0.0")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.