Configuring microservices

duration 20 minutes

Prerequisites:

Learn how to provide external configuration to microservices using MicroProfile Config.

What you’ll learn

You will learn how to externalize and inject both static and dynamic configuration properties for microservices using MicroProfile Config.

You will learn to aggregate multiple configuration sources, assign prioritization values to these sources, merge configuration values, and create custom configuration sources.

The application that you will be working with is an inventory service which stores the information about various JVMs running on different hosts. Whenever a request is made to the inventory service to retrieve the JVM system properties of a particular host, the inventory service will communicate with the system service on that host to get these system properties. You will add configuration properties to simulate if a service is down for maintenance.

Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

git clone https://github.com/openliberty/guide-microprofile-config.git
cd guide-microprofile-config

The start directory contains the starting project that you will build upon.

The finish directory contains the finished project that you will build.

Before you begin, make sure you have all the necessary prerequisites.

Try what you’ll build

The finish directory in the root of this guide contains the finished application. Give it a try before you proceed.

To try out the application, first go to the finish directory and run the following Maven goal to build the application and deploy it to Open Liberty:

cd finish
mvn liberty:run

After you see the following message, your Liberty instance is ready:

The defaultServer server is ready to run a smarter planet.

You can access the following two microservices to test their availability:

In addition, you can access a third microservice, which retrieves and aggregates all of the configuration properties and sources that are added throughout this guide. This is available at:

After you are finished checking out the application, stop the Liberty instance by pressing CTRL+C in the command-line session where you ran Liberty. Alternatively, you can run the liberty:stop goal from the finish directory in another shell session:

mvn liberty:stop

Ordering multiple configuration sources

Now, navigate to the start directory to begin.

When you run Open Liberty in dev mode, dev mode listens for file changes and automatically recompiles and deploys your updates whenever you save a new change. Run the following goal to start Open Liberty in dev mode:

mvn liberty:dev

After you see the following message, your Liberty instance is ready in dev mode:

**************************************************************
*    Liberty is running in dev mode.

Dev mode holds your command-line session to listen for file changes. Open another command-line session to continue, or open the project in your editor.

MicroProfile Config combines configuration properties from multiple sources, each known as a ConfigSource. Each ConfigSource has a specified priority, defined by its config_ordinal value.

A higher ordinal value means that the values taken from this ConfigSource will override values from ConfigSources with a lower ordinal value.

The following four sources are the default configuration sources:

  • A <variable name="…​" value="…​"/> element in the server.xml file has a default ordinal of 500.

  • System properties has a default ordinal of 400. (e.g. bootstrap.properties file)

  • Environment variables have a default ordinal of 300. (e.g. server.env file)

  • The META-INF/microprofile-config.properties configuration property file on the classpath has a default ordinal of 100.

Access the src/main/resources/META-INF/microprofile-config.properties local configuration file. This configuration file is the default configuration source for an application that uses MicroProfile Config.

microprofile-config.properties

 1# tag::all-props[]
 2# tag::ordinal[]
 3config_ordinal=100
 4# end::ordinal[]
 5# tag::inventory-port-number[]
 6io_openliberty_guides_port_number=9080
 7# end::inventory-port-number[]
 8# tag::inventory-in-maintenance[]
 9io_openliberty_guides_inventory_inMaintenance=false
10# end::inventory-in-maintenance[]
11# tag::system-in-maintenance[]
12io_openliberty_guides_system_inMaintenance=false
13# tag::system-in-maintenance[]
14# tag::test-config-overwrite[]
15io_openliberty_guides_testConfigOverwrite=DefaultSource
16# tag::test-config-overwrite[]
17# end::all-props[]

Injecting static configuration

The MicroProfile Config API is included in the MicroProfile dependency that is specified in your pom.xml file. Look for the dependency with the microprofile artifact ID. This dependency provides a library that allows you to use the MicroProfile Config API to externalize configurations for your microservices. The mpConfig feature is also enabled in the src/main/liberty/config/server.xml file.

