Skip to content

Commit

Permalink
Add akka service discovery example app
Browse files Browse the repository at this point in the history
[#157854651]

Signed-off-by: Christian Ang <[email protected]>
  • Loading branch information
Aidan Obley authored and christianang committed Jun 7, 2018
1 parent 9428e8b commit dd79618
Show file tree
Hide file tree
Showing 42 changed files with 740 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
27 changes: 27 additions & 0 deletions akka-sample-cluster-on-cloudfoundry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
*.class
*.log

# sbt specific
.cache
.history
.lib/
.idea/
dist/*
target/
lib_managed/
src_managed/
project/boot/
project/plugins/project/

# Scala-IDE specific
.scala_dependencies
.worksheet
*.sc
*.tgz
*.zip
bosh-lite
diego-release
garden-runc-release
cf-release
netman-release
bin
104 changes: 104 additions & 0 deletions akka-sample-cluster-on-cloudfoundry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
This repo contains sample Akka Cluster app integrated with Service Discovery.

# How to run Akka Cluster on Cloud Foundry (CF)

Thanks to [this git repo](https://github.com/gtantachuco-pivotal/akka-sample-cluster-on-cloudfoundry) for the original version of this example, which uses Amalgam8.

This is a short guide to walk you through how to deploy and run Akka Cluster-based application on [Cloud Foundry (CF)](https://cloudfoundry.org)

**Note:** Akka with no Remoting / Cluster can run on CF with no additional requirement. This article deals with cases when Remoting / Cluster features are used.

**Background:** [Akka Cluster](http://doc.akka.io/docs/akka/snapshot/scala/cluster-usage.html) is based on TCP communication or optionally can use UDP instead in Akka version >= 2.4.11.
CF's standard container-to-container (C2C) mechanism allows apps talking to other apps via TCP or UDP; however, the ingress traffic to the entry point application supports only HTTP(S) and TCP.

In this guide we will use this CF C2C feature to show how to run Akka Cluster that uses TCP.

## Prerequisites

The following instructions for this example assume the following:
- [This git repo](https://github.com/cloudfoundry/cf-networking-examples) cloned to ~/workspace
- [The CF Networking Release git repo](https://github.com/cloudfoundry/cf-networking-release) cloned to ~/workspace
- A Cloud Foundry with Service Discovery enabled; service discovery is part of the cf-networking-release

## Get ready to deploy apps to CF

- Login to CF:
```
cf login...
```
- Target the org and space where you want to deploy your apps:
```
cf target -o...
```

## Build the Akka application

You can deploy Akka application by using your foundation's [java-buildpack](https://github.com/cloudfoundry/java-buildpack.git). Our sample application is inspired by [akka-sample-cluster](https://github.com/akka/akka/tree/master/akka-samples/akka-sample-cluster-scala)).
It has backend nodes that calculate factorial upon receiving messages from frontend nodes. Frontend nodes also expose HTTP interface `GET <frontend-hostname>/info` that shows number of jobs completed.

- Go to your local repo's folder
```
cd ~/workspace/cf-networking-examples/akka-sample-cluster-on-cloudfoundry/akka-sample-cluster/
```
- Compile and package both Akka backend and frontend components:
```
sbt backend:assembly # backend
sbt frontend:assembly # frontend
```

## Deploying Akka backend application

- Deploy, but do not start yet, sample Akka backend: with `--no-route` and `--health-check-type none` options since backend doesn't expose any HTTP ports:
```
cf push --no-start --no-route --health-check-type none akka-backend -p target/scala-2.11/akka-sample-backend.jar -b java_buildpack_offline
```
- Map an internal route to the backend application. Assuming your internal tld is `apps.internal`:
```
cf map-route akka-backend apps.internal --hostname akka-backend
```
- Add this network policy, to allow the backend nodes to communicate:
```
cf add-network-policy akka-backend --destination-app akka-backend --port 2551 --protocol tcp
```
- Start the backend app:
```
cf start akka-backend
```
- Check the log to see that first node joined itself:
```
cf logs akka-backend
```
- **IMPORTANT:** To prevent cluster split, verify that the first node is running before scaling it.
- Scale backend to 2 instances:
```
cf scale akka-backend -i 2
```

## Deploying Akka frontend application

- Deploy, but don't start yet, the sample Akka frontend:
```
cf push akka-frontend --no-start -p target/scala-2.11/akka-sample-frontend.jar -b java_buildpack_offline
```
- Add this network policy to allow frontend app to communicate with backend app cannot via backend's TCP:2551 port:
```
cf add-network-policy akka-frontend --destination-app akka-backend --port 2551 --protocol tcp
```
- Start the fronted app:
```
cf start akka-frontend
```
- In separate windows or terminal sessions, check logs from both frontend and backend to ensure all client/server and server-to-server communications are working fine:
```
cf logs akka-backend
cf logs akka-frontend
```
- Verify that it works:
```
curl akka-frontend.<YOUR_CF_DOMAIN>/info
```
- If all is working, it should show the number of completed jobs

## Summary

This guide shows the implementation of a successful PoC; hence, it requires more than that to have a production Akka Cluster on CF.
121 changes: 121 additions & 0 deletions akka-sample-cluster-on-cloudfoundry/akka-sample-cluster/COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
Creative Commons Legal Code

CC0 1.0 Universal

CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.

Statement of Purpose

The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").

Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.

For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.

1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:

i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.

2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.

3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.

4. Limitations and Disclaimers.

a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.
10 changes: 10 additions & 0 deletions akka-sample-cluster-on-cloudfoundry/akka-sample-cluster/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Activator Template by Typesafe

Licensed under Public Domain (CC0)

To the extent possible under law, the person who associated CC0 with
this Activator Tempate has waived all copyright and related or neighboring
rights to this Activator Template.

You should have received a copy of the CC0 legalcode along with this
work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
42 changes: 42 additions & 0 deletions akka-sample-cluster-on-cloudfoundry/akka-sample-cluster/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
val akkaV = "2.4.3"

resolvers ++= Seq("Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/")

val Frontend = config("frontend") extend(Compile)
val Backend = config("backend") extend(Compile)
def customAssemblySettings =
inConfig(Frontend)(baseAssemblySettings ++
inTask(assembly)(mainClass := Some("sample.cluster.factorial.FactorialFrontend")) ++
inTask(assembly)(assemblyJarName := "akka-sample-frontend.jar")) ++
inConfig(Backend)(baseAssemblySettings ++
inTask(assembly)(mainClass := Some("sample.cluster.factorial.FactorialBackend")) ++
inTask(assembly)(assemblyJarName := "akka-sample-backend.jar"))

val project = Project(
id = "akka-sample-cluster-scala",
base = file("."),
settings = customAssemblySettings ++ Defaults.coreDefaultSettings ++ Seq(
name := """akka-sample-cluster""",
scalaVersion := "2.11.8",
// scalaVersion := provided by Typesafe Reactive Platform
scalacOptions in Compile ++= Seq("-encoding", "UTF-8", "-target:jvm-1.6", "-deprecation", "-feature", "-unchecked", "-Xlog-reflective-calls", "-Xlint"),
javacOptions in Compile ++= Seq("-source", "1.6", "-target", "1.6", "-Xlint:unchecked", "-Xlint:deprecation"),
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-cluster" % akkaV,
"com.typesafe.akka" %% "akka-multi-node-testkit" % akkaV,
"com.typesafe.akka" %% "akka-http-experimental" % akkaV,
"com.typesafe.akka" %% "akka-http-spray-json-experimental" % akkaV,
"com.typesafe.akka" %% "akka-contrib" % akkaV,
"org.scalaj" %% "scalaj-http" % "2.3.0",
"com.typesafe.play" %% "play-json" % "2.3.4",
"org.scalatest" %% "scalatest" % "2.2.1" % "test"//,
/*"org.fusesource" % "sigar" % "1.6.4"*/),
javaOptions in run ++= Seq(
//"-Djava.library.path=./sigar",
"-Xms128m", "-Xmx1024m"),
Keys.fork in run := true,
mainClass in (Compile, run) := Some("sample.cluster.simple.SimpleClusterApp"),
parallelExecution in Test := false,
mainClass in assembly := Some("sample.cluster.factorial.FactorialApp")
)
)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Activator-generated Properties
#Fri Aug 26 13:05:39 EDT 2016
template.uuid=472c6099-8b2b-4aee-a669-dbaab0c53138
sbt.version=0.13.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

