springcloud的项目使用一个tomcat部署

news2024/9/20 11:32:37

背景

我们项目使用springcloud、redis(缓存)、rocketMQ(消息中间件)、tinyid(分布式id)、minio(文件存储)、nacos(配置注册中心)这些组件开发了一个mes系统,但是有些工厂体量小,没有很大并发,所以考虑使用一台服务器,启动项目。

方案

  1. 所有模块的war放在tomcat的webapps下(这种不解释了,直接放进去就行);
  2. 分模块,分端口,把war放在通一个tomcat的不同webapps下,具体实现如下:
    1、新建几个webapps,改成有代表意义的名称,例如你的模块名称;

在这里插入图片描述
2、配置Tomcat 9.0\conf\server.xml(新建几个webapps就增加几个Service 的模块)

<?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="-1" 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>
 <!-- mes -->
  <Service name="Catalina"> 
    <Connector port="8084" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"  secretRequired="false"/>
    <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_rt"
            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" />
      </Host>
    </Engine>
  </Service>
   <!-- user -->
  <Service name="Catalina"> 
    <Connector port="8011" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8012" protocol="AJP/1.3" redirectPort="8443"  secretRequired="false"/>
    <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_tinyid"
            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" />
      </Host>
    </Engine>
  </Service>
</Server>

注意:

  • 更改appbase为自己新建的名称(保持一致):< Host name=“localhost” appBase=“webapps_tinyid”
    unpackWARs=“true” autoDeploy=“true”>
  • 更改端口号,防止端口被占用:< Connector port=“8011” protocol=“HTTP/1.1”
    connectionTimeout=“20000”
    redirectPort=“8443” />
    < Connector port=“8012” protocol=“AJP/1.3” redirectPort=“8443” secretRequired=“false”/>

步骤

  1. 把所有的模块都打成war包
    在pom中增加如下配置:
     <!-- 这里指定打war包的时不再需要tomcat相关的包 -->
     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
      </dependency>
      <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <scope>provide</scope>
       </dependency>
    <build>
        <finalName>mes</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

在pom上添加打包后的名称和使用springboot的插件打包:

 <build>
         <!--打包名称-->
        <finalName>mes</finalName> 
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

*注意:一定要添加上方pom配置,不然是项目在tomcat中会重启两次。
在启动类上添加,并且继承 SpringBootServletInitializer:

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

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(Application.class);
    }
}
  1. 启动后在nacos不能注册,是因为springboot启动后开放端口,但是使用war包启动后,不能开放,需要添加如下的配置类:
@Component
public class NacosConfig implements ApplicationRunner {

    @Autowired(required = false)
    private NacosAutoServiceRegistration registration;

    @Value("${server.port}")
    Integer port;

    @Override
    public void run(ApplicationArguments args) {
        if (registration != null && port != null) {
            Integer tomcatPort = port;
            try {
                tomcatPort = new Integer(getTomcatPort());
            } catch (Exception e) {
                e.printStackTrace();
            }

            registration.setPort(tomcatPort);
            registration.start();
        }
    }

    /**
     * 获取外部tomcat端口
     */
    public String getTomcatPort() throws Exception {
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        String port = objectNames.iterator().next().getKeyProperty("port");
        return port;
    }
}

3、MQ的处理;

  • 在pom中配置MQ
 <dependency>
      <groupId>org.apache.rocketmq</groupId>
      <artifactId>rocketmq-client</artifactId>
      <scope>provided</scope>
  </dependency>	
  • 去除MQProducerConfig(根据自己的类名)的@Configuration的注解;
  • 处理生产者的代码
    1、新建包和原来生产者包名一致,新建接口和原来生产者类名一致;
    2、新建包和类,实现以前生产者的接口;
    3、新建包和类,实现以前生产者的接口,但是实现方法体为空。

在这里插入图片描述
在这里插入图片描述
如果做完这些,你的springcloud的项目就可以在一个tomcat下面部署了,并且可以去除MQ。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/458549.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

YOLOv7+单目实现三维跟踪(python)