pom.xml

  1<?xml version='1.0' encoding='utf-8'?>
  2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3
  4    <modelVersion>4.0.0</modelVersion>
  5
  6    <groupId>io.openliberty.guides</groupId>
  7    <artifactId>guide-microprofile-config</artifactId>
  8    <version>1.0-SNAPSHOT</version>
  9    <packaging>war</packaging>
 10
 11    <properties>
 12        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 13        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 14        <maven.compiler.source>11</maven.compiler.source>
 15        <maven.compiler.target>11</maven.compiler.target>
 16        <!-- Liberty configuration -->
 17        <liberty.var.http.port>9080</liberty.var.http.port>
 18        <liberty.var.https.port>9443</liberty.var.https.port>
 19    </properties>
 20
 21    <dependencies>
 22        <!-- Provided dependencies -->
 23        <dependency>
 24            <groupId>jakarta.platform</groupId>
 25            <artifactId>jakarta.jakartaee-api</artifactId>
 26            <version>10.0.0</version>
 27            <scope>provided</scope>
 28        </dependency>
 29        <!-- tag::microprofile[] -->
 30        <dependency>
 31            <groupId>org.eclipse.microprofile</groupId>
 32            <artifactId>microprofile</artifactId>
 33            <version>6.1</version>
 34            <type>pom</type>
 35            <scope>provided</scope>
 36        </dependency>
 37        <!-- end::microprofile[] -->
 38        <!-- For tests -->
 39        <dependency>
 40            <groupId>org.junit.jupiter</groupId>
 41            <artifactId>junit-jupiter</artifactId>
 42            <version>5.11.3</version>
 43            <scope>test</scope>
 44        </dependency>
 45        <dependency>
 46            <groupId>org.jboss.resteasy</groupId>
 47             <artifactId>resteasy-client</artifactId>
 48             <version>6.2.10.Final</version>
 49            <scope>test</scope>
 50        </dependency>
 51        <dependency>
 52            <groupId>org.jboss.resteasy</groupId>
 53             <artifactId>resteasy-json-binding-provider</artifactId>
 54             <version>6.2.10.Final</version>
 55            <scope>test</scope>
 56        </dependency>
 57        <dependency>
 58            <groupId>org.glassfish</groupId>
 59            <artifactId>jakarta.json</artifactId>
 60             <version>2.0.1</version>
 61            <scope>test</scope>
 62        </dependency>
 63    </dependencies>
 64
 65    <build>
 66        <finalName>${project.artifactId}</finalName>
 67        <plugins>
 68            <plugin>
 69                <groupId>org.apache.maven.plugins</groupId>
 70                <artifactId>maven-war-plugin</artifactId>
 71                <version>3.4.0</version>
 72            </plugin>
 73            <!-- Plugin to run unit tests -->
 74            <plugin>
 75                <groupId>org.apache.maven.plugins</groupId>
 76                <artifactId>maven-surefire-plugin</artifactId>
 77                <version>3.5.1</version>
 78            </plugin>
 79            <!-- Enable liberty-maven plugin -->
 80            <plugin>
 81                <groupId>io.openliberty.tools</groupId>
 82                <artifactId>liberty-maven-plugin</artifactId>
 83                <version>3.11.1</version>
 84            </plugin>
 85            <!-- Plugin to run functional tests -->
 86            <plugin>
 87                <groupId>org.apache.maven.plugins</groupId>
 88                <artifactId>maven-failsafe-plugin</artifactId>
 89                <version>3.5.1</version>
 90                <configuration>
 91                    <systemPropertyVariables>
 92                      <http.port>${liberty.var.http.port}</http.port>
 93                      <https.port>${liberty.var.https.port}</https.port>
 94                    </systemPropertyVariables>
 95                </configuration>
 96            </plugin>
 97        </plugins>
 98    </build>
 99</project>

server.xml

 1<server description="Sample Liberty server">
 2
 3  <featureManager>
 4    <feature>restfulWS-3.1</feature>
 5    <feature>jsonb-3.0</feature>
 6    <feature>jsonp-2.1</feature>
 7    <feature>cdi-4.0</feature>
 8    <feature>mpRestClient-3.0</feature>
 9    <!-- tag::config[] -->
10    <feature>mpConfig-3.1</feature>
11    <!-- end::config[] -->
12  </featureManager>
13
14  <variable name="http.port" defaultValue="9080" />
15  <variable name="https.port" defaultValue="9443" />
16
17  <httpEndpoint host="*" httpPort="${http.port}"
18    httpsPort="${https.port}" id="defaultHttpEndpoint" />
19
20  <webApplication location="guide-microprofile-config.war"
21    contextRoot="/" />
22</server>

Now navigate to the src/main/resources/META-INF/microprofile-config.properties local configuration file to check some static configuration. This configuration file is the default configuration source for an application that uses MicroProfile Config.

The io_openliberty_guides_port_number property that has already been defined in this file, determines the port number of the REST service.

microprofile-config.properties

 1# tag::all-props[]
 2# tag::ordinal[]
 3config_ordinal=100
 4# end::ordinal[]
 5# tag::inventory-port-number[]
 6io_openliberty_guides_port_number=9080
 7# end::inventory-port-number[]
 8# tag::inventory-in-maintenance[]
 9io_openliberty_guides_inventory_inMaintenance=false
10# end::inventory-in-maintenance[]
11# tag::system-in-maintenance[]
12io_openliberty_guides_system_inMaintenance=false
13# tag::system-in-maintenance[]
14# tag::test-config-overwrite[]
15io_openliberty_guides_testConfigOverwrite=DefaultSource
16# tag::test-config-overwrite[]
17# end::all-props[]

To use this configuration property,

Create the InventoryConfig.java class.
src/main/java/io/openliberty/guides/inventory/InventoryConfig.java

