Understanding Eclipse Microprofile Config

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:

  1. System.getProperties() (ordinal=400)
  2. System.getenv() (ordinal=300)
  3. 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

 

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s