这是由于公司老项目转化springboot存在太多坑,特别是hibernate事务一条就坑到跑路,你又不想搞没听说过的国产中间件兼容,又不想搞weblogic、WebSphere等中间件的适配,不如直接把提供给客户的产品内嵌于tomcat中,启动就是一个sh,同时让客户不用安装tomcat,释放你的维护时间
。
信创时,使用
东方通
(TongWeb)、宝兰德
。有些名字你都没听过的
还有国外的IBM、weblogic等商用servlet容器
上面的容器或多或少都有各种坑,直接使用原方案tomcat部署,于是有了此文,将内嵌的tomcat直接运行springmvc项目。
你可以根据此文章,自研
一个国产中间件
,它的功能照抄weblogic即可。用于加入信创项目赚钱自研产品。
转自 https://lingkang.top/archives/springmvc-nei-qian-tomcat
转自 https://lingkang.top/archives/springmvc-nei-qian-tomcat
转自 https://lingkang.top/archives/springmvc-nei-qian-tomcat
原springmvc项目转成springboot难度大,与其强行转不如折中转,质疑各种商用中间件、理解各种web商用中间件、放弃各种web商用中间件、成为各种web商用中间件提供商。
项目框架组成:外置tomcat + spring5.3.x + springmvc +hibernate +mysql(oracle)
整改后:tomcat内嵌 + spring5.3.x + springmvc +hibernate +mysql(oracle)
打包时,不是生成war,而是生成目录以及sh启动脚本。
将内嵌tomcat打包到jar,同时添加sh启动脚本。(区别)
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-coreartifactId>
<version>9.0.84version>
dependency>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-jasperartifactId>
<version>9.0.84version>
dependency>
2024-01-05 最新依赖,若spring版本较低,适当降低 tomcat-embed 版本
将打包类型改为jar
<packaging>jarpackaging>
移除原来的war插件:maven-war-plugin
添加下面的插件
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>3.6.0version>
<configuration>
<appendAssemblyId>falseappendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependenciesdescriptorRef>
descriptorRefs>
<archive>
<manifest>
<mainClass>awb.TomcatServermainClass>
manifest>
archive>
<descriptors>
<descriptor>${project.basedir}/src/main/resource/package/package.xmldescriptor>
descriptors>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
需要注意,老项目的resource是resource
最后面没有s,springboot项目是有s的。
src/main/resource/package/package.xml
配置如下
<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">
<id>releaseid>
<formats>
<format>dirformat>
formats>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/resourcedirectory>
<outputDirectory>WebContent/WEB-INF/classesoutputDirectory>
<includes>
includes>
fileSet>
<fileSet>
<directory>${basedir}/src/main/resource/packagedirectory>
<outputDirectory>outputDirectory>
<fileMode>755fileMode>
<lineEnding>unixlineEnding>
<includes>
<include>*.shinclude>
<include>*.batinclude>
includes>
fileSet>
<fileSet>
<directory>${basedir}/WebContent/WEB-INF/libdirectory>
<outputDirectory>WebContent/WEB-INF/liboutputDirectory>
<includes>
<include>*.jarinclude>
includes>
fileSet>
<fileSet>
<directory>${basedir}/WebContentdirectory>
<outputDirectory>WebContentoutputDirectory>
<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>*.jsinclude>
<include>*.htmlinclude>
<include>*.cssinclude>
<include>*.jsoninclude>
<include>WEB-INF/web.xmlinclude>
includes>
fileSet>
fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>WebContent/WEB-INF/liboutputDirectory>
dependencySet>
dependencySets>
assembly>
src/main/resource/package/start.bat
、src/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
为启动类
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
是读取的一个配置文件,主要用于动态配置端口后访问上下文,可自定义或写死
执行
mvn package
没毛病,能正常访问:http://localhost:8080/afa
至此,自研国产web中间件完成。