springmvc内嵌tomcat、tomcat整合springmvc、自研国产web中间件
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.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為啟動類
啟動類 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中間件完成。
總結
以上是生活随笔為你收集整理的springmvc内嵌tomcat、tomcat整合springmvc、自研国产web中间件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9 个让你的 Python 代码更快的小
- 下一篇: IPv6实现内网穿透,极低成本保姆级教程