Configuring Microservices With MicroProfile and Kubernetes

DZone 's Guide to

Configuring Microservices With MicroProfile and Kubernetes

Read on to take a look at the YAML, Java, and terminal commands you need to get your microservices up and running with MicroProfile and Kubernetes.

· Microservices Zone ·
Free Resource

As defined in the Twelve-Factor-App, it's important for cloud-native applications to store configuration externally, rather than in the code since this makes it possible to deploy applications to different environments.

An app's config is everything that is likely to vary between deploys (staging, production, developer environments, etc). This includes: Resource handles to ... backing services. Credentials to external services ...

Microservices that are implemented with Java EE can leverage MicroProfile Config. The configuration can be done, for example, in Kubernetes YAML files and accessed from Java code via annotations and APIs.

Here is a simple example from the cloud-native-starter repo. The 'articles' service uses configuration to define whether or not to create ten articles the first time it is invoked. In the YAML file, an environment variable pointing to a ConfigMap is defined.

kind: Deployment
apiVersion: apps/v1beta1
  name: articles
  replicas: 1
        app: articles
        version: v1
      - name: articles
        image: articles:1
        - containerPort: 8080
        - name: samplescreation
              name: articles-config
              key: samplescreation
      restartPolicy: Always
kind: ConfigMap
apiVersion: v1
  name: articles-config
  samplescreation: CREATE

In the Java code, the configuration can be accessed via @Inject and @ConfigProperty.

public class CoreService {
  private static final String CREATE_SAMPLES = "CREATE";
  @ConfigProperty(name = "samplescreation", defaultValue = "dontcreate")
  private String samplescreation;
  private void addArticles() {
    if (samplescreation.equalsIgnoreCase(CREATE_SAMPLES))

Note that you cannot access the injected variable in the constructor. Instead use the @PostConstruct annotation. Thanks to Emily Jiang for figuring this out.

If you want to try this feature and many other MicroProfile and Istio features, get the code from the cloud-native-starter repo and run these commands.

$ git clone https://github.com/nheidloff/cloud-native-starter.git
$ scripts/check-prerequisites.sh
$ scripts/deploy-articles-java-jee.sh
$ scripts/show-urls.sh

To learn more about MicroProfile Config, check out these resources:

kubernetes, microprofile tutorial, microservices, microservices tutorial java

Published at DZone with permission of Niklas Heidloff , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}