SpringBoot应用部署于内置Tomcat容器的配置

Spring boot默认web程序启用tomcat内嵌容器,监听8080端口,下面这篇文章主要给大家介绍了关于Spring Boot如何优化内嵌Tomcat的相关配置。

添加内置tomcat依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

配置Jar打包方式

由于使用内置tomcat,所以需要将源码打包成jar格式进行发布。配置参考示例如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.school.edu</groupId>
    <artifactId>edu</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>edu-admin</artifactId>
  <packaging>jar</packaging>
  <name>edu-admin Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
        <dependency>
            <groupId>com.school.edu</groupId>
            <artifactId>edu-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>edu-admin</finalName>
        <resources>
                <resource>
                    <directory>${basedir}/src/main/webapp</directory>
                    <!--注意此次必须要放在此目录下才能被访问到-->
                    <targetPath>META-INF/resources</targetPath> 
                    <includes>
                        <include>**/**</include>
                    </includes>
                </resource>
                <resource>
                    <directory>${basedir}/src/main/resources</directory>
                    <includes>
                        <include>**/**</include>
                    </includes>
                </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.2.RELEASE</version>
                <configuration>
                    <!-- 指定应用启动类 -->
                    <mainClass>com.school.edu.admin.application.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

内置tomcat的代码发布

当使用内置tomcat时,发布程序代码就变得非常简单了,我们只需要执行如下命令即可:

nohup java -jar + *.jar &

切记,这里最后一个符号&非常关键,不可缺少。缺少会导致linux发布代码后,执行ctrl+c退出命令会一并关闭springboot应用程序。

评论

*
*