Contents
Overview
Helidon Config provides several extension modules that support other configuration formats (parsers) and sources. This document describes how to include them and use them in your project. In each case you need to add module dependencies to your project and, in some cases, write your application accordingly.
Additional Config Formats and Parsers
Automatic Media Type and File Type Handling
With each of the parsers described here, your application can either
- explicitly add a parser of the correct implementation to the
Config.Builder, or - rely on Java service loading and the config system’s matching of file types and media types to parsers.
If your application creates a Config.Builder with parser services disabled (see disableParserServices then that builder will not find the Java services for the various parsers and so will be unable to match the file type or media type of sources with the corresponding parser automatically. So if you want to use automatic type matching with a given builder, do not invoke Config.Builder.disableParserServices().
YAML
Maven Coordinates
Add the following dependency in your project:
pom.xml <dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>module-info.javamodule myModule {
requires io.helidon.config.yaml;
}Using the YAML Parser
The YAML parser handles the following media type:
application/x-yaml- YAML format (file type.yaml)
Config config = Config.create(classpath("application.yaml")); - The config system automatically maps the file type
.yamlto the media typeapplication/x-yamlwhich the Helidon YAML parser matches.
Config config = Config.create(classpath("my-config")
.parser(YamlConfigParserBuilder.buildDefault())); - The media type of the source
my-configis unknown, so the config system cannot choose a parser automatically. - The config system will parse the resource
my-configon the runtime classpath using the YAML parser instance created by theYamlConfigParserBuilder. ThebuildDefault()method creates a config parser with default behavior.
Config config = Config.create(classpath("my-config")
.mediaType("application/x-yaml")); - The media type of the source
my-configis unknown, so the config system cannot choose a parser automatically. - Specifying the media type for the config source allows the config system to use its matching algorithm with the available parsers to choose a parser for that type.
Config config = Config.builder(classpath("application.yaml"))
.disableParserServices()
.addParser(YamlConfigParserBuilder.buildDefault())
.build();- Disables automatic parser lookup and registration.
- Explicit registration of the YAML parser is therefore required.
HOCON/JSON
The Helidon HOCON config module handles sources in the HOCON and JSON formats.
Maven Coordinates
Add the following dependency in your project:
pom.xml <dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-hocon</artifactId>
</dependency>module-info.javamodule myModule {
requires io.helidon.config.hocon;
}Using the HOCON/JSON Parser
The parser handles the following media types:
application/hocon- HOCON format (file type.conf)application/json- JSON format (file type.json)
Config config = Config.create(classpath("application.conf")); - The config system automatically maps the file type
.confto the media type `application/hocon which the Helidon HOCON parser matches.
The same module and parser supports file type .json and the media type application/json.
Config config = Config.create(classpath("my-config")
.parser(HoconConfigParser.create())); - the media type of the source
my-configis unknown, so the config system cannot choose a parser automatically. - The config system will parse the resource
my-configusing the HOCON parser created by the HoconConfigParser. Thecreate()method creates a config parser with default behavior.
Config config = Config.create(classpath("my-config")
.mediaType("application/hocon")); - The media type of the source
my-configis unknown, so the config system cannot choose a parser automatically. - Specifying the media type for the config source allows the config system to use its matching algorithm with the available parsers to choose a parser for that type.
Config config = Config.builder(classpath("application.conf"))
.disableParserServices()
.addParser(HoconConfigParser.create())
.build();- Disables automatic parser lookup and registration.
- Explicit registration of the HOCON parser is therefore required.
Config config = Config.builder(classpath("application.conf"))
.disableParserServices()
.addParser(HoconConfigParser.builder()
.resolvingEnabled(false)
.build())
.build();- Creates new instance of the parser builder.
- Disables resolution of substitutions. (See the HOCON documentation.)
- Builds a new instance of the HOCON config parser.
You can also specify ConfigResolveOptions using the HoconConfigParser.builder().resolveOptions method.
Additional Config Source Types
Etcd
The Helidon Etcd config module supports reading configuration from a specified Etcd key.
Maven Coordinates
Add the following dependency to your project:
pom.xml <dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-etcd</artifactId>
</dependency>module-info.javamodule myModule {
requires io.helidon.config.etcd;
}Using the Etcd Config Source
To read configuration from an Etcd source, your application uses the EtcdConfigSourceBuilder.
Config config = Config.create(
EtcdConfigSourceBuilder
.create(URI.create("http://my-etcd:2379"),
"/config.yaml",
EtcdConfigSourceBuilder.EtcdApi.v3)); - Use the factory method
EtcdConfigSourceBuilder.createto initialize the builder. - Specify the Etcd endpoint address.
- Specify the Etcd key of the configuration document.
- Version of the Etcd API to use;
v2andv3are supported.
The config system will use the YAML parser automatically in this example because the file type of the key is .yaml.
The EtcdConfigSourceBuilder class extends AbstractParsableConfigSource.Builder and so supports the usual settings on config sources.
Monitoring for Source Changes
The Etcd support includes a polling strategy designed for an etcd config source.
Config config = Config.create(
EtcdConfigSourceBuilder
.create(URI.create("http://my-etcd:2379"), "/config.yaml", EtcdApi.v3)
.pollingStrategy(EtcdWatchPollingStrategy::new)); - Use the etcd-specific polling strategy.
Loading Meta-configuration via Etcd
The config system can load information about config sources from meta-configuration rather than requiring your application to construct the builder. To read meta-configuration from an Etcd source set the following required properties for the source:
typetoetcd, orclasstoio.helidon.config.etcd.EtcdConfigSourceBuilderuri(typeURI) - Etcd endpoint URI.key(typeString) - Etcd key that is associated with the configuration.api(typeEtcdConfigSourceBuilder.EtcdApi, i.e.v2orv3) - Etcd API version.
Other optional properties are inherited from AbstractParsableConfigSource.Builder. (see javadoc)
Config config = Config.loadSourcesFrom(classpath("config-meta-etcd.yaml"));config-meta-etcd.yaml for the etcd sourcesources:
- type: "etcd"
properties:
uri: "http://my-etcd:2379"
key: "/config.yaml"
api: "v3"
polling-strategy:
class: "io.helidon.config.etcd.EtcdWatchPollingStrategy" etcdconfig source type- Etcd source-specific (mandatory)
properties:uri,keyandapi. - Polling strategy
EtcdWatchPollingStrategyis automatically initialized by specified mandatoryproperties.
git
The Helidon git config module supports reading configuration from a git repository.
Maven Coordinates
Add the following dependency to your project:
pom.xml <dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-git</artifactId>
</dependency>module-info.javamodule myModule {
requires io.helidon.config.git;
}Using the git Config Source
To read configuration from a git source, your application uses the GitConfigSourceBuilder.
Config config = Config.create(
GitConfigSourceBuilder
.create("application.conf")
.uri(URI.create("https://github.com/okosatka/test-config.git"))
.directory(Paths.get("/config"))
.branch("dev")); - Use the factory method
GitConfigSourceBuilder.createto initialize the builder with a mandatory path to the configuration file. - Specify the git repository URI.
- Specify a directory where the git repository is already cloned or it will be cloned.
- Specify the git branch.
Note that the config system will use the HOCON parser in this example because the file type is .conf. Recall that for this to work the HOCON config module must be on module-path or classpath.
The GitConfigSourceBuilder supports the usual source builder properties because it extends AbstractParsableConfigSource.Builder.
Monitoring for Source Changes
Your application can monitor changes to a configuration loaded from a git source associating the regular built-in polling strategy with the source.
Config config = Config.create(
GitConfigSourceBuilder
.create("application.conf")
.uri(URI.create("https://github.com/okosatka/test-config.git"))
.pollingStrategy(PollingStrategies.regular(Duration.ofMinutes(5)))); - Use
PollingStrategies.regular(Duration duration)to monitor for config changes.
You can also implement your own polling strategy by implementing PollingStrategy. See the mutability support and polling strategy discussions.
Loading Meta-configuration via git
The config system can load information about config sources from meta-configuration rather than requiring your application to construct the builder. To read meta-configuration from a git config source set the following properties for the source:
typetogitorclasstoio.helidon.config.git.GitConfigSourceBuilderpath(typeString) - Relative path to the configuration file in repository.uri(typeURI) - URI to the git repository.directory(typePath) - Directory with a cloned repository, by default a temporary directory.branch(typeString) - git branch (default ismaster).
The meta-configuration must set the path and one of uri or directory. Other optional properties are inherited from AbstractParsableConfigSource.Builder (see javadoc)
Config config = Config.loadSourcesFrom(classpath("config-meta-git.yaml"));config-meta-git.yaml for the git sourcesources:
- type: "git"
properties:
path: "application.conf"
uri: "https://github.com/okosatka/test-config.git"
directory: "/config"
branch: "dev"
polling-strategy:
type: "regular"
properties:
interval: "PT5M" gitconfig source type- git source-specific properties:
path,uri,directoryandbranch. - Polling strategy
regularwith an interval, inDurationformat, of 5 minutes in this example.