InventoryConfig.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2022 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License 2.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-2.0/
 8 *
 9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11// end::copyright[]
12// tag::config-class[]
13package io.openliberty.guides.inventory;
14
15import jakarta.enterprise.context.RequestScoped;
16import jakarta.inject.Inject;
17import jakarta.inject.Provider;
18import org.eclipse.microprofile.config.inject.ConfigProperty;
19import io.openliberty.guides.config.Email;
20
21@RequestScoped
22public class InventoryConfig {
23
24  // tag::port-number[]
25  // tag::inject-port-number[]
26  @Inject
27  // end::inject-port-number[]
28  // tag::guides-port-number[]
29  @ConfigProperty(name = "io_openliberty_guides_port_number")
30  // end::guides-port-number[]
31  private int portNumber;
32
33  // end::port-number[]
34  // tag::build-in-converter[]
35  // tag::inject-inMaintenance[]
36  // tag::inject[]
37  @Inject
38  // end::inject[]
39  // tag::configPropety[]
40  @ConfigProperty(name = "io_openliberty_guides_inventory_inMaintenance")
41  // end::configPropety[]
42  // end::inject-inMaintenance[]
43  private Provider<Boolean> inMaintenance;
44
45  // end::build-in-converter[]
46  // tag::custom-converter[]
47  @Inject
48  @ConfigProperty(name = "io_openliberty_guides_email")
49  private Provider<Email> email;
50  // end::custom-converter[]
51
52  // tag::getPortNumber[]
53  public int getPortNumber() {
54    return portNumber;
55  }
56  // end::getPortNumber[]
57
58  // tag::isInMaintenance[]
59  public boolean isInMaintenance() {
60    // tag::inMaintenanceGet[]
61    return inMaintenance.get();
62    // end::inMaintenanceGet[]
63  }
64  // end::isInMaintenance[]
65
66  // tag::getEmail[]
67  public Email getEmail() {
68    return email.get();
69  }
70  // end::getEmail[]
71}
72// end::config-class[]

Inject the io_openliberty_guides_port_number property, and add the getPortNumber() class method to the InventoryConfig.java file.

The @Inject annotation injects the port number directly, the injection value is static and fixed on application starting.

The getPortNumber() method directly returns the value of portNumber because it has been injected.

Injecting dynamic configuration

Note that three default config sources mentioned above are static and fixed on application starting, so the properties within them cannot be modified while the Liberty is running. However, you can externalize configuration data out of the application package, through the creation of custom configuration sources, so that the service updates configuration changes dynamically.

Creating custom configuration sources

CustomConfigSource.json

1{
2  "config_ordinal": 150,
3  "io_openliberty_guides_email": "[email protected]",
4  "io_openliberty_guides_inventory_inMaintenance": false,
5  "io_openliberty_guides_system_inMaintenance": false,
6  "io_openliberty_guides_testConfigOverwrite": "CustomSource"
7}

Custom configuration sources can be created by implementing the org.eclipse.microprofile.config.spi.ConfigSource interface and using the java.util.ServiceLoader mechanism.

A CustomConfigSource.json JSON file has already been created in the resources directory. This JSON file simulates a remote configuration resource in real life. This file contains 4 custom config properties and has an ordinal of 150. To use these properties in the application, the data object needs to be transformed from this JSON file to the configuration for your application.

To link this JSON file to your application and to implement the ConfigSource interface,

Create the CustomConfigSource class.
src/main/java/io/openliberty/guides/config/CustomConfigSource.java

