手摸手教你极速部署War包(内含一Tomcat部署多个War包)

一. 准备工作



确保电脑已安装JDK及环境配置 官网JDK下载地址

下载JDK

下载Tomcat部署War包, Tomcat下载地址 , 推荐下载zip, 解压即可

下载Tomcat

注意JDK版本和Tomcat版本需要对应,参考下图, 看右边两列

在这里插入图片描述

二. 将项目打包成War包



1. 指定打包类型为War包

<packaging>war</packaging>

2. 排除SpringBoot内嵌的Tomcat

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

3. 跳过单元测试

<properties>
    <java.version>11</java.version>
    <skipTests>true</skipTests>
</properties>

4. 启动类小修改

继承 SpringBootServletInitializer 并重写 SpringApplicationBuilder

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

最后点击Maven工具的package进行打包
打包成功的War包会出现在项目的target目录下


5. 拷贝War包到Tomcat

将打包好的war包复制到tomcat下的webapps目录下,
将ROOT文件夹删掉, 之后将复制进来的war包重命名问ROOT.war

拷贝War包到Tomcat


三. 修改配置文件及部署


修改配置文件里的端口号为你项目端口号和修改项目war包的绝对路径
配置文件里面有两份配置,方便小伙伴们要在一个Tomcat部署两个War包
server.xml 文件里面不要出现中文!!! 就算是在注释里面有中文也不行!!!

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" 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>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->

<!-- The First -->
  <Service name="Catalina">

    <Connector port="8101" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    
    <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 &quot;%r&quot; %s %b" />
		<!-- <Context path="" docBase="F:\apache-tomcat-9.0.85\webapps\springboot-xxxx" reloadable="true"></Context> -->
		<!-- <Context path="" docBase="F:\apache-tomcat-9.0.85\webapps\xxxx" reloadable="true"></Context> -->
      </Host>
    </Engine>
  </Service>

<!-- The Second -->
  <!-- <Service name="Catalina1">
  
    <Connector port="9988" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    
    <Engine name="Catalina1" 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 &quot;%r&quot; %s %b" />
  		<Context path="" docBase="F:\apache-tomcat-9.0.85\webapps\springboot-xxxx" reloadable="true"></Context>
  		<Context path="" docBase="F:\apache-tomcat-9.0.85\webapps\xxxx" reloadable="true"></Context>
      </Host>
    </Engine>
  </Service> -->
</Server>


1. 修改监听端口号( server.xml )

如果war包的访问端口不是默认的8080, 则需要到server.xml文件里面更改监听端口
找到这个Connector之后, 修改对应的port端口号为自己的项目端口号即可

修改监听端口号


2. 修改项目绝对路径

建议直接复制war包所在路径

修改项目绝对路径


3. 修改部署War包输出日志乱码问题

找到conf目录下的logging.properties来修改对应编码


修改部署War包输出日志乱码问题


将UTF-8修改成GBK即可

修改部署War包输出日志乱码问题


4. Tomcat 启动!!!

进入bin目录, 双击startup.bat 即可


四. 一个Tomcat部署多个War包



1. 新增一个Service-节点

上面的server.xml文件里已经有两个Service节点了,将注释放开即可


2. 修改新增的Service节点

同理 复制我的server.xml文件 只需要修改端口号和项目绝对地址即可
点我跳转到 server.xml 文件


3. 创建对应的webapps

创建对应的webapps1, 一个文件夹里面放置一个项目的war包即可

一个Tomcat部署多个War包

4. Tomcat 启动!!!

进入bin目录, 双击startup.bat 即可

  • 24
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Tomcat可以通过两种方式来部署多个war。一种方式是通过在Tomcat的webapps目录下放置多个war来实现。每个war将会被Tomcat解压并部署为一个独立的Web应用程序。这种方式简单方便,适用于少量的Web应用程序。 另一种方式是修改Tomcat的配置文件server.xml,定义多个Connector。每个Connector都可以配置一个独立的端口,并将其映射到一个特定的war。这样,不同的war可以通过不同的端口访问。这种方式适用于需要更灵活的部署和管理多个Web应用程序的场景。 无论是哪种方式,一旦部署完成,Tomcat会自动解压和部署war,并将其转换为运行中的Web应用程序,可以通过相应的URL访问。这样,您就可以在同一个Tomcat服务器上同时运行多个独立的Web应用程序了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Tomcat部署多个war的方法步骤](https://download.csdn.net/download/weixin_38534444/12898843)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [多war部署在一个tomcat中](https://blog.csdn.net/xiaokanfuchen86/article/details/111184846)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值