Eclipse MicroProfile configuration


Microprofile config, helps application to read the configurations from different configurations sources. 

Different sources for configuration could be: 
1. Operating system environment variables ( mostly used in cloud native environment) 
2. JVM system properties.
3. Database, LDAP or any other key-value store ( example: [HasiCorp vault] (https://www.hashicorp.com/products/vault) ) 
4. External configuration files. 

#### Reading config properties
##### Programmatic:
An application can obtain its configuration programmatically via the `ConfigProvider`.

```
	Config config = ConfigProvider.getConfig();
    ConfigValue databaseName = config.getConfigValue("myapplication.databaseName");
    connect(dbUsername.getValue());

```

##### Dependency Injection:
MicroProfile Config also provides ways to inject configured values into your beans using the `@Inject` and the `@ConfigProperty` qualifier

```
    @Inject
    @ConfigProperty(name="myapplication.propertyname", defaultValue="Hello")
    private String someProperty;
```

If property value is not found and default value is also not provided then the application will throw *DeploymentException* as application cannot be properly configured.

The configuation property priority is based in the **ordinal** number defined for each source. If a particular property is defined in multiple config. sources then the value of property will be taken from the config source with high ordinal number value.
By default, following are the precedence order of config source:
	1. System properties (JVM properties)
    2. Environment variables
    3. resources/META-INF/microprofile-config.properties 
    
#### Remove config properties
This can be done by setting an empty value or a value causing the corresponding converter returning null in a config source. 

#### Property mapping
Microprofile provides mapping property name feature for environment variables using below rules. [See More details here](https://github.com/eclipse/ConfigJSR/issues/53#issuecomment-375123464): 

The mapping rules are as follow:

1. Exact match. The property in application exactly match with environment variables name. 
2. Replace the character that is non-alphanumeric with '_' 
3. Replace the character that is non-alphanumeric with '_' and convert to upper case.

Hence, if application property name used in applicaton is - ```my.app.property``` then environment variable can be - ```my_app_property``` or ```MY_APP_PROPERTY```.

#### Property Profile
We can have different values for properties as per different environments. There are two different ways to define properties for different environment. 
1. Using specific naming convention of property - `%<profile_name>.<property_name>` example: `%dev.username` 
2. Using specific property file for each profile, the naming convention for property file - `META-INF\microprofile-config-<profile_name>.properties `.

##### How to activate a profile 
An active profile can be define using property - **mp.config.profile** which can be included in any of config sources. 



Comments

Popular posts from this blog

Creating simple Maven multi module project in Java

Tricky Java Questions

How to update existing CCDT file (AMQCLCHL.TAB) for successful MQueue connection