CustomConfigSource.java

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2017, 2022 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License 2.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-2.0/
  8 *
  9 * SPDX-License-Identifier: EPL-2.0
 10 *******************************************************************************/
 11// end::copyright[]
 12// tag::customConfig[]
 13package io.openliberty.guides.config;
 14
 15import jakarta.json.stream.JsonParser;
 16import jakarta.json.stream.JsonParser.Event;
 17import jakarta.json.Json;
 18import java.math.BigDecimal;
 19import java.io.StringReader;
 20import java.util.HashMap;
 21import java.util.Map;
 22import java.util.Set;
 23
 24import java.io.BufferedReader;
 25import java.io.FileReader;
 26import org.eclipse.microprofile.config.spi.ConfigSource;
 27
 28/**
 29 * User-provided ConfigSources are dynamic.
 30 * The getProperties() method will be periodically invoked by the runtime
 31 * to retrieve up-to-date values. The frequency is controlled by
 32 * the microprofile.config.refresh.rate Java system property,
 33 * which is in milliseconds and can be customized.
 34 */
 35public class CustomConfigSource implements ConfigSource {
 36
 37  String fileLocation = System.getProperty("user.dir").split("target")[0]
 38      + "resources/CustomConfigSource.json";
 39
 40  @Override
 41  public int getOrdinal() {
 42    return Integer.parseInt(getProperties().get("config_ordinal"));
 43  }
 44
 45  @Override
 46  public Set<String> getPropertyNames() {
 47    return getProperties().keySet();
 48  }
 49
 50  @Override
 51  public String getValue(String key) {
 52    return getProperties().get(key);
 53  }
 54
 55  @Override
 56  public String getName() {
 57    return "Custom Config Source: file:" + this.fileLocation;
 58  }
 59
 60  // tag::getProperties[]
 61  public Map<String, String> getProperties() {
 62    Map<String, String> m = new HashMap<String, String>();
 63    String jsonData = this.readFile(this.fileLocation);
 64    JsonParser parser = Json.createParser(new StringReader(jsonData));
 65    String key = null;
 66    while (parser.hasNext()) {
 67      final Event event = parser.next();
 68      switch (event) {
 69      case KEY_NAME:
 70        key = parser.getString();
 71        break;
 72      case VALUE_STRING:
 73        String string = parser.getString();
 74        m.put(key, string);
 75        break;
 76      case VALUE_NUMBER:
 77        BigDecimal number = parser.getBigDecimal();
 78        m.put(key, number.toString());
 79        break;
 80      case VALUE_TRUE:
 81        m.put(key, "true");
 82        break;
 83      case VALUE_FALSE:
 84        m.put(key, "false");
 85        break;
 86      default:
 87        break;
 88      }
 89    }
 90    parser.close();
 91    return m;
 92  }
 93  // end::getProperties[]
 94
 95  public String readFile(String fileName) {
 96    String result = "";
 97    try {
 98      BufferedReader br = new BufferedReader(new FileReader(fileName));
 99      StringBuilder sb = new StringBuilder();
100      String line = br.readLine();
101      while (line != null) {
102        sb.append(line);
103        line = br.readLine();
104      }
105      result = sb.toString();
106      br.close();
107    } catch (Exception e) {
108      e.printStackTrace();
109    }
110    return result;
111  }
112}
113// end::customConfig[]

The getProperties() method reads the key value pairs from the resources/CustomConfigSource.json JSON file and writes the information into a map.

Finally, register the custom configuration source.

Create the configuration file.
src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource

org.eclipse.microprofile.config.spi.ConfigSource

1io.openliberty.guides.config.CustomConfigSource

Add the fully qualified class name of the configuration source into it.

Enabling dynamic configuration injection

Now that the custom configuration source has successfully been set up, you can enable dynamic configuration injection of the properties being set in this ConfigSource. To enable this dynamic injection,

Replace the InventoryConfig.java class.
src/main/java/io/openliberty/guides/inventory/InventoryConfig.java

InventoryConfig.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2022 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License 2.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-2.0/
 8 *
 9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11// end::copyright[]
12// tag::config-class[]
13package io.openliberty.guides.inventory;
14
15import jakarta.enterprise.context.RequestScoped;
16import jakarta.inject.Inject;
17import jakarta.inject.Provider;
18import org.eclipse.microprofile.config.inject.ConfigProperty;
19import io.openliberty.guides.config.Email;
20
21@RequestScoped
22public class InventoryConfig {
23
24  // tag::port-number[]
25  // tag::inject-port-number[]
26  @Inject
27  // end::inject-port-number[]
28  // tag::guides-port-number[]
29  @ConfigProperty(name = "io_openliberty_guides_port_number")
30  // end::guides-port-number[]
31  private int portNumber;
32
33  // end::port-number[]
34  // tag::build-in-converter[]
35  // tag::inject-inMaintenance[]
36  // tag::inject[]
37  @Inject
38  // end::inject[]
39  // tag::configPropety[]
40  @ConfigProperty(name = "io_openliberty_guides_inventory_inMaintenance")
41  // end::configPropety[]
42  // end::inject-inMaintenance[]
43  private Provider<Boolean> inMaintenance;
44
45  // end::build-in-converter[]
46  // tag::custom-converter[]
47  @Inject
48  @ConfigProperty(name = "io_openliberty_guides_email")
49  private Provider<Email> email;
50  // end::custom-converter[]
51
52  // tag::getPortNumber[]
53  public int getPortNumber() {
54    return portNumber;
55  }
56  // end::getPortNumber[]
57
58  // tag::isInMaintenance[]
59  public boolean isInMaintenance() {
60    // tag::inMaintenanceGet[]
61    return inMaintenance.get();
62    // end::inMaintenanceGet[]
63  }
64  // end::isInMaintenance[]
65
66  // tag::getEmail[]
67  public Email getEmail() {
68    return email.get();
69  }
70  // end::getEmail[]
71}
72// end::config-class[]

Inject the io_openliberty_guides_inventory_inMaintenance property, and add the isInMaintenance() class method.

The @Inject and @ConfigProperty annotations inject the io_openliberty_guides_inventory_inMaintenance configuration property from the CustomConfigSource.json file. The Provider<> interface used, forces the service to retrieve the inMaintenance value just in time. This retrieval of the value just in time makes the config injection dynamic and able to change without having to restart the application.

