Startup multiple Servers on same port

2,139 views
Skip to first unread message

mathias

unread,
May 17, 2015, 2:11:43 PM5/17/15
to ve...@googlegroups.com
Recently, I started wondering how a verticle can simply start another HTTP Server on the same port as the one that is already active. It seams to be no problem in vert.x because any verticle that starts a server can be deployed with multiple instances. But wouldn't that mean that multiple servers are listening on the same TCP connection? As far as I know it wasn't ever possible to bind multiple processes to the same port.

It works, but why?

Mumuney Abdlquadri

unread,
May 17, 2015, 2:26:55 PM5/17/15
to ve...@googlegroups.com
"At this point you might be asking yourself 'Hold on, how can you have more than one server listening on the same host and port? Surely you will get port conflicts as soon as you try and deploy more than one instance?'

Vert.x does a little magic here.

When you deploy another server on the same host and port as an existing server it doesn't actually try and create a new server listening on the same host/port.

Instead it internally maintains just a single server, and, as incoming connections arrive it distributes them in a round-robin fashion to any of the connect handlers set by the verticles."

From.


On Sun, May 17, 2015 at 7:11 PM, mathias <roleba...@gmail.com> wrote:
Recently, I started wondering how a verticle can simply start another HTTP Server on the same port as the one that is already active. It seams to be no problem in vert.x because any verticle that starts a server can be deployed with multiple instances. But wouldn't that mean that multiple servers are listening on the same TCP connection? As far as I know it wasn't ever possible to bind multiple processes to the same port.

It works, but why?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jordan Halterman

unread,
May 17, 2015, 7:44:16 PM5/17/15
to ve...@googlegroups.com
If you're interested, here's the relevant code:
https://github.com/eclipse/vert.x/blob/master/src/main/java/io/vertx/core/net/impl/NetServerImpl.java#L156

Vert.x maintains a set of shared TCP servers. When a NetServer is created, it checks if one exists and adds a handler in the relevant context if it does already exist, otherwise it creates a new one. The end result is a server that handles requests concurrently.

mathias

unread,
May 18, 2015, 5:20:30 AM5/18/15
to ve...@googlegroups.com
Alright, thank you!
So deploying 5 HTTP Servers is like setting the number of threads for this Server to 5. That is a very nice feature and it is well documented :-)

Scott Rutherford

unread,
May 18, 2015, 7:52:23 PM5/18/15
to ve...@googlegroups.com
Hi Mumuney,

But doesn't that mean this should work?

public void start(Future<Void> startFuture) {
  startServer1();
  startServer2();
}

public void startServer1() {
  Router router = Router.router(vertx);

  router.route("/set1").handler(routingContext -> {
     routingContext.response().putHeader("content-type", "text/html").end("Hello World 1!");
  });

  vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}

public void startServer2() {
  Router router = Router.router(vertx);

  router.route("/set2").handler(routingContext -> {
    routingContext.response().putHeader("content-type", "text/html").end("Hello World 2!");
  });

  vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}

Instead I get /set1 fine but /set2 returns "Resource not  found"

Thanks
Scott

Mumuney Abdlquadri

unread,
May 19, 2015, 6:38:56 AM5/19/15
to ve...@googlegroups.com
Hi Scott,

You have to find a way to make the routes share the same Router instance. e.g. declaring it as instance variable. It should work.

--

Scott Rutherford

unread,
May 19, 2015, 5:50:44 PM5/19/15
to ve...@googlegroups.com
Awesome, yep that fixed things. Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/sFVLFnxFzPc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Dr Scott Rutherford
Thats Useful, Inc.

Mumuney Abdlquadri

unread,
May 20, 2015, 3:59:34 AM5/20/15
to ve...@googlegroups.com
You are welcome.
Reply all
Reply to author
Forward
0 new messages