Deploy Vert.x on Tomcat

2020-07-25 10:12发布

I need to deploy to a PaaS (HANA Cloud Platform) that only supports a Tomcat container and also want to run Vert.x as an async framework.

What I did so far is to bootstrap Vert.x through a servlet:

public class VertxServlet extends HttpServlet {
...
    @Override
    public void init(ServletConfig cfg) {

        Vertx vertx = Vertx.vertx();
        vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
            public void handle(HttpServerRequest req) {
              System.out.println("Got request: " + req.uri());
              System.out.println("Headers are: ");
              for (Map.Entry<String, String> entry : req.headers()) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
              }
              req.response().headers().set("Content-Type", "text/html; charset=UTF-8");
              req.response().end("<html><body><h1>Hello from vert.x!</h1></body></html>");
            }
          }).listen(8888);
    }
...
}

And in my web.xml I put this:

<servlet>
    <servlet-name>VertxServlet</servlet-name>
    <display-name>VertxServlet</display-name>
    <description></description>
    <servlet-class>com.mypackage.VertxServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

It works quite well. The problem is that the PaaS is not able to configure the port mapping. The whole Tomcat including the reverse proxy is a prepared solution.

Questions:

  • Is it a viable approach to run Vert.x as a .war file or does it have any limitations and difficulties that may occur later?

  • Is there a way of binding my little Vert.x server to the default Tomcat port without running into a port conflict?

Thank you

2条回答
叛逆
2楼-- · 2020-07-25 10:52

As I understand it, you will run into a port binding problem to run on the same port as tomcat.

Instead of running vert.x inside tomcat, perhaps you should consider running vert.x in front of tomcat and proxy requests from vert.x to tomcat if the request is destined for tomcat. This will allow you to take advantage of vert.x async for requests that are destined for vert.x. If you do it the other way, I think you will loose some of the vert.x goodness. You may want to consider running on 2 jvm if you have the extra memory.

查看更多
成全新的幸福
3楼-- · 2020-07-25 11:08

I have found a solution. There's a HTTP Proxy Servlet where you can easily forward HTTP requests to the Vert.x socket: https://github.com/mitre/HTTP-Proxy-Servlet It works well for HTTP.

But this solution does not support web sockets, unfortunately.

查看更多
登录 后发表回答