Every time that you invoke the inMaintenance.get() method, the Provider<> interface picks up the latest value of the io_openliberty_guides_inventory_inMaintenance property from configuration sources.

CustomConfigSource.json

1{
2  "config_ordinal": 150,
3  "io_openliberty_guides_email": "[email protected]",
4  "io_openliberty_guides_inventory_inMaintenance": false,
5  "io_openliberty_guides_system_inMaintenance": false,
6  "io_openliberty_guides_testConfigOverwrite": "CustomSource"
7}

Creating custom converters

Configuration values are purely Strings. MicroProfile Config API has built-in converters that automatically converts configured Strings into target types such as int, Integer, boolean, Boolean, float, Float, double and Double. Therefore, in the previous section, it is type-safe to directly set the variable type to Provider<Boolean>.

To convert configured Strings to an arbitrary class type, such as the Email class type,

Replace the Email Class.
src/main/java/io/openliberty/guides/config/Email.java

Email.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2019 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License 2.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-2.0/
 8 *
 9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11// end::copyright[]
12
13package io.openliberty.guides.config;
14
15// tag::email[]
16public class Email {
17  private String name;
18  private String domain;
19
20  public Email(String value) {
21    String[] components = value.split("@");
22    if (components.length == 2) {
23      name = components[0];
24      domain = components[1];
25    }
26  }
27
28  public String getEmailName() {
29    return name;
30  }
31
32  public String getEmailDomain() {
33    return domain;
34  }
35
36  public String toString() {
37    return name + "@" + domain;
38  }
39}
40// end::email[]

To use this Email class type, add a custom converter by implementing the generic interface org.eclipse.microprofile.config.spi.Converter<T>. The Type parameter of the interface is the target type the String is converted to.

Create the CustomEmailConverter class.
src/main/java/io/openliberty/guides/config/CustomEmailConverter.java

CustomEmailConverter.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2023 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License 2.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-2.0/
 8 *
 9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11// end::copyright[]
12package io.openliberty.guides.config;
13
14import org.eclipse.microprofile.config.spi.Converter;
15
16// tag::customConfig[]
17public class CustomEmailConverter implements Converter<Email> {
18
19  @Override
20  public Email convert(String value) {
21    return new Email(value);
22  }
23
24}
25// end::customConfig[]

This implements the Converter<T> interface.

To register your implementation,

Create the configuration file.
src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter

org.eclipse.microprofile.config.spi.Converter

1io.openliberty.guides.config.CustomEmailConverter

Add the fully qualified class name of the custom converter into it.

To use the custom Email converter,

Replace the InventoryConfig class.
src/main/java/io/openliberty/guides/inventory/InventoryConfig.java

InventoryConfig.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2022 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License 2.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-2.0/
 8 *
 9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11// end::copyright[]
12// tag::config-class[]
13package io.openliberty.guides.inventory;
14
15import jakarta.enterprise.context.RequestScoped;
16import jakarta.inject.Inject;
17import jakarta.inject.Provider;
18import org.eclipse.microprofile.config.inject.ConfigProperty;
19import io.openliberty.guides.config.Email;
20
21@RequestScoped
22public class InventoryConfig {
23
24  // tag::port-number[]
25  // tag::inject-port-number[]
26  @Inject
27  // end::inject-port-number[]
28  // tag::guides-port-number[]
29  @ConfigProperty(name = "io_openliberty_guides_port_number")
30  // end::guides-port-number[]
31  private int portNumber;
32
33  // end::port-number[]
34  // tag::build-in-converter[]
35  // tag::inject-inMaintenance[]
36  // tag::inject[]
37  @Inject
38  // end::inject[]
39  // tag::configPropety[]
40  @ConfigProperty(name = "io_openliberty_guides_inventory_inMaintenance")
41  // end::configPropety[]
42  // end::inject-inMaintenance[]
43  private Provider<Boolean> inMaintenance;
44
45  // end::build-in-converter[]
46  // tag::custom-converter[]
47  @Inject
48  @ConfigProperty(name = "io_openliberty_guides_email")
49  private Provider<Email> email;
50  // end::custom-converter[]
51
52  // tag::getPortNumber[]
53  public int getPortNumber() {
54    return portNumber;
55  }
56  // end::getPortNumber[]
57
58  // tag::isInMaintenance[]
59  public boolean isInMaintenance() {
60    // tag::inMaintenanceGet[]
61    return inMaintenance.get();
62    // end::inMaintenanceGet[]
63  }
64  // end::isInMaintenance[]
65
66  // tag::getEmail[]
67  public Email getEmail() {
68    return email.get();
69  }
70  // end::getEmail[]
71}
72// end::config-class[]

Inject the io_openliberty_guides_email property, and add the getEmail() method.

Adding configuration to the microservice

To use externalized configuration in the inventory service,

Replace the InventoryResource class.
src/main/java/io/openliberty/guides/inventory/InventoryResource.java

InventoryResource.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2022 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License 2.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-2.0/
 8 *
 9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11// end::copyright[]
12// tag::config-methods[]
13package io.openliberty.guides.inventory;
14
15import java.util.Properties;
16
17// CDI
18import jakarta.enterprise.context.RequestScoped;
19import jakarta.inject.Inject;
20import jakarta.ws.rs.GET;
21import jakarta.ws.rs.Path;
22import jakarta.ws.rs.PathParam;
23import jakarta.ws.rs.Produces;
24import jakarta.ws.rs.core.MediaType;
25import jakarta.ws.rs.core.Response;
26
27
28
29@RequestScoped
30@Path("systems")
31public class InventoryResource {
32
33  @Inject
34  InventoryManager manager;
35
36  // tag::config-injection[]
37  @Inject
38  InventoryConfig inventoryConfig;
39  // end::config-injection[]
40
41  @GET
42  @Path("{hostname}")
43  @Produces(MediaType.APPLICATION_JSON)
44  public Response getPropertiesForHost(@PathParam("hostname") String hostname) {
45
46    if (!inventoryConfig.isInMaintenance()) {
47      // tag::config-port[]
48      Properties props = manager.get(hostname, inventoryConfig.getPortNumber());
49      // end::config-port[]
50      if (props == null) {
51        return Response.status(Response.Status.NOT_FOUND)
52                       .entity("{ \"error\" : \"Unknown hostname or the system service "
53                       + "may not be running on " + hostname + "\" }")
54                       .build();
55      }
56
57      // Add to inventory
58      manager.add(hostname, props);
59      return Response.ok(props).build();
60    } else {
61      // tag::email[]
62      return Response.status(Response.Status.SERVICE_UNAVAILABLE)
63                     .entity("{ \"error\" : \"Service is currently in maintenance. "
64                     + "Contact: " + inventoryConfig.getEmail().toString() + "\" }")
65                     .build();
66      // end::email[]
67    }
68  }
69
70  @GET
71  @Produces(MediaType.APPLICATION_JSON)
72  public Response listContents() {
73    // tag::isInMaintenance[]
74    if (!inventoryConfig.isInMaintenance()) {
75    // end::isInMaintenance[]
76      return Response.ok(manager.list()).build();
77    } else {
78      // tag::email[]
79      return Response.status(Response.Status.SERVICE_UNAVAILABLE)
80                     .entity("{ \"error\" : \"Service is currently in maintenance. "
81                     + "Contact: " + inventoryConfig.getEmail().toString() + "\" }")
82                     .build();
83      // end::getEmail[]
84    }
85  }
86
87}
88
89// end::config-methods[]

To add configuration to the inventory service, the InventoryConfig object is injected to the existing class.

The port number from the configuration is retrieved by the inventoryConfig.getPortNumber() method and passed to the manager.get() method as a parameter.

To determine whether the inventory service is in maintenance or not (according to the configuration value), inventoryConfig.isInMaintenance() class method is used. If you set the io_openliberty_guides_inventory_inMaintenance property to true in the configuration, the inventory service returns the message, ERROR: Service is currently in maintenance, along with the contact email. The email configuration value can be obtained by calling inventoryConfig.getEmail() method.

CustomConfigSource.json

1{
2  "config_ordinal": 150,
3  "io_openliberty_guides_email": "[email protected]",
4  "io_openliberty_guides_inventory_inMaintenance": false,
5  "io_openliberty_guides_system_inMaintenance": false,
6  "io_openliberty_guides_testConfigOverwrite": "CustomSource"
7}

Running the application

You started the Open Liberty in dev mode at the beginning of the guide, so all the changes were automatically picked up.

While the Liberty is running, the following two microservices should be available to access:

You can find the service that retrieves configuration information that is specific to this guide at the following location:

The config_ordinal value of the custom configuration source is set to 150. It overrides configuration values of the default microprofile-config.properties source, which has a config_ordinal value of 100.

CustomConfigSource.json

1{
2  "config_ordinal": 150,
3  "io_openliberty_guides_email": "[email protected]",
4  "io_openliberty_guides_inventory_inMaintenance": false,
5  "io_openliberty_guides_system_inMaintenance": false,
6  "io_openliberty_guides_testConfigOverwrite": "CustomSource"
7}

microprofile-config.properties

 1# tag::all-props[]
 2# tag::ordinal[]
 3config_ordinal=100
 4# end::ordinal[]
 5# tag::inventory-port-number[]
 6io_openliberty_guides_port_number=9080
 7# end::inventory-port-number[]
 8# tag::inventory-in-maintenance[]
 9io_openliberty_guides_inventory_inMaintenance=false
