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.
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.
The Akka HTTP server backend is the default in Play. You can also use the Netty backend if you choose.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With