Deploying Spring Boot Applicationsbuild

Deploying Spring Boot Applications

部署springboot的应用程序

Spring Boot applications can be deployed into production systems with various methods. In this article, we will go through step by step deployment of Spring Boot applications via the following 5 methods:

spring boot应用程序可以通过各种方法部署到生产系统中。在本文中,我们将通过以下5种方法逐步部署SpringBoot应用程序:

  • Deploying in Java Archive (JAR) as a standalone application,
  • 作为一个独立的应用程序部署在Java Archive(JAR)中,
  • Deploying as Web Application Archive (WAR) into a servlet container,
  • 作为web应用程序存档(war)部署到一个servlet容器中,
  • Deploying in Docker Container,
  • 部署在Docker容器中,
  • Deploying behind NGINX web server — direct setup,
  • 在nginx web服务器后面部署-直接安装,
  • Deploying behind NGINX web server — containerized setup.
  • 在nginx web服务器后面部署-容器化设置。

Deploying in Java Archive (JAR) as a standalone application

作为一个独立的应用程序部署在Java Archive(JAR)中

Spring Boot applications can easily be packaged into JAR files and deployed as standalone applications. This is done by the spring-boot-maven-plugin. The plugin is automatically added to pom.xml once the Spring project is created via Spring Initializr as a Maven project.

spring boot应用程序可以很容易地打包成jar文件,并作为独立的应用程序部署。这是通过使用spring-boot-maven-plugin插件完成的。这个插件是在以Spring Initializr 创建的Spring Boot 的Maven项目时自动添加到pom.xml 中的。

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

In order to package the application in a single (fat) jar file, run the maven command mvn package under project directory. This will package the application inside an executable jar file with all its dependencies (including the embedded servlet container — if its a web application). To run the jar file, use the following standard JVM command java -jar <jar-file-name>.jar.

要将应用程序打包到单个(fat)jar文件中,需要在project目录下运行maven命令mvn package。将把应用程序打包到一个可执行的jar文件中,其中包含它的所有依赖项(如果它是一个web应用程序,则包括嵌入的servlet容器)。要运行jar文件,请使用以下标准jvm命令java-jar<jar file name>.jar。

Deploying as Web Application Archive (WAR) into a servlet container

作为web应用程序存档(war)部署到servlet容器中


Spring Boot applications can be packaged into WAR files to be deployed into existing servlet containers (such as Tomcat, Jetty etc.). This can be done as follows:

Spring启动应用程序可以打包成WAR文件,以部署到现有的servlet容器(如Tomcat、Jetty etc.)中。具体操作如下:


Specify WAR packaging in pom.xml file via <packaging>war</packaging>. This will package the application into a WAR file (instead of JAR). On the second step, set the scope of Tomcat (servlet container) dependency to provided (so that it is not deployed into WAR file):
通过<packaging>war</packaging>在pom.xml文件中指定war packaging。这将把应用程序打包成一个war文件(而不是jar)。在第二步中,将tomcat(servlet container)依赖项的范围设置为provided(以便它不会部署到war文件中):


<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId
  <scope>provided</scope>
</dependency>

Initialise the Servlet context required by Tomcat by extending SpringBootServletInitializer and overriding configure method as follows:

通过扩展springboot servlet initializer 并重写配置方法,初始化tomcat所需的servlet上下文,如下所示:

@SpringBootApplication
public class DemoApp extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(DemoApp.class);
  }
  public static void main(String[] args) {
    SpringApplication.run(DemoApp.class, args);
  }
}

in order to package the application in a war file, run the standard maven command mvn clean package under project directory. This will generate the WAR package which can be deployed into a servlet container. To run the application inside an existing Tomcat container, copy the generated WAR file to tomcat/webapps/ directory.

为了要将应用程序打包到war文件中,需要在项目目录下运行标准maven命令mvn clean package。这将生成可以部署到servlet容器中的war包。若要在现有的Tomcat容器中运行应用程序,请将生成的WAR文件复制到Tomcat/WebApps/Directory。

Deploying in Docker Container
Before deploying the application into a Docker container, we will first package the application in a (fat) JAR file. This process is previously explained, therefore I will assume we have a jar file.
On the first step, we need to build a container image. For this, we start with creating a Dockerfile in the project root directory as follows:

在Docker容器中部署
在将应用程序部署到Docker容器之前,我们首先将应用程序打包到(fat)jar文件中。前面已经解释过这个过程,因此

假设我们有一个jar文件。
在第一步,我们需要建立一个容器图像。为此,我们首先在项目根目录中创建dockerfile,如下所示: 

# latest oracle openjdk is the basis
FROM openjdk:oracle
# copy jar file into container image under app directory
COPY target/demoApp.jar app/demoApp.jar
# expose server port accept connections
EXPOSE 8080
# start application
CMD ["java", "-jar", "app/demoApp.jar"]

Note that, in the above snippet, we assumed that the application JAR file ‘demoApp.jar’ is located under the target directory of our project. We also assumed that the embedded servlet port is 8080 (which is the default case for Tomcat).

注意,在上面的代码片段中,我们假设应用程序jar文件'demoapp.jar'位于项目的目标目录下。我们还假设嵌入式servlet端口是8080(这是tomcat的默认情况)。


We can now build the Docker image with the following command (from where the Dockerfile is located):

我们现在可以使用以下命令(dockerfile所在的位置)构建docker映像:

docker image build -t demo-app:latest .


where -t is the name and tag of the image to be built. Once the image is built, we can create and run the container via:

-t是要构建的图像的名称和标记。一旦构建了图像,我们就可以通过以下方式创建和运行容器:

docker container run -p 8080:8080 -d --name app-container demo-app

 where -p publishes (maps) host port to container port (in this case both are 8080). The option -d (detach) runs the container in background, and --name specifies the name of the container.

-p发布(映射)主机端口到容器端口(在本例中两者都是8080)。选项-d(detach)在后台运行容器,并且--name指定容器的名称。

Deploying behind NGINX web server — direct setup

在nginx web服务器后面部署-直接安装
Configuring servlet containers (such as Tomcat or Jetty) for real production (i.e. running on port 80, without root user and with SSL) may not be straight forward (but doable). Therefore, it is recommended to use a web server (such as Nginx) in front of your Spring Boot applications. This can be done in two ways; direct setup or containerized setup. In this section, we will demonstrate the direct setup.

实际生产配置servlet容器(如tomcat或jetty)(即在端口80上运行、没有根用户和ssl)可能不是直接的(但可行)。因此,建议在spring boot应用程序前面使用web服务器(比如nginx)。这可以通过两种方式完成:直接安装或集装箱安装。在本节中,我们将演示直接设置。

In direct setup, we run the Nginx web server and the Spring Boot applications directly on localhost (on different ports of course). And we let Ngnix proxy REST requests to Spring Boot applications. For this:

在直接设置中,我们直接在本地主机(当然是在不同的端口)上运行nginx web服务器和spring引导应用程序。我们允许ngnix代理rest请求到spring启动应用程序。为此:

发布了88 篇原创文章 · 获赞 33 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/ccmedu/article/details/102633723