10# end::inventory-in-maintenance[]
11# tag::system-in-maintenance[]
12io_openliberty_guides_system_inMaintenance=false
13# tag::system-in-maintenance[]
14# tag::test-config-overwrite[]
15io_openliberty_guides_testConfigOverwrite=DefaultSource
16# tag::test-config-overwrite[]
17# end::all-props[]

Play with this application by changing configuration values for each property in the resources/CustomConfigSource.json file. Your changes are added dynamically, and you do not need to restart the Liberty. Refresh http://localhost:9080/config to see the dynamic changes.

For example, change io_openliberty_guides_inventory_inMaintenance from false to true, then try to access http://localhost:9080/inventory/systems again. The following message displays: ERROR: Service is currently in maintenance.

Testing the application

Create the ConfigurationIT class.
src/test/java/it/io/openliberty/guides/config/ConfigurationIT.java

ConfigurationIT.java

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2017, 2024 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License 2.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-2.0/
  8 *
  9 * SPDX-License-Identifier: EPL-2.0
 10 *******************************************************************************/
 11// end::copyright[]
 12// tag::test[]
 13package it.io.openliberty.guides.config;
 14
 15import static org.junit.jupiter.api.Assertions.assertEquals;
 16
 17import jakarta.ws.rs.client.Client;
 18import jakarta.ws.rs.client.ClientBuilder;
 19import jakarta.ws.rs.core.Response;
 20
 21import org.junit.jupiter.api.AfterEach;
 22import org.junit.jupiter.api.BeforeEach;
 23import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
 24import org.junit.jupiter.api.Order;
 25import org.junit.jupiter.api.Test;
 26import org.junit.jupiter.api.TestMethodOrder;
 27
 28@TestMethodOrder(OrderAnnotation.class)
 29public class ConfigurationIT {
 30
 31  private String port;
 32  private String baseUrl;
 33  private Client client;
 34
 35  private final String INVENTORY_HOSTS = "inventory/systems";
 36  private final String USER_DIR = System.getProperty("user.dir");
 37  private final String DEFAULT_CONFIG_FILE = USER_DIR
 38      + "/src/main/resources/META-INF/microprofile-config.properties";
 39  private final String CUSTOM_CONFIG_FILE = USER_DIR.split("target")[0]
 40      + "/resources/CustomConfigSource.json";
 41  private final String INV_MAINTENANCE_PROP = "io_openliberty_guides"
 42      + "_inventory_inMaintenance";
 43
 44  @BeforeEach
 45  // tag::setup[]
 46  public void setup() {
 47    port = System.getProperty("http.port");
 48    baseUrl = "http://localhost:" + port + "/";
 49    ConfigITUtil.setDefaultJsonFile(CUSTOM_CONFIG_FILE);
 50
 51    client = ClientBuilder.newClient();
 52  }
 53  // end::setup[]
 54
 55  @AfterEach
 56  public void teardown() {
 57    ConfigITUtil.setDefaultJsonFile(CUSTOM_CONFIG_FILE);
 58    client.close();
 59  }
 60
 61  @Test
 62  @Order(1)
 63  // tag::testInitialServiceStatus[]
 64  public void testInitialServiceStatus() {
 65    boolean status = Boolean.valueOf(ConfigITUtil.readPropertyValueInFile(
 66        INV_MAINTENANCE_PROP, DEFAULT_CONFIG_FILE));
 67    if (!status) {
 68      Response response = ConfigITUtil.getResponse(client, baseUrl + INVENTORY_HOSTS);
 69
 70      int expected = Response.Status.OK.getStatusCode();
 71      int actual = response.getStatus();
 72      assertEquals(expected, actual);
 73    } else {
 74      assertEquals(
 75         "{ \"error\" : \"Service is currently in maintenance."
 76         + "Contact: [email protected]\" }",
 77          ConfigITUtil.getStringFromURL(client, baseUrl + INVENTORY_HOSTS),
 78          "The Inventory Service should be in maintenance");
 79    }
 80  }
 81  // end::testInitialServiceStatus[]
 82
 83  @Test
 84  @Order(2)
 85  // tag::testPutServiceInMaintenance[]
 86  public void testPutServiceInMaintenance() {
 87    Response response = ConfigITUtil.getResponse(client, baseUrl + INVENTORY_HOSTS);
 88
 89    int expected = Response.Status.OK.getStatusCode();
 90    int actual = response.getStatus();
 91    assertEquals(expected, actual);
 92
 93    ConfigITUtil.switchInventoryMaintenance(CUSTOM_CONFIG_FILE, true);
 94
 95    String error = ConfigITUtil.getStringFromURL(client, baseUrl + INVENTORY_HOSTS);
 96
 97    assertEquals(
 98         "{ \"error\" : \"Service is currently in maintenance. "
 99         + "Contact: [email protected]\" }",
100        error, "The inventory service should be down in the end");
101  }
102  // end::testPutServiceInMaintenance[]
103
104  @Test
105  @Order(3)
106  // tag::testChangeEmail[]
107  public void testChangeEmail() {
108    ConfigITUtil.switchInventoryMaintenance(CUSTOM_CONFIG_FILE, true);
109
110    String error = ConfigITUtil.getStringFromURL(client, baseUrl + INVENTORY_HOSTS);
111
112    assertEquals(
113         "{ \"error\" : \"Service is currently in maintenance. "
114         + "Contact: [email protected]\" }",
115        error, "The email should be [email protected] in the beginning");
116
117    ConfigITUtil.changeEmail(CUSTOM_CONFIG_FILE, "[email protected]");
118
119    error = ConfigITUtil.getStringFromURL(client, baseUrl + INVENTORY_HOSTS);
120
121    assertEquals(
122         "{ \"error\" : \"Service is currently in maintenance. "
123         + "Contact: [email protected]\" }",
124        error, "The email should be [email protected] in the beginning");
125  }
126  // end::testChangeEmail[]
127
128}
129// end::test[]

