Spring Boot Microservices Deployment with Docker Compose

Spring Boot is a popular Java-based framework for developing web applications. In this blog post, we’ll discuss how to deploy Spring Boot with Docker Compose and the advantages of doing so.

The “spring boot microservices with docker-compose example github” is a blog post that explains how to deploy Spring Boot Microservices with Docker Compose.

In this article, you’ll learn how to use Docker compose to develop and deploy a multi-container Spring Boot application.

Overview

We learnt how to Dockerize a basic Spring Boot-based application in the last article. We managed each container separately using various docker commands since it was a tiny application. In a real-world situation, however, their single host service interacts with multiple dependent services in Docker containers while creating production-grade software. As a result, manually managing each container using Docker commands is almost impossible. Containers must be managed as if they were a single entity. Docker Compose comes to the rescue in this case.

Docker Compose is a highly powerful technology that enables you to deploy complicated architectural apps rapidly. It’s used to keep track of many containers that belong to the same application service. This technology is similar to Docker, except it enables you to deal with more complicated applications.

I’ll teach you how to host an application in a Docker container and manage it using docker-compose in this post.

Preparation:

  • JDK 1.8
  • Idea for Intellij IDE
  • Maven
  • SpringBoot is a kind of application that is used to start a computer
  • Plugin for Lombok
  • Docker Desktop is a program that allows you to run Dock 

I’ll host an application in a Docker container in this blog, which is made up of two interdependent containers:

  • A Spring Boot application container.
  • A container for PostgreSQL databases.

Finally, we’ll use a single docker-compose file to handle the interaction between two containers.

Creating a Basic Spring Boot Application is the first step.

To bootstrap your project, visit the Spring Initializr page. We’ll create the following sub-packages under our main package ‘com.appsdeveloperblogs.dockercomposetutorial’ after importing the project into IDE:

  • The class ‘ItemController’ with the filename ‘ItemController.java’ is included in this package.
  • The item entity POJO structure with the filename ‘Item.java’ is included in this package.
  • JpaRepository interface implementation for performing CRUD operations with Postgres DB is included in this package.
  • Service: The’service.java’ file in this package includes the service class. 

The ultimate structure of the project will be as follows:

project structure

We’ll go through each subject in depth in the parts that follow.

Dependencies in Maven

The whole pom.xml file will look like this:

2.5.5 com.appsdeveloperblog.DockerComposeTutorial 4.0.0 org.springframework.boot spring-boot-starter-parent Spring-boot-DockerCompose 0.0.1-SNAPSHOT Tutorial on SpringBoot Docker Compose SpringBoot Docker Compose Tutorial 11 org.springframework.boot org.springframework.boot org.springframework.boot org.springframework.boot org.springframework.boot org. org.springframework.boot org.springframework.boot org.springframework.boot org.springframework.boot org.springframework. org.postgresql.spring-boot-starter-data-jpa postgresql lombok 1.18.22 org.springframework.boot 42.2.18 org.projectlombok spring-boot-maven-plugin

Entity Type

The POJO structure of the Item object is contained in the Entity package. The following is the whole code for the Item.java file:

Import lombok from the package com.appsdeveloperblogs.dockercomposetutorial.entity. Import lombok into AllArgsConstructor. Import lombok as a getter. Import lombok; NoArgsConstructor Import javax.persistence as a setter. Import javax.persistence.GeneratedValue; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue; import @Entity @Getter @Setter @AllArgsConstructor @NoArgsConstructor public class Item @Id @GeneratedValue private int id; private String name; @Entity @Getter @Setter @AllArgsConstructor @NoArgsConstructor @AllArgsConstructor @AllArgsConstructor @AllArgsConstructor @AllArgsConstructor @AllArgsConstructor @AllArgsConstructor

 

Class of the Controller

We’ll use the @RestController annotation to annotate this class. As a result, it will handle web queries such as GET /items to get item lists from the Postgres database. The following is the whole code for the ItemController.java file:

