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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Automated Multi-Repo IBM App Connect Enterprise BAR Builds
  • What Is End-To-End Testing? E2E Testing Tutorial With Examples and Best Practices
  • How To Backup and Restore a PostgreSQL Database
  • Write a Smart Contract With ChatGPT, MetaMask, Infura, and Truffle
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Build and Run a Hello World Java Microservice

How to Build and Run a Hello World Java Microservice

Learn how to build a simple Java-based microservice and deploy it to various cloud platforms.

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
Jun. 18, 19 · Tutorial
Like (6)
Save
Tweet
Share
16.32K Views

Join the DZone community and get the full member experience.

Join For Free

The repo cloud-native-starter contains an end-to-end sample application that demonstrates how to develop your first cloud-native applications. Two of the microservices have been developed with Java EE and MicroProfile. To simplify the creation of new Java EE microservices, I've added another very simple service that can be used as template for new services.

Get the code.

The template contains the following functionality:

  • Image with OpenJ9, OpenJDK, Open Liberty and MicroProfile: Dockerfile
  • Maven project: pom.xml
  • Open Liberty server configuration: server.xml
  • Health endpoint: HealthEndpoint.java
  • Kubernetes yaml files: deployment.yaml and service.yaml
  • Sample REST GET endpoint: AuthorsApplication.java, GetAuthor.java and Author.java

If you want to use this code for your own microservice, remove the three Java files for the REST GET endpoint and rename the service in the pom.xml file and the YAML files.

The microservice can be run in different environments:

  • Docker
  • Minikube
  • IBM Cloud Kubernetes Service

In all cases, get the code first:

$ git clone https://github.com/nheidloff/cloud-native-starter.git
$ cd cloud-native-starter
$ ROOT_FOLDER=$(pwd)

Run Microservices in Docker

The microservice can be run in Docker Desktop.

$ cd ${ROOT_FOLDER}/authors-java-jee
$ mvn package
$ docker build -t authors .
$ docker run -i --rm -p 3000:3000 authors
$ open http://localhost:3000/openapi/ui/

Run Microservices in Minikube

These are the instructions to run the microservice in Minikube.

$ cd ${ROOT_FOLDER}/authors-java-jee
$ mvn package
$ eval $(minikube docker-env)
$ docker build -t authors:1 .
$ kubectl apply -f deployment/deployment.yaml
$ kubectl apply -f deployment/service.yaml
$ minikubeip=$(minikube ip)
$ nodeport=$(kubectl get svc authors --ignore-not-found --output 'jsonpath={.spec.ports[*].nodePort}')
$ open http://${minikubeip}:${nodeport}/openapi/ui/

Run Microservices in IBM Cloud Kubernetes Service

IBM provides the managed IBM Cloud Kubernetes Service. You can get a free IBM Cloud account. Check out the instructions on how to create a Kubernetes cluster.

Set your namespace and cluster name, for example:

$ REGISTRY_NAMESPACE=niklas-heidloff-cns
$ CLUSTER_NAME=niklas-heidloff-free

Build the image:

$ cd ${ROOT_FOLDER}/authors-java-jee
$ ibmcloud login -a cloud.ibm.com -r us-south -g default
$ ibmcloud ks cluster-config --cluster $CLUSTER_NAME
$ export ... // for example: export KUBECONFIG=/Users/$USER/.bluemix/plugins/container-service/clusters/niklas-heidloff-free/kube-config-hou02-niklas-heidloff-free.yml
$ mvn package
$ REGISTRY=$(ibmcloud cr info | awk '/Container Registry  /  {print $3}')
$ ibmcloud cr namespace-add $REGISTRY_NAMESPACE
$ ibmcloud cr build --tag $REGISTRY/$REGISTRY_NAMESPACE/authors:1 .

Deploy the microservice:

$ cd ${ROOT_FOLDER}/authors-java-jee/deployment
$ sed "s+<namespace>+$REGISTRY_NAMESPACE+g" deployment-template.yaml > deployment-template.yaml.1
$ sed "s+<ip:port>+$REGISTRY+g" deployment-template.yaml.1 > deployment-template.yaml.2
$ sed "s+<tag>+1+g" deployment-template.yaml.2 > deployment-iks.yaml
$ kubectl apply -f deployment-iks.yaml
$ kubectl apply -f service.yaml
$ clusterip=$(ibmcloud ks workers --cluster $CLUSTER_NAME | awk '/Ready/ {print $2;exit;}')
$ nodeport=$(kubectl get svc authors --output 'jsonpath={.spec.ports[*].nodePort}')
$ open http://${clusterip}:${nodeport}/openapi/ui/
$ curl -X GET "http://${clusterip}:${nodeport}/api/v1/getauthor?name=Niklas%20Heidloff" -H "accept: application/json"

Swagger UI for Microservices

Once deployed, Swagger UI can be opened which shows the APIs of the authors service:

microservice Java (programming language) Docker (software) IBM Cloud Build (game engine) Kubernetes Java EE Cloud application Web Protocols

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

Opinions expressed by DZone contributors are their own.

Trending

  • Automated Multi-Repo IBM App Connect Enterprise BAR Builds
  • What Is End-To-End Testing? E2E Testing Tutorial With Examples and Best Practices
  • How To Backup and Restore a PostgreSQL Database
  • Write a Smart Contract With ChatGPT, MetaMask, Infura, and Truffle

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

Let's be friends: