Environment Detection

One of the key aspects of my Continuous Delivery talks is applications being aware of where they are.

Environment Detection stands for the ability to sense in which environment an application is currently deployed using the simplest means at its disposal.

Once this information is available you can act on it in a number of meaningful ways, such as loading the appropriate configuration or enabling/disabling features for the current environment.

This is powerful and simpler than it sounds. I'll show you a number of ways to implement the detection and then, we'll look at how to integrate it in an application. But always remember: use the simplest technique that works for you.


The most common detection strategies are:
  • IP Address/Range
  • HostName
  • File contents
  • Operating System
  • System Property

Let's have a look at how to implement them in Java.

IP Address/Range

// Assuming 1.2.3.4 is the IP Address of your DEV machine
// and 5.6.7.x is the IP Range of your PROD environment
String ipAddress = InetAddress.getLocalHost().getHostAddress();
if ("1.2.3.4".equals(ipAddress)) {
// DEV
} else if (ipAddress.startsWith("5.6.7.")) {
// PROD
}

HostName

// Assuming abc and def are the hostNames of your DEV and PROD servers
String hostName = InetAddress.getLocalHost().getHostName();
if ("abc".equals(hostName)) {
// DEV
} else if ("def".equals(hostName)) {
// PROD
}

File contents

import org.springframework.util.FileCopyUtils;

// Assuming /etc/my.env exists
// and contains the name of the environment such as DEV, TEST, PROD, ...
String environment = FileCopyUtils.copyToString(new FileReader("/etc/my.env"));

Operating System

// Assuming DEV is on Windows and PROD on Linux
String operatingSystem = System.getProperty("os.name");
if (operatingSystem.startsWith("Windows")) {
// DEV
} else {
// PROD
}

System Property

// Assuming my.env is set
// and contains the name of the environment such as DEV, TEST, PROD, ...
String environment = System.getProperty("my.env");

And that's all there is to it !


Here is a quick example of how to leverage this newly acquired piece of information:

Say we use a framework like Spring and want to easily load the appropriate configuration for the current environment.

We use an EnvironmentDetectionService to detect the environment and store the result in a System Property called my.env. The configuration for each environment is stored in a separate folder.

We can now load the appropriate configuration like this:
<bean id="environmentDetectionService" class="my.pkg.EnvironmentDetectionService"
init-method="detectEnvironment"/>

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="environmentDetectionService">
<property name="location" value="/config/${my.env}/my.properties"/>
</bean>


Environment Detection is a simple, yet very powerful capability. Start using it today and you'll never look back!

 


Axel

About Axel Fontaine

Axel Fontaine is the founder and CEO of Boxfuse the easiest way to deploy JVM and Node.js applications to AWS.

Axel is also the creator and project lead of Flyway, the open-source tool that makes database migration easy.

He is a Continuous Delivery and Immutable Infrastructure expert, a Java Champion, a JavaOne Rockstar and a regular speaker at many large international conferences including JavaOne, Devoxx, Jfokus, JavaZone, QCon, JAX, ...

You can follow him on Twitter at @axelfontaine

 

Architecting for Continuous Delivery and Zero Downtime

Two day intensive on-site training with Axel Fontaine

Upcoming dates

Iasi, Romania (May 10-11, 2017)
Oslo, Norway (Oct 16-17, 2017)

« Continuous Delivery Talk at the Rheinjug (in German)
Continuous Delivery Talk on Parleys »
Browse complete blog archive
Subscribe to the feed