项目作者: kumuluz

项目描述 :
KumuluzEE MicroProfile Config 1.2 provides complete implementation of Eclipse MicroProfile Config 1.2 API for configuring microservices.
高级语言: Java
项目地址: git://github.com/kumuluz/kumuluzee-config-mp.git
创建时间: 2017-09-26T20:10:40Z
项目社区:https://github.com/kumuluz/kumuluzee-config-mp

开源协议:Other

下载


KumuluzEE MicroProfile Config

KumuluzEE CI

KumuluzEE Config MicroProfile extension provides you with a complete Eclipse MicroProfile Feature implementation for configuring your applications.

KumuluzEE MicroProfile Config 1.2 implements the
MicroProfile Config 1.2 API.

KumuluzEE MicroProfile Config contains APIs for accessing the configuration values from
KumuluzEE Configuration framework and adding custom
MicroProfile ConfigSources.

Usage

MicroProfile Config API can be enabled by adding the following Maven dependency:

  1. <dependency>
  2. <groupId>com.kumuluz.ee.config</groupId>
  3. <artifactId>kumuluzee-config-mp</artifactId>
  4. <version>${kumuluzee-config-mp.version}</version>
  5. </dependency>

Configuration sources

KumuluzEE MicroProfile Config implementation exposes configuration from the
KumuluzEE configuration framework.

Supported configuration sources (sorted by their priority) are:

  • System properties (ordinal 400)
  • Environment variables (ordinal 300)
  • Configuration file (ordinal 100; config.yml, config.yaml, config.properties or
    META-INF/microprofile-config.properties; configuration file name can be overridden by specifying its name in the
    com.kumuluz.ee.configuration.file system property)

Additional configuration sources (including configuration servers such as etcd and Consul implemented in
kumuluzee-config) have the default ordinal 110. Ordinal value can be overridden for every configuration source by
setting the configuration key config_ordinal in the configuration source to the desired value.

Accessing configuration values

There are a few possible approaches for getting Config instance and accessing configuration values, as described in
this section.

Programmatic access to the configuration object

You can access the Config object programmatically:

  1. Config config = ConfigProvider.getConfig();
  2. String configValue = config.getValue("mp.test", String.class));

CDI injection of the configuration object

You can use CDI injection to get the Config object:

  1. @Inject
  2. private Config config;
  3. String configValue = config.getValue("mp.test", String.class));

CDI injection of the configuration values

You can get the configuration values directly with CDI injection:

  1. @Inject
  2. @ConfigProperty(name = "mp.test", defaultValue="defaultTestValue")
  3. private String injectedString;
  4. @Inject
  5. @ConfigProperty(name = "mp.test")
  6. private Optional<String> injectedStringOpt;
  7. @Inject
  8. @ConfigProperty(name = "mp.test")
  9. private Provider<String> stringProvider;

If the requested property does not exist, the injections in the first and third example throw DeploymentException.
Injection in the second example will return Optional.empty(), if the requested property does not exist.

Converters

Since configuration sources return values of type String, converters are used to convert received values to the types
that were requested. All converters from the MicroProfile Config specification are implemented. They are as follows:

  1. boolean and Boolean (“true”, “1”, “YES”, “Y”, “ON” are considered true (case insensitive), everything else
    false)
  2. int and Integer
  3. long and Long
  4. float and Float
  5. double and Double
  6. Duration
  7. LocalTime
  8. LocalDate
  9. LocalDateTime
  10. OffsetDateTime
  11. OffsetTime
  12. Instant
  13. URL
  14. Class (based on the result of Class.forName)
  15. Common sense converter (Tries to find class’s constructor with a single string parameter or static methods
    valueOf(String) or parse(CharSequence))

You can add custom converters by implementing
the org.eclipse.microprofile.config.spi.Converter interface and registering your implementation in the
/META-INF/services/org.eclipse.microprofile.config.spi.Converter file with the fully qualified class name. Example of
a custom converter is shown below:

  1. @Priority(500)
  2. public class CustomerConverter implements Converter<Customer> {
  3. @Override
  4. public Customer convert(String s) {
  5. return Customer.parse(s);
  6. }
  7. }

You can create multiple Converters for the same type. Converter with the highest priority will be used. Priority is
read from the @Priority annotation.

Injection of arrays

Injection of arrays is supported. Arrays are defined as comma separated values in the configuration values. For example,
to define array in microprofile-config.properties file, add the following line:

  1. mp.example-string-array=first,second,third\\,still_third

The commas can be escaped with the backslash character (in .properties files, the backslash character must be escaped
itself as shown above). Also note, that converters are applied to each array element, which means that array types are
not limited only to String.

To inject the property defined above, use the following code:

  1. @Inject
  2. @ConfigProperty(name = "mp.example-string-array")
  3. private String[] strings;

Injection of Set and List is also supported:

  1. @Inject
  2. @ConfigProperty(name = "mp.example-string-array")
  3. List<String> stringsAsList;
  4. @Inject
  5. @ConfigProperty(name = "mp.example-string-array")
  6. Set<String> stringsAsSet;

Adding custom configuration sources

Custom configuration sources can be added to extend the configuration framework.
You can add custom configuration source by implementing the org.eclipse.microprofile.config.spi.ConfigSource
interface and registering your implementation in the
/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource file with the fully qualified class name.
Example of a custom configuration source is shown below:

  1. public class ExampleConfigSource implements ConfigSource {
  2. @Override
  3. public Map<String, String> getProperties() {
  4. return null;
  5. }
  6. @Override
  7. public int getOrdinal() {
  8. return 120;
  9. }
  10. @Override
  11. public String getValue(String s) {
  12. if ("mp.custom-source-value".equals(s)) {
  13. return "Hello from custom ConfigSource";
  14. }
  15. return null;
  16. }
  17. @Override
  18. public String getName() {
  19. return "ExampleConfigSource";
  20. }
  21. }

To dynamically add multiple configuration sources, implement the
org.eclipse.microprofile.config.spi.ConfigSourceProvider interface and register your implementation in the
/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSourceProvider file with the fully qualified class name.
The interface contains the method Iterable<ConfigSource> getConfigSources(ClassLoader classLoader) in which multiple
configuration sources can be registered programmatically.

Changelog

Recent changes can be viewed on Github on the Releases Page

Contribute

See the contributing docs

When submitting an issue, please follow the
guidelines.

When submitting a bugfix, write a test that exposes the bug and fails before applying your fix. Submit the test
alongside the fix.

When submitting a new feature, add tests that cover the feature.

License

MIT