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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes
  • [CSF] Using Metrics In Spring Boot Services With Prometheus, Graphana, Instana, and Google cAdvisor
  • Why Camel K?
  • Advanced Functional Testing in Spring Boot Using Docker in Tests

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Deploying Multiple Spring Boot Microservices to Docker

Deploying Multiple Spring Boot Microservices to Docker

As the name suggests employee-producer will be exposing REST APIs which will be consumed by the employee-consumer.

By 
Rida Shaikh user avatar
Rida Shaikh
·
Jun. 09, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.8K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous docker tutorial we saw how to deploy a single Spring Boot Microservice to Docker Container. But suppose the requirement is such that we may need to deploy multiple microservices to Docker Container. For example we have the following microservices that we have defined in previous tutorial.

As the name suggests employee-producer will be exposing REST APIs which will be consumed by the employee-consumer.

The way Docker has been designed such that a Docker Container should have only a single service running. Again we can have multiple services running in docker using some workarounds but this will not be a good design. So we will be deploying the two microservices employee-producer and employee-consumer to two different containers and then have them interact with each other.

In order to achieve this will have to make use of the docker networking commands.

This tutorial is explained in the below Youtube Video.


Lets Begin!

Lets begin with the implementation part. Import the two projects in eclipse.

  • Employee Producer
Java
 




xxxxxxxxxx
1


 
1
From openjdk:8
2
copy ./target/employee-producer-0.0.1-SNAPSHOT.jar employee-producer-0.0.1-SNAPSHOT.jar
3
CMD ["java","-jar","employee-producer-0.0.1-SNAPSHOT.jar"]



Open the terminal and start the docker

systemctl start docker


Now open the terminal and go to the Spring Boot employee-producer project folder.
 Next we will build an image with the name producer.

docker image build -t employee-producer .

Next we will run the above image as a container named producer. Also we will be publishing the docker port 8080 to centos port 8080.

docker container run --name producer -p 8080:8080 -d employee-producer

So our employee container has started. We can test this by going to localhost:8080/employee, we will see that our application is deployed successfully.

  • Employee Consumer
    • We have created and started a container named producer where we have deployed the employee-producer service.
      So the only change we will be making is while consuming the employee producer service we will be using this container named producer instead of localhost:8080.
      So in the ConsumerControllerClient class we will be having the base url as http://producer:8080/employee instead of http://localhost:8080/employee.
Java
 




xxxxxxxxxx
1
31


 
1
package com.javainuse.controllers;
2
import java.io.IOException;
3
import org.springframework.http.HttpEntity;
4
import org.springframework.http.HttpHeaders;
5
import org.springframework.http.HttpMethod;
6
import org.springframework.http.MediaType;
7
import org.springframework.http.ResponseEntity;
8
import org.springframework.web.client.RestClientException;
9
import org.springframework.web.client.RestTemplate;
10
public class ConsumerControllerClient {
11
    public void getEmployee() throws RestClientException, IOException {
12

          
13
        String baseUrl = "http://producer:8080/employee";
14
        RestTemplate restTemplate = new RestTemplate();
15
        ResponseEntity<String> response=null;
16
        try{
17
        response=restTemplate.exchange(baseUrl,
18
                HttpMethod.GET, getHeaders(),String.class);
19
        }catch (Exception ex)
20
        {
21
            System.out.println(ex);
22
        }
23
        System.out.println(response.getBody());
24
    }
25

          
26
    private static HttpEntity<?> getHeaders() throws IOException {
27
        HttpHeaders headers = new HttpHeaders();
28
        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
29
        return new HttpEntity<>(headers);
30
    }
31
}



  • The docker file is as follows:
Java
 




xxxxxxxxxx
1


 
1
From openjdk:8
2
copy ./target/employee-consumer-0.0.1-SNAPSHOT.jar employee-consumer-0.0.1-SNAPSHOT.jar
3
CMD ["java","-jar","employee-consumer-0.0.1-SNAPSHOT.jar"]



  • Open the terminal and go to the Spring Boot employee consumer project folder.
     Next we will build an image with the name consumer.

docker image build -t employee-consumer.

  • Next we will run the above image as a container named consumer.

docker image build -t employee-consumer.

  • Next check the logs using

docker container logs consumer

  • Here we can see that the container named consumer is not able to communicate with the container named producer.
  • So we are getting a null pointer exception.

Inter Docker Container Communication Using Docker Networking

We will be using Docker Networking to allow multiple containers to interact with each other.

We will need to create our own network and add both the employee-producer and employee-consumer services to it. We will stop and remove the existing docker containers named consumer and producer.

Lets first check the available networks

docker network ls

Next we will create our own network will be of type bridge

docker network create consumer-producer

Lets start the employee producer container on the newly created network.

docker container run --network consumer-producer --name producer -p 8080:8080 

-d employee-producer 

Lets start the employee consumer+ container on the newly created network.

docker container run --network consumer-producer --name consumer 

-d employee-consumer 

Lets check the consumer container logs-

docker container logs consumer

code snippet
Docker (software) Spring Framework Spring Boot microservice

Opinions expressed by DZone contributors are their own.

Related

  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes
  • [CSF] Using Metrics In Spring Boot Services With Prometheus, Graphana, Instana, and Google cAdvisor
  • Why Camel K?
  • Advanced Functional Testing in Spring Boot Using Docker in Tests

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!