The Vert.x Configuration retriever provides a way to configure your Vert.x application.

It:

  • offer multiple configuration syntaxes (json, properties, yaml (extension), hocon (extension)…​

  • offer multiple configuration stores (files, directories, http, git (extension), redis (extension), system properties, environment properties)…​

  • let you define the processing order and overloading

  • support runtime reconfiguration

Concepts

The configuration retriever is structured around:

  • a Configuration Retriever instantiated and used by the Vert.x application. It configures a set of configuration store

  • configuration store defines a location from where the configuration data is read and and a syntax (json by default)

The configuration is retrieved as a JSON Object.

Using the Vert.x Configuration Retriever

To use the Vert.x Configuration Retriever, add the following dependency to the dependencies section of your build descriptor:

  • Maven (in your pom.xml):

<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-config</artifactId>
  <version>3.4.0.Beta1</version>
</dependency>
  • Gradle (in your build.gradle file):

compile 'io.vertx:vertx-config:3.4.0.Beta1'

Once done, you first need to instantiate the ConfigRetriever:

import io.vertx.config.ConfigRetriever

var retriever = ConfigRetriever.create(vertx)

By default the Vert.x Configuration Retriever is configured with the following stores (in this order):

  • The Vert.x verticle config()

  • The system properties

  • The environment variables

You can configure your own stores:

import io.vertx.config.ConfigRetriever
import io.vertx.config.ConfigRetrieverOptions
import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*
import io.vertx.kotlin.core.json.*

var httpStore = ConfigStoreOptions(
  type = "http",
  config = json {
    obj(
      "host" to "localhost",
      "port" to 8080,
      "path" to "/conf"
    )
  })

var fileStore = ConfigStoreOptions(
  type = "file",
  config = json {
    obj("path" to "my-config.json")
  })

var sysPropsStore = ConfigStoreOptions(
  type = "sys")


var options = ConfigRetrieverOptions(
  stores = listOf(httpStore, fileStore, sysPropsStore))

var retriever = ConfigRetriever.create(vertx, options)

More details about the overloading rules and available stores are available below.

Once you have the instance of the Vert.x Configuration Retriever, retrieve the configuration as follows:

retriever.getConfig({ ar ->
  if (ar.failed()) {
    // Failed to retrieve the configuration
  } else {
    var config = ar.result()
  }
})

Overloading rules

The declaration order of the configuration store is important as it defines the overloading. For conflicting key, configuration stores arriving last overloads the value provided by the previous configuration stores. Let’s take an example. We have 2 configuration stores:

  • A provides {a:value, b:1}

  • B provides {a:value2, c:2}

Declared in this order (A, B), the resulting configuration would be: {a:value2, b:1, c:2}.

If you declare them in the reverse order (B, A), you would get: * {a:value, b:1, c:2}.

Available configuration stores

The Vert.x Configuration Retriever provides a set of configuration store and format. Some more are available as extension and you can implement your own.

Structure of the configuration

Each declared data store must specify the type. It can also define the format. If not set JSON is used.

Some configurations tore requires additional configuration (such a path…​). This configuration is passed as a Json Object using setConfig

File

This configuration store just read the configuration from a file. It supports all supported formats.

import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*
import io.vertx.kotlin.core.json.*

var file = ConfigStoreOptions(
  type = "file",
  format = "properties",
  config = json {
    obj("path" to "path-to-file.properties")
  })

The path configuration is required.

JSON

The JSON configuration store just serves the given JSON config as it is.

import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*
import io.vertx.kotlin.core.json.*

var json = ConfigStoreOptions(
  type = "json",
  config = json {
    obj("key" to "value")
  })

The only supported format for this configuration store is JSON.

Environment Variables

This configuration store maps environment variables to a Json Object contributed to the global configuration.

import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*

var json = ConfigStoreOptions(
  type = "env")

This configuration store does not support the format configuration.

System Properties

This configuration store maps system properties to a Json Object contributed to the global configuration.

import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*
import io.vertx.kotlin.core.json.*

var json = ConfigStoreOptions(
  type = "sys",
  config = json {
    obj("cache" to "false")
  })

This configuration store does not support the format configuration.

You can configure the cache attribute (true by default) let you decide whether or not it caches the system properties on the first access and does not reload them.

HTTP

This configuration stores retrieves the configuration from a HTTP location. It can use any supported format.

import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*
import io.vertx.kotlin.core.json.*

var http = ConfigStoreOptions(
  type = "http",
  config = json {
    obj(
      "host" to "localhost",
      "port" to 8080,
      "path" to "/A"
    )
  })

It creates a Vert.x HTTP Client with the store configuration (see next snippet). To ease the configuration, you can also configure the host, port and path with the host, port and path properties.

import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*
import io.vertx.kotlin.core.json.*

var http = ConfigStoreOptions(
  type = "http",
  config = json {
    obj(
      "defaultHost" to "localhost",
      "defaultPort" to 8080,
      "ssl" to true,
      "path" to "/A"
    )
  })

Event Bus

This event bus configuration stores receives the configuration from the event bus. This stores let you distribute your configuration among your local and distributed components.

import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*
import io.vertx.kotlin.core.json.*

var eb = ConfigStoreOptions(
  type = "event-bus",
  config = json {
    obj("address" to "address-getting-the-conf")
  })

This configuration store supports any type of format.

Directory

This configuration store is similar to the file configuration store, but instead of reading a single file, read several files from a directory.

This configuration store configuration requires:

  • a path - the root directory in which files are located

  • at least one fileset - an object to select the files

Each fileset contains: * a pattern : a Ant style pattern to select files. The pattern is applied on the relative path of the files location in the directory. * an optional format indicating the format of the files (each fileset can use a different format, BUT files in a fileset must share the same format).

import io.vertx.config.ConfigStoreOptions
import io.vertx.kotlin.config.*
import io.vertx.kotlin.core.json.*

var dir = ConfigStoreOptions(
  type = "directory",
  config = json {
    obj(
      "path" to "config",
      "filesets" to array(obj("pattern" to "dir/*json"), obj(
        "pattern" to "dir/*.properties",
        "format" to "properties"
      ))
    )
  })

Listening for configuration changes

The Configuration Retriever periodically retrieve the configuration and if the outcome is different from the current one, your application can be reconfigured. By default the configuration is reloaded every 5 seconds.

import io.vertx.config.ConfigRetriever
import io.vertx.config.ConfigRetrieverOptions
import io.vertx.core.Vertx
import io.vertx.kotlin.config.*

var options = ConfigRetrieverOptions(
  scanPeriod = 2000,
  stores = listOf(store1, store2))

var retriever = ConfigRetriever.create(Vertx.vertx(), options)
retriever.getConfig({ json ->
  // Initial retrieval of the configuration
})

retriever.listen({ change ->
  // Previous configuration
  var previous = change.previousConfiguration
  // New configuration
  var conf = change.newConfiguration
})

Retrieving the last retrieved configuration

You can retrieved the last retrieved configuration without "waiting" to be retrieved using:

var last = retriever.getCachedConfig()

Reading configuration as a stream

The ConfigRetriever provide a way to access the stream of configuration. It’s a ReadStream of JsonObject. By registering the right set of handlers you are notified:

  • when a new configuration is retrieved

  • when an error occur while retrieving a configuration

  • when the configuration retriever is closed (the endHandler is called).

import io.vertx.config.ConfigRetriever
import io.vertx.config.ConfigRetrieverOptions
import io.vertx.core.Vertx
import io.vertx.kotlin.config.*

var options = ConfigRetrieverOptions(
  scanPeriod = 2000,
  stores = listOf(store1, store2))

var retriever = ConfigRetriever.create(Vertx.vertx(), options)
retriever.configStream().endHandler({ v ->
  // retriever closed
}).exceptionHandler({ t ->
  // an error has been caught while retrieving the configuration
}).handler({ conf ->
  // the configuration
})

Retrieving the configuration as a Future

The ConfigRetriever provide a way to retrieve the configuration as a Future:

import io.vertx.config.ConfigRetriever

var future = ConfigRetriever.getConfigAsFuture(retriever)
future.setHandler({ ar ->
  if (ar.failed()) {
    // Failed to retrieve the configuration
  } else {
    var config = ar.result()
  }
})

Extending the Configuration Retriever

You can extend the configuration by implementing:

  • the io.vertx.config.spi.ConfigurationProcessor SPI to add support for a format

  • the io.vertx.config.spi.ConfigurationStoreFactory SPI to add support for configuration store (place from where the configuration data is retrieved)