Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distributed application with Akka Remoting
- Loading branch information
1 parent
ee30902
commit 5735a10
Showing
15 changed files
with
207 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
backend/src/main/scala/com/example/backend/BackendApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
package com.example.backend | |||
|
|||
import com.example.ExampleApp | |||
|
|||
object BackendApp extends App with ExampleApp { | |||
|
|||
system.actorOf(Backend.props, "backend") | |||
|
|||
log.info("Backend Service started") | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,17 +1,57 @@ | |||
import BuildSettings._ | import BuildSettings._ | ||
import Dependencies._ | import Dependencies._ | ||
import Resolvers._ | import Resolvers._ | ||
import com.typesafe.sbt.packager.archetypes.JavaAppPackaging.autoImport.bashScriptExtraDefines | |||
import com.typesafe.sbt.packager.docker._ | |||
|
|
||
lazy val commonSettings = buildSettings ++ Seq( | lazy val commonSettings = buildSettings ++ Seq( | ||
resolvers += akkaHttpJsonResolvers | resolvers += akkaHttpJsonResolvers | ||
) | ) | ||
|
|
||
lazy val dockerSettings = Seq( | |||
dockerExposedPorts := Seq(Docker.akkaTcpPort), | |||
bashScriptExtraDefines := Seq(Docker.bashExports) | |||
) | |||
|
|||
lazy val frontendDockerSettings = dockerSettings ++ Seq( | |||
dockerCommands := Seq( | |||
Cmd("FROM", "java:latest"), | |||
Cmd("USER", "root"), | |||
ExecCmd("RUN", "apt-get", "-qq", "update"), | |||
ExecCmd("RUN", "apt-get", "-yq", "install", "dnsutils"), | |||
ExecCmd("RUN", "apt-get", "clean"), | |||
ExecCmd("RUN", "rm", "-rf", "/var/lib/apt/lists/*") | |||
) ++ dockerCommands.value.filterNot { | |||
case Cmd("FROM", _) => true | |||
case _ => false | |||
}, | |||
bashScriptExtraDefines := Seq(Docker.frontendBashExports) | |||
) | |||
|
|||
lazy val root = (project in file(".")) | lazy val root = (project in file(".")) | ||
.settings(buildSettings: _*) | .settings(buildSettings: _*) | ||
.aggregate(frontend) | .aggregate(frontend, backend) | ||
|
|||
lazy val common = project | |||
.settings(commonSettings: _*) | |||
.settings(libraryDependencies ++= akka) | |||
|
|
||
lazy val frontend = project | lazy val frontend = project | ||
.settings(commonSettings: _*) | .settings(commonSettings: _*) | ||
.settings(libraryDependencies ++= akkaHttp) | .settings(libraryDependencies ++= akkaHttp) | ||
.enablePlugins(JavaAppPackaging) | .enablePlugins(JavaAppPackaging) | ||
.enablePlugins(DockerPlugin) | .enablePlugins(DockerPlugin) | ||
.settings(frontendDockerSettings) | |||
.dependsOn(`backend-api`, common) | |||
|
|||
lazy val backend = project | |||
.settings(commonSettings: _*) | |||
.settings(libraryDependencies ++= akka) | |||
.enablePlugins(JavaAppPackaging) | |||
.enablePlugins(DockerPlugin) | |||
.settings(dockerSettings) | |||
.dependsOn(`backend-api`, common) | |||
|
|||
lazy val `backend-api` = project | |||
.settings(commonSettings: _*) | |||
.settings(libraryDependencies ++= akka) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,23 @@ | |||
akka { | |||
|
|||
actor { | |||
provider = remote | |||
} | |||
|
|||
remote { | |||
enabled-transports = ["akka.remote.netty.tcp"] | |||
netty.tcp { | |||
hostname = ${app.remote.interface} | |||
port = ${app.remote.port} | |||
} | |||
} | |||
} | |||
|
|||
app { | |||
remote { | |||
interface = "127.0.0.1" | |||
interface = ${?REMOTE_INTERFACE} | |||
port = 0 | |||
port = ${?REMOTE_PORT} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
package com.example | |||
|
|||
import akka.actor.ActorSystem | |||
import akka.event.LoggingAdapter | |||
import com.typesafe.config.ConfigFactory | |||
|
|||
trait ExampleApp { | |||
|
|||
val actorSystemName = "example" | |||
val config = ConfigFactory.load() | |||
implicit val system = ActorSystem(actorSystemName, config) | |||
val log: LoggingAdapter = system.log | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,12 +1,10 @@ | |||
akka { | |||
actor { | |||
} | |||
} | |||
|
|||
app { | app { | ||
http { | http { | ||
interface = "0.0.0.0" | interface = "0.0.0.0" | ||
port = 9000 | port = 9000 | ||
port = ${?HTTP_PORT} | port = ${?HTTP_PORT} | ||
} | } | ||
backend { | |||
nodes = ${?BACKEND_NODES} | |||
} | |||
} | } |
41 changes: 41 additions & 0 deletions
41
frontend/src/main/scala/com/example/BackendPathsConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,41 @@ | |||
package com.example | |||
|
|||
import com.typesafe.config.Config | |||
|
|||
trait BackendPathsConfig { | |||
|
|||
def actorSystemName: String | |||
def config: Config | |||
|
|||
def backendPaths() = remoteNodesAddress.map(node => s"$node/user/backend") | |||
|
|||
private def remoteNodesAddress: List[String] = { | |||
val remoteInterface = config.getString("app.remote.interface") | |||
val remotePort = config.getInt("app.remote.port") | |||
if (config.hasPath("app.backend.nodes")) { | |||
config.getString("app.backend.nodes") | |||
.split(',').toList | |||
.map(toAkkaNodeRef(remotePort)) | |||
.flatten | |||
} else if (remotePort > 0) { | |||
List(toAkkaNodeRef(remoteInterface, remotePort)) | |||
} else { | |||
List.empty | |||
} | |||
} | |||
|
|||
private def toAkkaNodeRef(defaultPort: Int)(peer: String): Option[String] = { | |||
peer.trim.split(':') match { | |||
case Array(host, port) => | |||
Some(toAkkaNodeRef(host, port.toInt)) | |||
case Array(host) => | |||
if (defaultPort == 0) None | |||
else Some(toAkkaNodeRef(host, defaultPort)) | |||
} | |||
} | |||
|
|||
private def toAkkaNodeRef(host: String, port: Int) = { | |||
s"akka.tcp://$actorSystemName@$host:$port" | |||
} | |||
|
|||
} |
16 changes: 5 additions & 11 deletions
16
frontend/src/main/scala/com/example/frontend/FrontendApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
apiVersion: extensions/v1beta1 | |||
kind: Deployment | |||
metadata: | |||
name: backend-deployment | |||
spec: | |||
replicas: 1 | |||
template: | |||
metadata: | |||
labels: | |||
app: backend | |||
cluster: example | |||
spec: | |||
containers: | |||
- name: backend | |||
image: backend:0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
apiVersion: v1 | |||
kind: Service | |||
metadata: | |||
name: backend-service | |||
spec: | |||
type: ClusterIP | |||
clusterIP: None | |||
selector: | |||
app: backend | |||
ports: | |||
- port: 2551 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,32 @@ | |||
object Docker { | |||
|
|||
val akkaTcpPort = 2551 | |||
|
|||
val bashExports = """ | |||
export REMOTE_INTERFACE=$(hostname --ip-address) | |||
# Configure the default remote port unless passed | |||
if [ -z "$REMOTE_PORT" ]; then | |||
export REMOTE_PORT=""" + akkaTcpPort + """ | |||
fi | |||
""" | |||
|
|||
val frontendBashExports = """ | |||
# Configure backend nodes from service if passed | |||
# otherwise keep them as they are | |||
if [ ! -z "$BACKEND_DISCOVERY_SERVICE" ]; then | |||
BACKEND_NODES=$( | |||
host -t A $BACKEND_DISCOVERY_SERVICE | \ | |||
grep 'has address' | | |||
cut -d ' ' -f 4 | | |||
xargs | | |||
sed -e 's/ /,/g' | |||
) | |||
if [ ! -z "$BACKEND_NODES" ]; then | |||
export BACKEND_NODES | |||
fi | |||
fi | |||
""" + bashExports | |||
|
|||
} |