resolvers += Classpaths.typesafeReleases

addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.8")

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
clustering {

ip = "127.0.0.1"
ip = ${?THIS_IP}

port = 0
port = ${?AKKA_SAMPLE_THIS_PORT}

seed-host = "akka-backend.apps.internal"
seed-host = ${?AKKA_SAMPLE_SEED_IP_1}

seed-port = 2551
seed-port = ${?AKKA_SAMPLE_SEED_PORT_1}

name = AkkaSampleCluster
}

akka {
actor {
provider = "akka.cluster.ClusterActorRefProvider"
}
remote {
log-remote-lifecycle-events = off
netty.tcp {
hostname = ${clustering.ip}
port = ${clustering.port}
}
}

cluster {
seed-nodes = [
"akka.tcp://"${clustering.name}"@"${clustering.seed-host}":"${clustering.seed-port}]
auto-down-unreachable-after = 10s
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
include "application"

# //#min-nr-of-members
akka.cluster.min-nr-of-members = 2
# //#min-nr-of-members

# //#role-min-nr-of-members
akka.cluster.role {
frontend.min-nr-of-members = 1
backend.min-nr-of-members = 1
}
# //#role-min-nr-of-members

# //#adaptive-router
akka.actor.deployment {
/factorialFrontend/factorialBackendRouter = {
router = round-robin-group # adaptive-group
# metrics-selector = heap
# metrics-selector = load
# metrics-selector = cpu
# metrics-selector = mix
nr-of-instances = 4
routees.paths = ["/user/factorialBackend"]
cluster {
enabled = on
use-role = backend
allow-local-routees = off
}
}
}
# //#adaptive-router
Loading

0 comments on commit dd79618

Please sign in to comment.