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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Manage Microservices With Docker Compose
  • Keep Your Application Secrets Secret
  • 3 Reasons for the Mounting Demand for Smart Cloud-Native Application Development
  • Auto-Scaling a Spring Boot Native App With Nomad

Trending

  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  • Why DDoS Protection Is an Architectural Decision for Developers
  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.

By 
Niklas Heidloff user avatar
Niklas Heidloff
·
Jun. 18, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
17.4K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Manage Microservices With Docker Compose
  • Keep Your Application Secrets Secret
  • 3 Reasons for the Mounting Demand for Smart Cloud-Native Application Development
  • Auto-Scaling a Spring Boot Native App With Nomad

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook