SpringBoot项目如何进行打包部署

前言:

  SSM架构下只需要部署多台Tomcat服务器后通过Nginx负载均衡即可解决。而在SpringBoot中并不需要自己部署Tomcat,而是内部已经自带了。所以我们只需要将其打包后启动,然后交给Nginx进行负载均衡即可

  前期SSM架构做过的Tomcat集群:Tomcat集群部署流程流程  和 Nginx负载均衡策略:Nginx负载均衡配置、常用策略、场景及特点

  


 

那么这里我们要讲的就是SpringBoot项目如何进行打包部署:

  springboot的打包方式有很多种。有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的。这里主要介绍如何打成jar进行部署。

  不推荐用war,因为springboot适合前后端分离,打成jar进行部署更合适。

 

(一)首先需要在application.properties当中配置端口以及访问项目名

server.port=8080           : 访问端口8080

server.servlet.context-path=/demo  :访问项目名demo   例:http://localhost:8080/demo

#Tomcat
server.port=8080
server.servlet.context-path=/demo

 

(二)marven的pom.xml配置文件(完整版)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.imooc</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- mysql:上面有指定版本号,所以这里没指定 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.8</version>
        </dependency>
        <!-- c3p0连接池 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                    <mainClass>com.imooc.demo.DemoApplication</mainClass>
                </configuration>

            </plugin>
            <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <dependencies> <dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-filtering</artifactId> <version>1.3</version> </dependency> </dependencies> </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
pom.xml

注意最下面的build这块一定要配置否则打jar的时候会说找不 到主类

<build>
  <plugins>
    <plugin>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <mainClass>com.imooc.demo.DemoApplication</mainClass>
      </configuration>
    </plugin>
  </plugins>
<build>

 

(三)在启动类当中加上extends SpringBootServletInitializer并重写configure方法,这是为了打包springboot项目用的。

package com.imooc.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override//为了打包springboot项目
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}

 

(四)然后按照顺序运行mvn clean再mvn install,我是用idea执行的

 

 然后就会出来我们需要的jar

 

 

(五)然后到这个jar的根目录下执行java -jar demo-0.0.1-SNAPSHOT.jar

这个执行方式windows和linux上都一样

 

 


 

本章转自:SpringBoot项目如何进行打包部署

posted @ 2021-03-13 19:01  _kerry  阅读(3949)  评论(0编辑  收藏  举报