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.
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.
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.
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.
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.
@RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello, Docker!"; } }
./mvnw clean packageAmong them,
./mvnw is the packaging script used to execute Maven commands,
clean package is the Maven command, use For cleaning, compiling and packaging projects.
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"]
docker build -t my-app .Among them,
my- app is the name of the image and can be modified according to the actual situation.
docker run -d -p 8080:8080 my-appAmong 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
, 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 ComposeFirst, 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:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=dev1
app2:
image: my-app
ports:
- "8081:8080"
environment:
- SPRING_PROFILES_ACTIVE=dev2</pre>
The above configuration file defines two services ,
and app2
respectively correspond to two application instances. We can specify the application's configuration environment by setting the SPRING_PROFILES_ACTIVE
environment variable.
docker-compose up -d
Among them,
- The d parameter indicates running the container in background mode. By accessing
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!