2011-05-26
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!
About Axel Fontaine
I'm an entrepreneur, public speaker and software development expert based in Munich.
I'm the creator of Sprinters. Sprinters lets you run your GitHub Actions jobs 10x cheaper on your own AWS account with secure, ephemeral, high-performance, low-cost runners within the privacy of your own VPC.
I also created CloudCaptain, previously known as Boxfuse. CloudCaptain is a cloud deployment platform enabling small and medium size companies to focus on development, while it takes care of infrastructure and operations.
Back in 2010, I bootstrapped Flyway, and grew it into the world's most popular database migration tool. Starting late 2017, I expanded the project beyond its open-source roots into a highly profitable business, acquiring many of the world's largest companies and public institutions as customers. After two years of exponential growth, I sold the company to Redgate in 2019.
In the past I also spoke regularly at many large international conferences including JavaOne, Devoxx, Jfokus, JavaZone, JAX and more about a wide range of topics including modular monoliths, immutable infrastructure and continuous delivery. As part of this I received the JavaOne RockStar speaker award. As a recognition for my contributions to overall Java industry, Oracle awarded me the Java Champion title.
You can find me on 𝕏 as @axelfontaine and email me at axel@axelfontaine.com