YOLOv7单目跟踪 1. 目标跟踪2. 测距模块2.1 设置测距模块2.2 添加测距 3. 主代码4. 实验效果 相关链接 1. YOLOv5单目测距&#xff08;python&#xff09; 2. YOLOv7单目测距&#xff08;python&#xff09; 3. YOLOv5单目跟踪&#xff08;python&#xff09; 4. 具体效果已在B…

中期国际:值得信赖的外汇MT4开户平台应该具备那些特点

在外汇市场中&#xff0c;有许多外汇平台供投资者选择。然而&#xff0c;由于市场存在许多复杂因素&#xff0c;选择平台时必须谨慎。投资者必须选择具有可靠资质的正规外汇MT4开户平台&#xff0c;以提高投资的安全性。选择外汇MT4开户平台非常重要&#xff0c;因此&#xff0…

LVS负载均衡群集—NAT

目录 一、群集的概述1、群集的含义2、出现高并发的解决方法3、群集的三种分类3.1负载均衡群集3.2高可用群集3.3高性能运算群集 4、负载均衡的结构 三、LVS调度器用的调度方法四、LVS的工作模式及其工作过程1.NAT模式&#xff08;VS-NAT&#xff09;2.直接路由模式&#xff08;V…

springboot整合juit和springboot整合mybatis

springboot整合juit 先看一眼包路径&#xff0c;发现main程序的路径和测试类的路径是一样的 启用新注解&#xff1a;SpringBootTest代替了之前sm整合juit时的 RunWith(SpringJUnit4ClassRunner.class) //spring配置类 ContextConfiguration(classes config.class)新的如此…

protoc 插件-protoc-gen-grpc-gateway-gosdk

&#x1f447;我在这儿 基本介绍 protoc-gen-grpc-gateway-gosdk 是一个 protoc 插件, 能根据 proto 文件一键生成 go http sdk 客户端代码&#xff0c;通过借助 grpc-gateway 插件将 grpc 接口转化为 http 的方式, 进而可以通过本插件生成 http sdk 代码。 特性 1.一键自动生…

springboot整合cache+redis

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、cache是什么&#xff1f;二、使用步骤1.使用方式1.引入依赖2.搭建项目依赖问题application.ymlTestControllerTestServiceTestServiceImplUserMapperMyRedi…

使用vue.component全局注册组件、props的使用

通过components注册的是私有子组件 例如&#xff1a; 在组件A的 components 节点下&#xff0c;注册了组件F。 则组件F只能用在组件A中;不能被用在组件C中。 注册全局组件 在vue项目的 main.js 入口文件中&#xff0c;通过 Vue.component() 方法&#xff0c;可以注册全局组件…

数据结构和算法学习记录——平衡二叉树(基本介绍、平衡因子、平衡二叉树的定义、平衡二叉树的高度)

目录 基本介绍 平衡因子 平衡二叉树 平衡二叉树的高度 基本介绍 什么是平衡二叉树&#xff1f; 以一个例子来解释一下&#xff1a; 搜索树结点按不同的插入次序&#xff0c;将会导致不同的深度和平均查找长度ASL 在二叉搜索树中查找一个元素&#xff1a; &#xff08…

TCP 协议的低效实现

包括 Linux kernel 在内的各种 TCP 实现均使用类似 skb 的对象管理一个个 packet&#xff0c;使 TCP 失去了 “流” 特征。应用通过 syscall 每写入一批数据&#xff0c;协议栈都可能生成一个 skb&#xff1a; ​ 仅管理这些 skb 就是一笔大开销。除了 skb 数据结构本身的 cru…

Python小姿势 - import requests

import requests Python中使用requests模块发送POST请求 在使用Python进行开发时&#xff0c;经常会遇到需要向某个网址发送POST请求的情况。这时候就需要使用到requests模块了。 requests模块是Python的一个标准模块&#xff0c;可以直接使用pip安装。 安装完成后&#xff0c;…

Java每日一练(20230425)

目录 1. 乘积最大子数组 &#x1f31f;&#x1f31f; 2. 插入区间 &#x1f31f;&#x1f31f; 3. 删除有序数组中的重复项 II &#x1f31f;&#x1f31f; &#x1f31f; 每日一练刷题专栏 &#x1f31f; Golang每日一练 专栏 Python每日一练 专栏 C/C每日一练 专栏…

CesiumForUnreal之3DTileset点选拾取属性与单体高亮

文章目录 1.实现目标2.实现过程2.1 3DTiles数据准备2.2 属性拾取2.3 单体高亮3.参考资料1.实现目标 在UE5中使用CesiumForUnreal插件加载本地的3dTiles建筑白模数据,实现点击拾取3DTileset单体要素的属性数据,并对高亮单体进行展示,GIF动图如下: 2.实现过程 总体的实现过程…

模型剪枝网络 Learning Efficient Network throung Network Slimming 简述

1. 概述 训练得到的特征图&#xff0c;并不是所有特征图都重要&#xff0c;另一方面&#xff0c;希望对权重执行策略&#xff0c;体现出权重之间的差异性&#xff0c;最终目的就是获得不同特征图中的channel sacling factors&#xff0c;表征了不同特征图的重要性 2. BN 采…

老码农眼中的大模型(LLM)

即便全力奔跑&#xff0c;也不一定能跟上时代的步伐。但如果失去了学习的动力&#xff0c;很可能会被时代淘汰。而且&#xff0c;当时代淘汰我们的时候&#xff0c;往往不会有任何预警。基于大模型的 ChatGPT 给我们带来了极大的震撼&#xff0c;那么什么是大模型呢&#xff1f…

【网络进阶】五种IO网络模型(一)

文章目录 1. 阻塞IO2. 非阻塞IO 1. 阻塞IO 在Linux中&#xff0c;默认情况下&#xff0c;所有的套接字&#xff08;socket&#xff09;都是阻塞的。典型的读取操作流程如下&#xff1a; 当用户进程调用read系统调用时&#xff0c;内核开始执行I/O的第一个阶段&#xff0c;即…

智慧医院智能化系统设计与能耗管理产品选型

摘要&#xff1a;结合某知名大型三甲综合医院项目的智能化系统设计&#xff0c;提出智慧医院智能化系统的技术解决方案&#xff0c;阐述智慧医院智能化系统方案的总体架构、建设目标、设计宗旨、典型应用及各智能化子系统的设计方案。 关键词&#xff1a;智慧医院&#xff1b;智…

mybatis3源码篇(2)——执行流程

mybatis 版本&#xff1a;v3.3.0 文章目录 执行流程MapperProxyFactoryMapperProxyMapperMethodexecuteconvertArgsToSqlCommandParamResultHandler SqlSessionExecutor&#xff08;执行器&#xff09;StatementHandler&#xff08;声明处理器&#xff09;ParameterHandler&…

【设计模式】我对设计模式的C语言解读(下)

书接上回 由于内容太多&#xff0c;编辑器太卡了&#xff0c;所以分P了 上P在这里 目录 书接上回备忘录模式观察者模式 备忘录模式 备忘录模式的介绍: https://refactoringguru.cn/design-patterns/memento 备忘录模式的C实现: https://refactoringguru.cn/design-patterns/m…

【数据挖掘与商务智能决策】第十三章 数据降维之PCA 主成分分析

13.1.2 PCA主成分分析代码实现 1.二维空间降维Python代码实现 import numpy as np X np.array([[1, 1], [2, 2], [3, 3]]) Xarray([[1, 1],[2, 2],[3, 3]])# 也可以通过pandas库来构造数据&#xff0c;效果一样 import pandas as pd X pd.DataFrame([[1, 1], [2, 2], [3, 3…

二分查找【数组】

⭐前言⭐ ※※※大家好&#xff01;我是同学〖森〗&#xff0c;一名计算机爱好者&#xff0c;今天让我们进入复习模式。若有错误&#xff0c;请多多指教。更多有趣的代码请移步Gitee &#x1f44d; 点赞 ⭐ 收藏 &#x1f4dd;留言 都是我创作的最大的动力&#xff01; 题目 70…