Service Discovery: Java, a Database App, and DC/OS

DZone 's Guide to

Service Discovery: Java, a Database App, and DC/OS

Service discovery is a key part of containerized components. As containers are born and destroyed, your app needs to find the moving parts. See how to do it in DC/OS.

· Cloud Zone ·
Free Resource

Why Service Discovery?

An application typically consists of multiple components, such as an application server, a database, a web server, caching, and a messaging server. Typically, multiple replicas of each component would run based upon the needs of your application. Deploying this application using a container orchestration framework means that each replica would run as a container. So, an application is typically deployed as a multi-container application.

Each container is assigned a unique IP address for its lifetime. But containers are ephemeral and may terminate and be rescheduled on a different host by the orchestration framework. A container is typically assigned a different IP address in that case. This means an application deployed in an application server cannot rely upon the IP address of the database. This is where service discovery is required.

So, multiple replicas of a component are assigned a logical name. For example, web for all the application server containers and db for all the database containers. Now, an application can talk to the database containers using the logical service name. This allows the database containers to be rescheduled anywhere in the cluster, and also scale up and down dynamically.

Let’s see how this can be accomplished in DC/OS with a single instance of an application server and database server. This blog will use WildFly for the application server and Couchbase for the database.

Couchbase Cluster on Mesos with DC/OS provides more details on how to set up a Couchbase cluster on DC/OS.

This blog will use the following main steps:

  • Setup DC/OS Cluster
  • Marathon application definition
  • Deploy the application

The complete source code used in this blog is at github.com/arun-gupta/dcos-java-database.

Many thanks to @unterstein for creating the Maven plugin and helping me understand the inner workings of DC/OS.

Set Up the DC/OS Cluster

A DC/OS cluster can be easily created using the CloudFormation template. Detailed instructions, including system requirements and screenshots and setup, are available at Installing DC/OS on AWS.

Your CloudFormation output should look as shown:

Note down the value shown for the keys DnsAddress and PublicSlaveDnsAddress. The value of the first key can be used to access DC/OS GUI and looks like:

Configure DC/OS CLI as explained in here. In short, the following commands are used:

  • dcos config set core.dcos_url http://${DnsAddress} Replace ${DnsAddress} with the corresponding value from the CloudFormation output.
  • dcos auth login
  • dcos config show core.dcos_acs_token. If not already done, clone the repo from github.com/arun-gupta/dcos-java-database. Create a new file.dcos-token and copy the output from the command in this file.
  • dcos package install marathon-lb

Marathon Application Definition

The Marathon framework is used to schedule containers in DC/OS. A Marathon application can be defined by providing an application definition.

As mentioned earlier, this blog will show how a simple Java application can talk to a database. We’ll use a Java EE application deployed in WildFly and use Couchbase as the database. The application definition looks like:


What are the key points in this application definition?

  • The application has two containers: database and web. The web container has a dependency on the database container defined using the dependencies attribute.
  • The database container uses the arungupta/couchbase:travel Docker image. This image is created from github.com/arun-gupta/couchbase-javaee/tree/master/couchbase. It uses Couchbase base image and uses the Couchbase REST API to pre-configure the database. A sample bucket is also loaded in the database as well.
  • The web container uses arungupta/wildfly-couchbase-javaee:travel image. This image is created from github.com/arun-gupta/couchbase-javaee/blob/master/Dockerfile. This is a Java EE 7 application bundled in WildFly. The app uses COUCHBASE_URI as an environment variable to connect to the Couchbase database. The value of this environment variable is configured to use DNS service discovery and is derived as explained in Virtual Networks.

Make sure to change the value of HAPROXY_0_VHOST to match the value of ${PublicSlaveDnsAddress} from the CloudFormation output. The label HAPROXY_0_VHOST instructs Marathon-LB to expose the Docker container, the WildFly application server in our case, on the external load balancer with a virtual host. The 0 in the label key corresponds to the servicePort index, beginning from 0. If you had multiple servicePort definitions, you would iterate them as 0, 1, 2, and so on. Deploying an internally and externally load-balanced app with marathon-lb provide more details about how to configure marathon-lb.

Service Discovery and Load Balancing provide more details about service discovery and load balancing in DC/OS.

Deploy the Application Using Maven

The application can be deployed using dcos-maven-plugin.

The plugin looks like:


The main points in this fragment are:

  • The plugin version is 0.2. That indicates the plugin is still in the early stages of development.
  • dcosUrl is the value of the ${DnsAddress} key from the CloudFormation output. This address is used for deployment of the application.
  • <deployable> enables different types of deployments — app, group or pods. This element is a hint for the plugin and should likely go away in a future version as Marathon API consolidates. Follow #11 for more details.

Other details and configuration about the plugin are at dcos-maven-plugin.

Deploy the application:

mvn install

The following output is shown:

[INFO] --- dcos-maven-plugin:0.2:deploy (dcos:deploy) @ dcos-java-database ---
[INFO] About to execute DC/OS deploy
[INFO] app definition: /Users/arungupta/workspaces/dcos-java-database/app-definition.json
[INFO] dcos token: /Users/arungupta/workspaces/dcos-java-database/.dcos-token
[INFO] dcos url: http://DCOS-ElasticLoadBa-1TH4TXIU5P783-1163025470.us-west-1.elb.amazonaws.com/
[INFO] ignore ssl certificate: true
[INFO] deployable: group
[INFO] Response from DC/OS [200] {"version":"2017-03-07T13:27:49.970Z","deploymentId":"dbc80f96-28cb-4040-8d0d-78452e461ec1"}

Here are some of the updated outputs from the DC/OS console.

First, the updated Services tab:

Two applications are in the service:

The database application has one task:

The status of the database task:

Logs from the database task:

It shows the output from Couchbase REST API for configuring the server.

Status of the web task:

Logs from the web task:

It shows that the Java EE application has been deployed successfully.

Access the application:

curl http://DCOS-PublicSlaveLo-DD2EGGFVCJA0-1604955948.us-west-1.elb.amazonaws.com/airlines/resources/airline

The address is the value of the key ${PublicSlaveDnsAddress} from the CloudFormation output. A formatted output, for example with jq, looks like:

    "travel-sample": {
      "country": "United States",
      "iata": "Q5",
      "callsign": "MILE-AIR",
      "name": "40-Mile Air",
      "icao": "MLA",
      "id": 10,
      "type": "airline"
    "travel-sample": {
      "country": "United States",

. . .

     "icao": "RLA",
      "id": 1203,
      "type": "airline"

That’s it!

As mentioned earlier, the complete source code used in this blog is at github.com/arun-gupta/dcos-java-database.

This blog showed how a simple Java application can talk to a database using service discovery in DC/OS.

For further information check out:

cloud, containerization, dcos, service discovery, tutorial

Published at DZone with permission of Arun Gupta , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}