Home  >  Article  >  Java  >  Use Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications

Use Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications

王林
王林Original
2023-10-20 15:16:42642browse

使用Docker和Spring Boot实现应用的快速部署和水平扩展

Use Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications

With the development of cloud computing and container technology, more and more enterprises are beginning to adopt Docker To implement application deployment and management. Docker is characterized by being lightweight, highly portable, and capable of rapid deployment and expansion, while Spring Boot is a framework for building Java applications and provides a way to simplify development. This article will introduce how to combine Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications, and provide specific code examples.

1. The concept and use of Docker

Docker is a container technology that can package applications and their dependent environments into a container to achieve rapid deployment and transplantation of applications in different environments. and share.

  1. Install Docker

First of all, we need to install Docker. You can go to the Docker official website to download the corresponding installation package and install it according to the official documentation.

  1. Create a Docker image

Docker image is the basis of the Docker container and the packaging form of the application. We can use Dockerfile to define the image building process. The following is a simple Dockerfile example:

FROM openjdk:11
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

This example uses the officially provided OpenJDK 11 as the base image, copies the application's jar package into the container, and specifies the startup command through the ENTRYPOINT instruction.

  1. Build the Docker image

In the directory where the Dockerfile is located, execute the following command to build the Docker image:

docker build -t my-app .

Among them, my- app is the name of the image and can be modified according to the actual situation.

  1. Run the Docker container

After building the image, we can use the following command to run the Docker container:

docker run -d -p 8080:8080 my-app

Among them, -d# The ## parameter indicates running the container in background mode, the -p parameter indicates mapping the host's 8080 port to the container's 8080 port, and my-app is the name of the image.

2. Rapid deployment of Spring Boot applications

Spring Boot is a rapid development framework that can be used to quickly build independent, Spring-based applications.

    Create a Spring Boot application
First, we need to create a Spring Boot application. You can use Spring Initializr (https://start.spring.io/) to generate a basic Spring Boot project.

    Writing application logic
In the generated project, we can write our own business logic. Here is a simple RESTful interface as an example. The code is as follows:

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, Docker!";
    }

}

    Package into an executable jar package
In the root directory of the project, execute the following command to Packaged into an executable jar package:

./mvnw clean package

Among them,

./mvnw is the packaging script used to execute Maven commands, clean package is the Maven command, use For cleaning, compiling and packaging projects.

    Build the Docker image
In the root directory of the project, create a file named

Dockerfile and copy the following content into it:

FROM openjdk:11
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

    Build Docker image
In the root directory of the project, execute the following command to build the Docker image:

docker build -t my-app .

Among them,

my- app is the name of the image and can be modified according to the actual situation.

    Run the Docker container
After building the image, we can use the following command to run the Docker container:

docker run -d -p 8080:8080 my-app

Among them,

-d# The ## parameter indicates running the container in background mode, the -p parameter indicates mapping the host's 8080 port to the container's 8080 port, and my-app is the name of the image. By accessing

http://localhost:8080/hello

, we can see that the returned content is Hello, Docker!, indicating that the Spring Boot application has been successfully deployed . 3. Horizontal expansion of applications

Another advantage of Docker is that it can easily expand horizontally applications to meet high concurrency requirements.

Using Docker Compose
  1. Docker Compose is a tool for defining and running multiple Docker containers. We can use Docker Compose to manage multiple instances of an application.

First, we need to create a file named

docker-compose.yml

and copy the following content into it: <pre class='brush:yaml;toolbar:false;'>version: '3' services: app: image: my-app ports: - &quot;8080:8080&quot; environment: - SPRING_PROFILES_ACTIVE=dev1 app2: image: my-app ports: - &quot;8081:8080&quot; environment: - SPRING_PROFILES_ACTIVE=dev2</pre>The above configuration file defines two services ,

app

and app2 respectively correspond to two application instances. We can specify the application's configuration environment by setting the SPRING_PROFILES_ACTIVE environment variable.

Start the application instance
  1. In the directory where the configuration file is located, execute the following command to start the application instance:
docker-compose up -d

Among them,

- The d

parameter indicates running the container in background mode. By accessing

http://localhost:8080/hello

and http://localhost:8081/hello, we can see that the returned content is still Hello, Docker!, indicating that the two application instances have been started successfully. <p>By using Docker and Spring Boot, we can achieve rapid deployment and horizontal expansion of applications. Through Docker's containerization technology, we can package the application and its dependent environment into a container to achieve cross-platform deployment and transplantation. Using Spring Boot, we can quickly build the skeleton of the application and simplify development. I hope this article will help you understand and use Docker and Spring Boot. </p>

The above is the detailed content of Use Docker and Spring Boot to achieve rapid deployment and horizontal expansion of applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn