springmvc内嵌tomcat、tomcat整合springmvc、自研国产web中间件

发布时间 2024-01-08 17:44:55作者: 凌康

springmvc内嵌tomcat、tomcat整合springmvc、自研国产web中间件

这是由于公司老项目转化springboot存在太多坑,特别是hibernate事务一条就坑到跑路,你又不想搞没听说过的国产中间件兼容,又不想搞weblogic、WebSphere等中间件的适配,不如直接把提供给客户的产品内嵌于tomcat中,启动就是一个sh,同时让客户不用安装tomcat,释放你的维护时间

信创时,使用东方通(TongWeb)、宝兰德。有些名字你都没听过的
还有国外的IBM、weblogic等商用servlet容器
上面的容器或多或少都有各种坑,直接使用原方案tomcat部署,于是有了此文,将内嵌的tomcat直接运行springmvc项目。

此文优势

你可以根据此文章,自研一个国产中间件,它的功能照抄weblogic即可。用于加入信创项目赚钱自研产品。

前提条件

原springmvc项目转成springboot难度大,与其强行转不如折中转,质疑各种商用中间件、理解各种web商用中间件、放弃各种web商用中间件、成为各种web商用中间件提供商。

基础组成

项目框架组成:外置tomcat + spring5.3.x + springmvc +hibernate +mysql(oracle)
整改后:tomcat内嵌 + spring5.3.x + springmvc +hibernate +mysql(oracle)

特点

  • 老项目开发正常按照原来的开发模式,idea+tomcat。
  • 打包时,不是生成war,而是生成目录以及sh启动脚本。将内嵌tomcat打包到jar,同时添加sh启动脚本。(区别)

添加依赖

        <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>9.0.84</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.84</version>
        </dependency>

2024-01-05 最新依赖,若spring版本较低,适当降低 tomcat-embed 版本

将打包类型改为jar

<packaging>jar</packaging>

移除原来的war插件:maven-war-plugin

添加下面的插件

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.6.0</version>
  <configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <!-- 此处,要改成自己的程序入口(即 main 函数类) -->
      <manifest>
        <mainClass>awb.TomcatServer</mainClass>
      </manifest>
    </archive>
    <descriptors>
      <!--assembly配置文件路径,注意需要在项目中新建文件package.xml-->
      <descriptor>${project.basedir}/src/main/resource/package/package.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

需要注意,老项目的resource是resource 最后面没有s,springboot项目是有s的。