Import java.util.List; import com.appsdeveloperblogs.dockercomposetutorial.service.ItemService; import org.springframework.beans.factory.annotation; package com.appsdeveloperblogs.dockercomposetutorial.controller. Import org.springframework.web.bind.annotation; autowired Import org.springframework.web.bind.annotation from GetMapping. @RestController public class; import com.appsdeveloperblogs.dockercomposetutorial.entity.Item; @Autowired private ItemService itemService; @GetMapping(“/items”) public List items() return itemService.findAll(); ItemController @Autowired private ItemService itemService; @GetMapping(“/items”) public List items() return itemService.findAll();

 

Class for the Repository

Because we’re utilizing PostgreSQL for data persistence, this class extends the JpaRepository interface. As a result, JpaRepository will give us with generic methods like save(), findAll(), insert(), and so on by default. The UserRepository.java file’s whole code is as follows:

import org.springframework.data.jpa.repository.JpaRepository; import com.appsdeveloperblogs.dockercomposetutorial.entity.Item; public interface package com.appsdeveloperblogs.dockercomposetutorial.entity.Item JpaRepositoryItem, Integer> extends ItemRepository.

 

Class of Service

The @Service annotation is applied to the class, and it is here that we add the business logic for storing, retrieving, and updating the item. At the start of the program, we’ll create three test entries in our Postgres databases. The following is the whole code for the ItemService.java file:

