When we’ll develop an application, we need define some configurations to our application, such as : configuration about environment, configuration about integration and others. For a long time, many developers putted its configurations inside application. But, it has a problem, because when we need change these configurations we need rebuild the application and the same package can work in one environment but take a error in another environment, because the package is prepared to specific environment. It’s a bad practice, but when we work with Cloud and microservice this problem grow up.
Eclipse Microprofile Config is a solution that permit us externalize configurations from application (more specifically microservice). This solution works with ConfigSource, which provides configuration properties and works with an order (defined by an ordinal). This solution has 3 default ConfigSources, but we can create a custom ConfigSource to get the configuration properties of any source. These default ConfigSource are:
- System.getProperties() (ordinal=400)
- System.getenv() (ordinal=300)
- All META-INF/microprofile-config.properties files on the ClassPath (default ordinal=100, separately configurable via a config_ordinal property inside each file)
With this solution you can read the configuration from a external source using CDI Injection. Bellow we have an example.
@Inject @ConfigProperty(name ="bd.subject.path",defaultValue = "http://172.18.0.5:8080/") private String bdPath;
On the code above we get a configuration of path to a database by CDI Injection, and if the property is not found by the ConfigSource, the default valuer ( defaultValue = “http://172.18.0.5:8080/”) is inserted.
If you can see more about this example, access the repository on github: https://github.com/rhuan080/microprofile-example