src/main/resource/package/package.xml 配置如下

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <!--
        assembly 打包配置更多配置可参考官方文档:
            http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
     -->
    <id>release</id>

    <!--
        设置打包格式,可同时设置多种格式,常用格式有:dir、zip、tar、tar.gz
            dir 格式便于在本地测试打包结果
            zip 格式便于 windows 系统下解压运行
            tar、tar.gz 格式便于 linux 系统下解压运行
     -->
    <formats>
        <format>dir</format>
        <!--<format>zip</format>-->
        <!-- <format>tar.gz</format> -->
    </formats>

    <!-- 打 zip 设置为 true 时,会在 zip 包中生成一个根目录,打 dir 时设置为 false 少层目录 -->
    <!--<includeBaseDirectory>true</includeBaseDirectory>-->

    <fileSets>
        <!-- src/main/resource 全部 copy 到 config 目录下 -->
        <fileSet>
            <directory>${basedir}/src/main/resource</directory>
            <outputDirectory>WebContent/WEB-INF/classes</outputDirectory>
            <includes>
                <!--包含那些依赖-->
            </includes>
        </fileSet>

        <!-- 项目根下面的脚本文件 copy 到根目录下 -->
        <fileSet>
            <directory>${basedir}/src/main/resource/package</directory>
            <outputDirectory></outputDirectory>
            <!-- 脚本文件在 linux 下的权限设为 755,无需 chmod 可直接运行 -->
            <fileMode>755</fileMode>
            <lineEnding>unix</lineEnding>
            <includes>
                <include>*.sh</include>
                <include>*.bat</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${basedir}/WebContent/WEB-INF/lib</directory>
            <outputDirectory>WebContent/WEB-INF/lib</outputDirectory>
            <includes>
                <!--包含那些依赖-->
                <include>*.jar</include>
            </includes>
        </fileSet>
        <!-- 静态资源 -->
        <fileSet>
            <directory>${basedir}/WebContent</directory>
            <outputDirectory>WebContent</outputDirectory>
            <includes>
                <!--包含那些依赖-->
                <include>AFA_Management_Fonts/**</include>
                <include>compressor/**</include>
                <include>conf/**</include>
                <include>dependence/**</include>
                <include>elementui/**</include>
                <include>fonts/**</include>
                <include>icons/**</include>
                <include>image/**</include>
                <include>img/**</include>
                <include>module/**</include>
                <include>script/**</include>
                <include>*.js</include>
                <include>*.html</include>
                <include>*.css</include>
                <include>*.json</include>
                <include>WEB-INF/web.xml</include>
            </includes>
        </fileSet>


    </fileSets>

    <!-- 依赖的 jar 包 copy 到 lib 目录下 -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>WebContent/WEB-INF/lib</outputDirectory>
        </dependencySet>
    </dependencySets>

</assembly>

src/main/resource/package/start.batsrc/main/resource/package/start.sh 分别对应Linux、window下的启动脚本

@echo off
setlocal & pushd

set MAIN_CLASS=awb.TomcatServer

set JAVA_OPTS=-Xms256m -Xmx2048m -Dfile.encoding=UTF-8

set APP_BASE_PATH=%~dp0
set CP=%APP_BASE_PATH%WebContent\WEB-INF\lib\*;%APP_BASE_PATH%WebContent\WEB-INF\classes

java -Xverify:none %JAVA_OPTS% -cp %CP% %MAIN_CLASS%
goto:eof

sh

#!/bin/bash
# ----------------------------------------------------------------------
#
# 使用说明:
# 1: 该脚本使用前需要首先修改 MAIN_CLASS 值,使其指向实际的启动类
#
# 2:使用命令行 ./start.sh start | stop | restart 可启动/关闭/重启项目
#
#
# 3: JAVA_OPTS 可传入标准的 java 命令行参数,例如 -Xms256m -Xmx2048m 这类常用参数
#
# 4: 函数 start() 给出了 4 种启动项目的命令行,根据注释中的提示自行选择合适的方式
#
# ----------------------------------------------------------------------

# 启动入口类,该脚本文件用于别的项目时要改这里
MAIN_CLASS=awb.TomcatServer

COMMAND="$1"


# Java 命令行参数,根据需要开启下面的配置,改成自己需要的
JAVA_OPTS="-Xms256m -Xmx2048m -Dfile.encoding=UTF-8"

# 生成 class path 值
APP_BASE_PATH=$(cd `dirname $0`; pwd)
CP=${APP_BASE_PATH}/WebContent/WEB-INF/lib/*:${APP_BASE_PATH}/WebContent/WEB-INF/classes

function start()
{
    # 运行为后台进程,并在控制台输出信息
    #java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} &

    # 运行为后台进程,并且不在控制台输出信息
    # nohup java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} >/dev/null 2>&1 &

    # 运行为后台进程,并且将信息输出到 output.log 文件
    nohup java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} > output.out &

    # 运行为非后台进程,多用于开发阶段,快捷键 ctrl + c 可停止服务
    # java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS}
}

function stop()
{
    # 支持集群部署
    kill `pgrep -f ${APP_BASE_PATH}` 2>/dev/null

    # kill 命令不使用 -9 参数时,会回调 onStop() 方法,确定不需要此回调建议使用 -9 参数
    # kill `pgrep -f ${MAIN_CLASS}` 2>/dev/null

    # 以下代码与上述代码等价
    # kill $(pgrep -f ${MAIN_CLASS}) 2>/dev/null
}

if [[ "$COMMAND" == "start" ]]; then
	start
elif [[ "$COMMAND" == "stop" ]]; then
    stop
else
    stop
    start
fi

awb.TomcatServer为启动类

启动类 TomcatServer

package awb;

import awb.operations.config.GlobalConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

import java.io.File;

/**
 * @author lingkang
 * created by 2024/1/5
 */
@Slf4j
public class TomcatServer {
    public static void main(String[] args) throws Exception {
        log.info("服务启动中...");
        // 端口和上下文路路径
        int port = Integer.parseInt(GlobalConfig.PROP.getProperty("server.port", "8080"));
        String path = GlobalConfig.PROP.getProperty("server.context.path", "/afa");
        log.info("path={} , port={}", path, port);
        Tomcat tomcat = new Tomcat();
        tomcat.setHostname("0.0.0.0");
        // 端口监听
        Connector connector = tomcat.getConnector();
        connector.setPort(port);

        // WebContent 的名称要与打包的名称对上,使用当前路径
        String dir = System.getProperty("user.dir");
        log.info("dir : {}", dir);
        String WebContent = dir + File.separator + "WebContent";
        log.info("WebContent : {}", WebContent);
        tomcat.setBaseDir(WebContent);
        tomcat.addWebapp(path, WebContent);

        // 启动
        tomcat.start();

        // 服务连接
        tomcat.getService().addConnector(connector);
        log.info("web: http://localhost:" + port + path);
        tomcat.getServer().await();
    }
}

注意,上面的 GlobalConfig 是读取的一个配置文件,主要用于动态配置端口后访问上下文,可自定义或写死

项目结构是传统的servlet整合springmvc

打包

执行

mvn package

或者用idea的插件

输出如下:

window下双击 start.bat 启动

没毛病,能正常访问:http://localhost:8080/afa

还不影响xml等配置的修改

至此,自研国产web中间件完成。