1.修改pom.xml
在pom.xml中增加
<packaging>war</packaging>
pom.xml 中的 标签下增加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
2.增加初始化类
在 SpringBootApplication 同级目录下增加类,继承 SpringBootServletInitializer 抽象类,重写 configure() 方法。
其中 SyslogApplication 类 为构建springboot 项目时根据项目名称生成的项目启动类。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
* @description: 部署到外部服务器
* @author YCH
* @date 2021-05-19
* @version 1.0
*/
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SyslogApplication.class);
}
}
3.打包部署运行
a. 使用命令 mvn clean package -Dmaven.test.skip=true
或者在ide工具中使用 maven lifecycle package 进行打包,war包生成目录默认是在项目目录下的target下
b. 将war 直接放在本地Tomcat的 webapps 文件夹下即可,启动Tomcat,会自动解压war包
c. 项目中的端口号配置不再生效,使用Tomcat进行端口配置,或统一端口使用不同访问路径,访问不同的项目。
配置不同端口号:
将不同项目放在不同文件夹下,这里新建了webapps1,webapps2,webapps3,三个文件夹,在server.xml 中进行配置即可。
<Server port="80" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!--
第一个项目,webapps,默认端口为8080
-->
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
<!-- 第二个项目,webapps1,端口号8081
再修改redirectPort:8444、
appBase="webapps1"
-->
<Service name="Catalina">
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8444" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps1" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
<!-- 第三个项目,webapps2,端口号8082
再修改redirectPort:8445、
appBase="webapps2"
-->
<Service name="Catalina">
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8445" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps2" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
同一端口号配置不同访问路径
在server.xml 中的 标签内增加 配置,path为项目启动后访问路径(即 IP:port/配置的path/uri)
<!--path 为访问项目地址;docBase 为项目路径(可以为相对路径或绝对路径)-->
<!--图片服务器-->
<Context path="wechat_njk_file/" docBase="E:\DeveTools\tomcat\apache-tomcat-8.5.64\webapps\wechat_njk_file" reloadable="true" />
<!--阿里云视频服务器-->
<Context path="alivideo/" docBase="E:\DeveTools\tomcat\apache-tomcat-8.5.64\webapps\alivideo" reloadable="true" />