import java.util.List; import javax.annotation; package com.appsdeveloperblogs.dockercomposetutorial.service Import org.springframework.beans.factory.annotation in PostConstruct. Import org.springframework.stereotype.Service; import org.springframework.transaction.annotation. Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation. Import com.appsdeveloperblogs.dockercomposetutorial.entity to make it transactional. com.appsdeveloperblogs.dockercomposetutorial.repository is an item; import it. @Service public class ItemRepository @Transactional @PostConstruct public void ItemService @Autowired private ItemRepository itemRepository; @Transactional @PostConstruct public void itemRepository.save(new Item(1, “John”)); itemRepository.save(new Item(2, “Alex”)); itemRepository.save(new Item(3, “Sandra”)); itemRepository.save(new Item(4, “Alex”); itemRepository.save(new Item(5, “Alex”); itemRepository.save(new Item(6, “Alex”); itemRepository.save(new

Creating a Docker file is the second step.

To begin, we create a Dockerfile file at the root of our project structure with the information shown below. This Dockerfile tells Docker how to produce an application image in which format.

The following is the Dockerfile’s content:

ADD target/*.jar myapplication FROM openjdk:11 [“java”, “-jar”,”myapplication”] ENTRYPOINT EXPOSE 8080 EXPOSE 8080 EXPOSE 8080 E

Step 3: Install Docker Make a file

We will eventually generate the docker-compose.yml file.

An application is often made up of numerous components that are interdependent but may be operated independently on separate computers. For instance, in our current Spring Boot application, we offer a REST API with PostgreSQL CRUD operations. We have two containers here: one that assures the site’s functionality through REST API, and the other that is in charge of the database. For this usecase, docker-compose is ideal since it allows us to manage all services in a single file.

The following is the docker-compose.yml file:

‘3’ is the version number. myspringapp: myspringapp: myspringapp: myspringapp: myspringapp image: build: myapplication “8080:8080” is the port number. always depends on: re-start SPRING DATASOURCE URL=jdbc:postgresql:/mypostgres:5432/mydb in the mypostgres environment – SPRING JPA HIBERNATE DDL AUTO=create – SPRING DATASOURCE USERNAME=postgres – SPRING DATASOURCE PASSWORD=password mypostgres: picture credit: postgres always re-start “5432:5432” is the port number. POSTGRES USER=postgres – POSTGRES DB=mydb environment: – POSTGRES PASSWORD=password – POSTGRES USER=postgres – POSTGRES DB=mydb

 

Docker-compose.yml (docker-compose.yml)

Let’s take a closer look at the docker-compose file.

Version:

This tells Docker Compose the version of the file you wish to use when composing it. In our situation, we’ll utilize version 3, which is the most widely used at the moment.

Services:

Under the Services argument, the set of containers that need to be managed by docker-compose must be specified. For two containers in our application, we’ve established two services with the names myspringapp and mypostgres, respectively. Then, when needed, you must describe your container with many extra parameters.

  • image: It lets us provide the name of the Docker image we wish to utilize.
  • build: This indicates the path of our Dockerfile, with. denoting the directory in which the docker file is stored.
  • ports: This tells Docker Compose that we wish to expose a port from our host system to our container, making it accessible to the outside world.
  • Because containers are single-process by definition, if this one meets a catastrophic problem, it may be brought to a halt. In our situation, the restart: always option ensures that if the Postgres SQL server crashes, it will immediately restart.
  • depends on: This enables you to define the order in which containers should start and stop. The Postgres SQL container, for example, must be running before the Spring application can begin.
  • environment: The clause enables us to set up an environment variable in the container for the execution of individual containers.

The project is now live.

It’s time to launch the project now that it’s been constructed. When working on individual containers without docker-compose, this part of our work corresponds to when the command docker run is used.

The server exposed port 8080 to fulfill client requests, as specified in the docker file. As a result, navigating to http://localhost:8080/items in your browser will show the data obtained from the Postgres SQL database.

Let’s use the terminal to perform some commands.

First, we’ll use the normal maven command to create our project:

clean install mvn

The next step is to use the docker-compose command to construct a containerized image for our application.

build docker-compose

It should show the following output:

output docker compose build

Now we must execute all of our containers with a single command, as seen below.

up docker-compose

If docker already has dependent images locally, it will start right away; otherwise, it will fetch images for the docker hub first. Docker is getting Postgres images from the internet since docker-compose couldn’t locate them locally, as seen below. This is a one-time only activity.

docker compose up

If everything builds well, the output will show the following success message:

ation: [org.hibernate.engine.transaction.jta.platform.internal.org.hibernate.engine.transaction.jta.platform.internal.org.hibernate.engine .NoJtaPlatform] | 2021-11-13 11:56:30.070 INFO | myspringapp 1 1 —- [main] j.LocalContainerEntityManagerFactoryBean: j.LocalContainerEntityManagerFactoryBean: Initialized JPA EntityManagerFactory f or persistence unit ‘default’ | 2021-11-13 11:56:30.888 | myspringapp 1 —- [main] WARN 1 Spring.jpa.open-in-view is enabled by default in JpaBaseConfiguration$JpaWebConfiguration. As a result, database queries may be run while the view is being shown. To prevent this warning, explicitly specify spring.jpa.open-in-view. | 2021-11-13 11:56:31.462 INFO | myspringapp 1 1 —- [main] o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat started with context path ” on port(s): 8080 (http). | 2021-11-13 11:56:31.484 INFO | myspringapp 1 1 —- [primary] c.a.d. DemoApplication: DemoApplication started in 8.107 seconds (JVM running for 8.849)

Now that all of the containers are up and running, we’ll try using Postman or the browser to visit the RESTful API.

We’ll use the following command to go to the endpoint:

http://localhost:8080/items

It will show a page with a list of item records retrieved from the Postgres SQL DB, as intended.

 

1636939118_32_Spring-Boot-Microservices-Deployment-with-Docker-Compose

Containers are being stopped.

We may also use the following commands to halt all containers.

down docker-compose

 

Conclusion

We learnt how to use docker-compose to execute sophisticated apps on distributed systems with minimum management in this tutorial. In other words, you can simply construct, grow, heal, and operate any number of containers using simple docker-compose.yml instructions. I hope you found this tutorial useful.

Watch This Video-

Spring Boot is a popular Java framework for building web applications. It provides an easy way to create microservices that can be deployed with Docker Compose. Reference: spring boot docker-compose.

Frequently Asked Questions

How do I deploy spring boot Microservices with Docker?

A: Spring Boot makes it easy to deploy Microservices. To check out how, please visit the documentation link below and follow the instructions listed on that page.

How do I deploy multiple Microservices to Docker?

A: There are many ways to deploy multiple Microservices. Some of them include, but arent limited too; deploying the services in different containers with host networking between the container ports, using overlay networks such as Kubernetes or OpenShift, and running your own Docker host in order to run these services on it.

How Microservices are deploy in Docker?

A: A microservice is a software application that provides one or more business functions so that they can be independently developed and deployed on different systems. Microservices are typically designed to communicate with each other using lightweight protocols such as HTTP.

Related Tags

  • docker-compose scale spring boot
  • spring boot microservices with docker example
  • docker-compose microservices example
  • docker microservices architecture example
  • spring boot microservices containerization