Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Akka Http for Multiple Bindings

Using akka http to bind to a port and then route incoming connections is easy enough given the documentation.

One question not addressed is how to bind multiple ports for different routes. If I have multiple specifications:

val route1 : Flow[HttpRequest, HttpResponse,_] = ???
val interface1 : String = ???
val port1 : Int = ???

val route2 : Flow[HttpRequest, HttpResponse,_] = ???
val interface2 : String = ???
val port2 : Int = ???

Should these be bound with one HttpExt?

implicit val actorSystem : akka.actor.ActorSystem = ???

val httpExt = akka.http.scaladsl.Http()

httpExt.bindAndHandle(route1, interface1, port1)
httpExt.bindAndHandle(route2, interface2, port2)

Or, should a different HttpExt be used for each bind?

Http().bindAndHandle(route1, interface1, port1)
Http().bindAndHandle(route2, interface2, port2)

If either is valid, then are there any implications for using one method over the other?

Thank you in advance for your review and response.

like image 738
Ramón J Romero y Vigil Avatar asked Sep 08 '16 12:09

Ramón J Romero y Vigil


People also ask

What is Akka HTTP used for?

Akka HTTP is mostly used for making integration layers based on HTTP. A user can build the application based on their requirements, and Akka HTTP can be used to cater to the needs of HTTP integration.

Does Akka HTTP use Netty?

The Akka HTTP server backend is the default in Play. You can also use the Netty backend if you choose.

What is directive in Akka HTTP?

A “Directive” is a small building block used for creating arbitrarily complex route structures. Akka HTTP already pre-defines a large number of directives and you can easily construct your own: Basics. Structure.

What is DSL in Akka?

The Akka HTTP Core Server API provides a Flow - or Function -level interface that allows an application to respond to incoming HTTP requests by mapping requests to responses (excerpt from Low-level server side example): Scala.


1 Answers

A little source code investigation shows the following.

Here we see what happens at Http() call. Http object extends ExtensionId[HttpExt] trait, which is responsible for registering additional functionality with actor system. This is that super call. As we can see it uses this reference, which in our case in an object reference (important). Here is what actually happen when we call Http(). The first thing this code does is checking whether the extension is already registered. Remember, that ext in our case is an object, so any subsequent calls after first one won't do anything.

This means that either of your approaches would work the same, but imo using one httpExt is less confusing, given how the underlying system really works.

like image 109
Tim Avatar answered Oct 22 '22 04:10

Tim