microprofile-config.properties

 1# tag::all-props[]
 2# tag::ordinal[]
 3config_ordinal=100
 4# end::ordinal[]
 5# tag::inventory-port-number[]
 6io_openliberty_guides_port_number=9080
 7# end::inventory-port-number[]
 8# tag::inventory-in-maintenance[]
 9io_openliberty_guides_inventory_inMaintenance=false
10# end::inventory-in-maintenance[]
11# tag::system-in-maintenance[]
12io_openliberty_guides_system_inMaintenance=false
13# tag::system-in-maintenance[]
14# tag::test-config-overwrite[]
15io_openliberty_guides_testConfigOverwrite=DefaultSource
16# tag::test-config-overwrite[]
17# end::all-props[]

The testInitialServiceStatus() test case reads the value of the io_openliberty_guides_inventory_inMaintenance configuration property in the META-INF/microprofile-config.properties file and checks the HTTP response of the inventory service. If the configuration value is false, the service returns a valid response. Otherwise, the service returns the following message: ERROR: Service is currently in maintenance.

Because the io_openliberty_guides_inventory_inMaintenance configuration property is set to false by default, the testPutServiceInMaintenance() test case first checks that the inventory service is not in maintenance in the beginning. Next, this test switches the value of the io_openliberty_guides_inventory_inMaintenance configuration property to true. In the end, the inventory service returns the following message: ERROR: Service is currently in maintenance.

The testChangeEmail() test case first puts the inventory service in maintenance, then it changes the email address in the configuration file. In the end, the inventory service should display the error message with the latest email address.

In addition, a few endpoint tests have been provided for you to test the basic functionality of the inventory and system services. If a test failure occurs, then you must have introduced a bug into the code. Remember that you must register the custom configuration source and custom converter in the src/main/resources/META-INF/services/ directory. If you don’t complete these steps, the tests will fail. These tests run automatically as a part of the integration test suite.

Running the tests

Because you started Open Liberty in dev mode, you can run the tests by pressing the enter/return key from the command-line session where you started dev mode.

You see the following output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.config.ConfigurationIT
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.92 s - in it.io.openliberty.guides.config.ConfigurationIT
Running it.io.openliberty.guides.system.SystemEndpointIT
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.017 s - in it.io.openliberty.guides.system.SystemEndpointIT
Running it.io.openliberty.guides.inventory.InventoryEndpointIT
[WARNING ] Interceptor for {http://client.inventory.guides.openliberty.io/}SystemClient has thrown exception, unwinding now
Could not send Message.
[err] The specified host is unknown.
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.077 s - in it.io.openliberty.guides.inventory.InventoryEndpointIT

Results:

Tests run: 7, Failures: 0, Errors: 0, Skipped: 0

The warning and error messages are expected and result from a request to a bad or an unknown hostname. This request is made in the testUnknownHost() test from the InventoryEndpointIT integration test.

To see whether the tests detect a failure, remove the configuration resetting line in the setup() method of the ConfigurationIT.java file. Then, manually change some configuration values in the resources/CustomConfigSource.json file. Rerun the tests. You will see a test failure occur.

When you are done checking out the service, exit dev mode by pressing CTRL+C in the command-line session where you ran Liberty.

Great work! You’re done!

You just built and tested a MicroProfile application with MicroProfile Config in Open Liberty.

Feel free to try one of the related guides. They demonstrate new technologies that you can learn and expand on top what you built in this guide.

Guide Attribution

Configuring microservices by Open Liberty is licensed under CC BY-ND 4.0

Copy file contents
Copied to clipboard

Prerequisites:

Nice work! Where to next?

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

What could make this guide better?

Raise an issue to share feedback

Create a pull request to contribute to this guide

Need help?

Ask a question on Stack Overflow

Like Open Liberty? Star our repo on GitHub.

Star