DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Configuring Microservices With MicroProfile and Kubernetes

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.

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
Apr. 12, 19 · Tutorial
Like (2)
Save
Tweet
Share
11.49K Views

Join the DZone community and get the full member experience.

Join For Free

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
metadata:
  name: articles
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: articles
        version: v1
    spec:
      containers:
      - name: articles
        image: articles:1
        ports:
        - containerPort: 8080
        env:
        - name: samplescreation
          valueFrom:
            configMapKeyRef:
              name: articles-config
              key: samplescreation
      restartPolicy: Always
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: articles-config
data:
  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";
  @Inject
  @ConfigProperty(name = "samplescreation", defaultValue = "dontcreate")
  private String samplescreation;
  @PostConstruct
  private void addArticles() {
    if (samplescreation.equalsIgnoreCase(CREATE_SAMPLES))
      addSampleArticles();
    }

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:

  • OpenLiberty guide: Configuring microservices
  • MicroProfile Config in Istio
  • MicroProfile Configuration Feature
  • How to externalize the configuration for flexibility and easier deployment
  • Configure your app using Eclipse MicroProfile Config
microservice Kubernetes

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT: The Unexpected API Test Automation Help
  • Express Hibernate Queries as Type-Safe Java Streams
  • How to Secure Your CI/CD Pipeline
  • A Brief Overview of the Spring Cloud Framework

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: