SpringBoot多项目多端口war包部署

0.运行环境

  • JDK8.0
  • Tomcat9.0.55(最好与springboot内置版本一致,避免不必要的冲突)

1.打包springboot项目(maven方式)

  1. 修改pom文件根节点下打包方式为war
<packaging>war</packaging>
  1. 修改打包项目文件名build节点下新增<finalName>demo</finalName>
    <build>
        <finalName>demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  1. 修改项目依赖排除打包时自带的tomcat环境

方案一:添加依赖指定依赖范围

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

方案二:


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 移除嵌入式tomcat插件 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>


<dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-servlet-api</artifactId>
     <!-- 版本与tomcat版本对应 -->
   <version>9.0.50</version>
</dependency>

  1. 修改启动类,并重写初始化方法

启动类上继承SpringBootServletInitializer类,重写configure方法返回值,为启动类return builder.sources(DaimengApplication.class);

public class DaimengApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DaimengApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DaimengApplication.class);
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}
  1. 执行打包命令mvn clean package生成项目war包

2.部署项目

  1. 新建文件夹demo1、demo2将项目war包分别放入不同文件夹。

image-20211202133331105

  1. 修改tomcat目录下conf文件夹内server.xml文件,在根节点下加入新的service
<!--项目1 -->
<Service name="demo1">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Engine name="demo1" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
    
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase=""
            unpackWARs="true" autoDeploy="true">
            
        <Context path="" docBase="/home/webapps/demo1/demo.war" debug="0" privileged="true" />
 
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
</Service>

<!--项目2 -->
<Service name="demo2">
    <Connector port="9000" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="9001" />
    <Engine name="demo2" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
    
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase=""
            unpackWARs="true" autoDeploy="true">
            
        <Context path="" docBase="/home/webapps/demo2/demo.war" debug="0" privileged="true" />
 
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
</Service>

注:配置时注意文件读取权限,宝塔Linux面板建议在tomcat根目录下新建项目文件夹。

Q.E.D.


这个人很懒,什么都没有写