Skip to content

Deployment context

Howard Yuan edited this page Jul 10, 2014 · 4 revisions

DeploymentContext defines a set of deployment context based properties. The values of these properties are optional and can be any string. For example, environment is one property of deployment context and can be “prod”, “test” or any other string that makes sense to an organization.

Using the DeploymentContext and load cascaded configuration files

Deployment context properties can be used in many ways. Archaius uses deployment context to load cascaded configuration files. Here is the use case:

  • Application defines a set of default properties
  • At runtime, those properties should be overridden by deployment context specific values.

For example, an application has a configuration file database.properties, which contains default set of database properties. It has two other properties file

  • database-prod.properties, which contains overridden values for “prod” environment
  • database-test.properties, which contains overridden values for “test” environment

To load any one of the above file, the ConfigurationManager will consult with DeploymentContext object set with it to determine what environment it is in (through DeploymentContext.getDeploymentEnvironment() method), and load additional database-${environment} file to override the default values.

DeploymentContext is pluggable in Archaius. ConfigurationManager will instantiate DeploymentContext implementation class given as value for system property “archaius.default.deploymentContext.class”. If that is not defined, Archaius will by default install a DeploymentContext instance that determine its return values based on a set of predefined properties:

  • archaius.deployment.environment
  • archaius.deployment.region
  • archaius.deployment.datacenter
  • archaius.deployment.applicationId
  • archaius.deployment.serverId
  • archaius.deployment.stack

The value of these properties can be set as system properties.

Another feature of Archaius is that you can define the way cascaded property files are loaded. For example, you want Archaius to load an EC2 region specific properties file database-us-east-1.properties to override the default database.properties. To do that, you can define this property at the end of the database.properties file:


@next=database-${@region}.properties

“@next” is a special property to define next file to load. “@region” is a automatically set property and will expand to value returned from DeploymentContext.getDeploymentRegion() at runtime. You can prepend “@” to any DeploymentContext.ContextKey enum to get its value as a property. For example,

  // this will return the same value as DepolymentContext.getValue(ContextKey.environment)
  // or DeploymentContext.getDeploymentEnvironment()
  ConfigurationManager.getConfigInstance().getString("@environment"); 
  • Out of box, cascaded configuration files are only supported through static files. Applications need to call the loading explicitly instead of relying on Archaius’s default behavior. By default, Archaius uses poller to poll the default configuration file “config.properties” and treat “config.properties” as a dynamic source. For a dynamic source, Deployment context and @next property are not recognized. If you want to cascade config.properties, you need to call ConfigurationManager.loadCascadedPropertiesFromResources(“config”) when you initialize the application.