• spring boot 打包war后 部署到外部 tomcat 的具体正确操作【包括修改端口 与 去除请求路径的工程名】


    1.前言

    工程做好了,总不能放在idea运行吧?不然怎么把项目放到云服务器呢?【这一篇随笔不讲解发布的云服务器的操作,在其他随笔有详细记载。】

      解决的方案是把springboot 工程 打包成war文件 ,然后部署到外部tomcat服务器的webapps文件夹里面,

    然后修改配置文件service.xml 用于修改访问端口与去除请求路径的工程名。

    【一般用一个tomcat可以设置多个节点,也就是说一个tomcat服务器可以同时开启多个工程,地址一样,用端口来区分,

    但是

    Tomcat 默认配置的最大请求数是 150,也就是说同时支持 150 个并发,当然了,也可以将其改大。
    当某个应用拥有 250 个以上并发的时候,应考虑应用服务器的集群。
    具体能承载多少并发,需要看硬件的配置,CPU 越多性能越高,分配给 JVM 的内存越多性能也就越高,但也会加重 GC 的负担。
    操作系统对于进程中的线程数有一定的限制:
    Windows 每个进程中的线程数不允许超过 2000
    Linux 每个进程中的线程数不允许超过 1000
    另外,在 Java 中每开启一个线程需要耗用 1MB 的 JVM 内存空间用于作为线程栈之用。
    Tomcat的最大并发数是可以配置的,实际运用中,最大并发数与硬件性能和CPU数量都有很大关系的。

    所以一般一个工程最少使用一个tomcat ,如果并发量超级大,会使用tomcat集群,即多个tomcat运行同一个工程。

    这里只用一个tomcat演示

     2.操作

     (1)打包war

    修改pom文件

     添加打包方式

     添加依赖包

     设置war包名称 ,如果不写 ,会默认 为 【工程名】-【版本号】.war

     

    我这里使用了依赖包管理标签,依赖包与我不一样没关系【这个工程是我以前学习spring cloud时候建立的,所以不要纠结其他依赖包为什么不同,没影响的】

    pom源码

    <?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>cen.cloud</groupId>-->
        <!--        <artifactId>cen-mycloud</artifactId>-->
        <!--        <version>0.0.1-SNAPSHOT</version>-->
        <!--        <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
        <!--    </parent>-->
        <groupId>com.example</groupId>
        <artifactId>provider-8001</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>provider-8001</name>
        <description>Demo project for Spring Boot</description>
    
        <!--    //打包方式-->
        <packaging>war</packaging>
    
        <dependencyManagement>
            <!--进行项目依赖版本通过一管理-->
            <dependencies>
                <!-- 方法二 : spring boot 的版本控制 ,与<parent>标签作用等同-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.1.6.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--  spring cloud 的版本控制 -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.SR2</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <!--        spring boot web 组件-->
            <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>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <!--eureka 注册中心依赖包 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <!-- 修改后立即生效,热部署 -->
            <!-- 热修改后端-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.4.RELEASE</version>
            </dependency>
            <!-- 热修改前端-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <!--            <optional>true</optional>-->
            </dependency>
    
            <!-- 打war包时加入此项, 告诉spring-boot tomcat相关jar包用外部的,不要打进去 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
    
    
        </dependencies>
    
        <build>
            <!-- 打包出的war包名字-->
            <finalName>provider-8001</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    
            </plugins>
        </build>
    
    </project>
    View Code

     (2)修改启动类,tomcat无法使用spring boot的启动类,需要继承

    SpringBootServletInitializer 

    后重写父类方法

    启动类源码

    package com.example.provider8001;
    
    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;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    //@SpringBootApplication
    ////开启发现服务
    //@EnableEurekaClient
    //public class Provider8001Application {
    //
    //    public static void main(String[] args) {
    //        SpringApplication.run(Provider8001Application.class, args);
    //    }
    //
    //}
    
    /**
     * spring boot 打包成war 用于外部tomcat运行才使用下面的启动方式
     */
    @SpringBootApplication
    //开启发现服务
    @EnableEurekaClient
    //启动类继承SpringBootServletInitializer实现configure ,用于配合war打包在外部tomcat使用
    public class Provider8001Application extends SpringBootServletInitializer {
    
        public static void main(String[] args) {
            SpringApplication.run(Provider8001Application.class, args);
        }
    
        //配合war打包
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(Provider8001Application.class);
        }
    
    
    }
    View Code

    (3)idea软件打包成war 很简单的

    菜单栏,找到 Build -Build Artifacts 

    点击会弹出 选择栏,根据数字顺序操作即可

    打包的过程可能会遇到一些黄色的感叹号警告,这是因为jdk版本的不一致或版本太老的关系,不影响war使用,但是对强迫症很不友好

     可以去除

    【我使用jdk1.8 ,因此设置都需要选中数字1.8或8】

    下面是去除的方法

    如图,打开project structure 

     

     

     然后打开setting 

     

    然后再试一下,好了没有警告了

    (4)好了war打包好了,那么在哪里呢?

    默认再target包里面

     可以直接复制出来,也可以进入工程文件夹查找

    (5)需要提前准备一个tomcat 【我是用版本9.0.12】

    找到tomcat的标签 <Context >只有在

    webapps文件夹 将war包直接粘贴进入即可

     是不是很疑惑,那个与war包一样名字的文件夹是什么回事 ,其实文件夹是在tomcat启动的时候自动创建的然后自动将war解压到里面

    (6)配置信息

    找到server.xml文件 打开

    每一个<server >标签都是一个节点 ,一个节点可以设置一个工程

     ,但是如果设置多个节点 ,该标签可以设置改成<Service name="Catalina"> ,这样就可以不用单独留一个端口来关闭该节点 ,给本地省接口

    不加关闭端口,一个节点还需要3个端口号 ,没必要浪费

    具体多节点配置可参考这篇博文 : https://blog.csdn.net/yin__ren/article/details/93198351

    配置访问工程端口号

    一般只有改port 即可 ,重定向端口根据需要可以改动,但是一个阶段得重定向接口需要统一

    【注意,端口多了得注意管理,不能端口冲突,如果一台主机装了多个tomcat,那么不同的tomcat端口也不可以重复,因为是共用一个本地ip地址,因此端口号数据是共享的】

    配置 访问路径去除工程名

    在<Host>标签内部添加新标签 <Context>

    注意了 ,重点来了

    配置<Host>标签 信息 

    name 是域名或者ip地址 【默认本地localhost ,如果设置域名还需要配置解析才能生效,我懒得写,以后再说吧】,

    appBase 是应用路径 ,如果配置了新标签 <Context> 必须去除 appBase 参数,否则会在tomcat启动时启动两次,

    会导致报错org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean

     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
       -->
      <Service name="Catalina">
    
        <!--The connectors can use a shared executor, you can define one or more named thread pools-->
        <!--
        <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
            maxThreads="150" minSpareThreads="4"/>
        -->
    
    
        <!-- A "Connector" represents an endpoint by which requests are received
             and responses are returned. Documentation at :
             Java HTTP Connector: /docs/config/http.html
             Java AJP  Connector: /docs/config/ajp.html
             APR (HTTP/AJP) Connector: /docs/apr.html
             Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
        -->
    <!-- 设置访问工程端口号-->
        <Connector port="8001" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />
        <!-- A "Connector" using the shared thread pool-->
        <!--
        <Connector executor="tomcatThreadPool"
                   port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />
        -->
        <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
             This connector uses the NIO implementation. The default
             SSLImplementation will depend on the presence of the APR/native
             library and the useOpenSSL attribute of the
             AprLifecycleListener.
             Either JSSE or OpenSSL style configuration may be used regardless of
             the SSLImplementation selected. JSSE style configuration is used below.
        -->
        <!--
        <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
                   maxThreads="150" SSLEnabled="true">
            <SSLHostConfig>
                <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                             type="RSA" />
            </SSLHostConfig>
        </Connector>
        -->
        <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
             This connector uses the APR/native implementation which always uses
             OpenSSL for TLS.
             Either JSSE or OpenSSL style configuration may be used. OpenSSL style
             configuration is used below.
        -->
        <!--
        <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
                   maxThreads="150" SSLEnabled="true" >
            <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
            <SSLHostConfig>
                <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                             certificateFile="conf/localhost-rsa-cert.pem"
                             certificateChainFile="conf/localhost-rsa-chain.pem"
                             type="RSA" />
            </SSLHostConfig>
        </Connector>
        -->
    
        <!-- Define an AJP 1.3 Connector on port 8009 -->
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    
    
        <!-- An Engine represents the entry point (within Catalina) that processes
             every request.  The Engine implementation for Tomcat stand alone
             analyzes the HTTP headers included with the request, and passes them
             on to the appropriate Host (virtual host).
             Documentation at /docs/config/engine.html -->
    
        <!-- You should set jvmRoute to support load-balancing via AJP ie :
        <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
        -->
        <Engine name="Catalina" defaultHost="localhost">
    
          <!--For clustering, please take a look at documentation at:
              /docs/cluster-howto.html  (simple how to)
              /docs/config/cluster.html (reference documentation) -->
          <!--
          <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
          -->
    
          <!-- Use the LockOutRealm to prevent attempts to guess user passwords
               via a brute-force attack -->
          <Realm className="org.apache.catalina.realm.LockOutRealm">
            <!-- This Realm uses the UserDatabase configured in the global JNDI
                 resources under the key "UserDatabase".  Any edits
                 that are performed against this UserDatabase are immediately
                 available for use by the Realm.  -->
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                   resourceName="UserDatabase"/>
          </Realm>
    
    <!--      修改域名-->
    <!--      <Host name="localhost"  appBase="webapps"-->
    <!--            unpackWARs="true" autoDeploy="true">-->
    <!--      如果设置了将访问路径去除了项目名,则必须删除参数appBase的数据 ,否则会导致同一个工程加载两次,
    然后报错org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean-->
          <Host name="localhost"  appBase=""
                unpackWARs="true" autoDeploy="true">
    
    <!-- path="" 是访问路径  , docBase是war包解压后的文件夹在tomcat里的相对位置 ,reloadable是当配置文件有修改时重启节点-->
                    <Context path="" docBase="webapps/provider-8001"  reloadable="true" />
    
    
            <!-- SingleSignOn valve, share authentication between web applications
                 Documentation at: /docs/config/valve.html -->
            <!--
            <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
            -->
    
            <!-- Access log processes all example.
                 Documentation at: /docs/config/valve.html
                 Note: The pattern used is equivalent to using pattern="common" -->
            <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>
    </Server>
    View Code

    保存文件

    (7)进入work/Catalina目录查看内部是否有内容,有得则删除,没有则忽略这一步

    (8)进入bin目录 ,找到 startup.bat 双击运行即可

    (9)出现 start Server startup in 15866 ms  即表示工程启动成功

    3.测试

    (1)访问网址  http://localhost:8001/getname?name=爱你哟

    成功访问controller层接口

    (2)我做了个 zuul【5001】 ->消费者【9001】 - >上面tomcat配置的工程【8001】 三成调用 【分布式 微服务框架 spring security 】的测试

    具体实现这里不展示

    访问网址  http://localhost:5001/mzuul/consumer-9001/doname?name=岑惜&token=nuu

            完美完成,撒花!!!

    ------------------------------------

    参考博文原址 : 

     https://blog.csdn.net/yin__ren/article/details/93198351

    https://www.cnblogs.com/zhaosq/p/10870762.html

    https://www.cnblogs.com/tudou-22/p/9330875.html

  • 相关阅读:
    Decimal 格式化输出( 去掉多余的0和点)
    HTML Character Sets
    生成下面的模块时,启用了优化或没有调试信息
    PJBLog的CSS模板图
    .NET 实例化顺序
    Live Mail 报错 0x80048820 可能的处理方式
    Windows下将Ldif文件导入OpenLdap时的中文转换问题
    DataGrid中动态添加列
    Sip协议栈消息层的设计与实现
    Prism学习笔记模块之间通信的几种方式
  • 原文地址:https://www.cnblogs.com/c2g5201314/p/13053315.html
Copyright © 2020-2023  润新知