springmvc內(nèi)嵌tomcat放典、tomcat整合springmvc、自研國產(chǎn)web中間件
這是由于公司老項目轉(zhuǎn)化springboot存在太多坑基茵,特別是hibernate事務(wù)一條就坑到跑路奋构,你又不想搞沒聽說過的國產(chǎn)中間件兼容,又不想搞weblogic耿导、WebSphere等中間件的適配声怔,不如直接把提供給客戶的產(chǎn)品內(nèi)嵌于tomcat中,啟動就是一個sh舱呻,同時讓客戶不用安裝tomcat,釋放你的維護(hù)時間
悠汽。
信創(chuàng)時箱吕,使用
東方通
(TongWeb)、寶蘭德
柿冲。有些名字你都沒聽過的
還有國外的IBM茬高、weblogic等商用servlet容器
上面的容器或多或少都有各種坑,直接使用原方案tomcat部署假抄,于是有了此文葵擎,將內(nèi)嵌的tomcat直接運行springmvc項目嗡综。
此文優(yōu)勢
你可以根據(jù)此文章,自研
一個國產(chǎn)中間件
,它的功能照抄weblogic即可冠摄。用于加入信創(chuàng)項目賺錢自研產(chǎn)品。
前提條件
原springmvc項目轉(zhuǎn)成springboot難度大门扇,與其強(qiáng)行轉(zhuǎn)不如折中轉(zhuǎn)圆雁,質(zhì)疑各種商用中間件、理解各種web商用中間件为黎、放棄各種web商用中間件邮丰、成為各種web商用中間件提供商行您。
基礎(chǔ)組成
項目框架組成:外置tomcat + spring5.3.x + springmvc +hibernate +mysql(oracle)
整改后:tomcat內(nèi)嵌 + spring5.3.x + springmvc +hibernate +mysql(oracle)
特點
- 老項目開發(fā)正常按照原來的開發(fā)模式,idea+tomcat剪廉。
-
打包時娃循,不是生成war,而是生成目錄以及sh啟動腳本斗蒋。
將內(nèi)嵌tomcat打包到j(luò)ar捌斧,同時添加sh啟動腳本。(區(qū)別)
添加依賴
<!-- 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版本較低骤星,適當(dāng)降低 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 函數(shù)類) -->
<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>
<!--
設(shè)置打包格式队贱,可同時設(shè)置多種格式,常用格式有:dir潭袱、zip柱嫌、tar、tar.gz
dir 格式便于在本地測試打包結(jié)果
zip 格式便于 windows 系統(tǒng)下解壓運行
tar屯换、tar.gz 格式便于 linux 系統(tǒng)下解壓運行
-->
<formats>
<format>dir</format>
<!--<format>zip</format>-->
<!-- <format>tar.gz</format> -->
</formats>
<!-- 打 zip 設(shè)置為 true 時编丘,會在 zip 包中生成一個根目錄,打 dir 時設(shè)置為 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 下的權(quán)限設(shè)為 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>
<!-- 靜態(tài)資源 -->
<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
分別對應(yīng)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 值晕窑,使其指向?qū)嶋H的啟動類
#
# 2:使用命令行 ./start.sh start | stop | restart 可啟動/關(guān)閉/重啟項目
#
#
# 3: JAVA_OPTS 可傳入標(biāo)準(zhǔn)的 java 命令行參數(shù)抑片,例如 -Xms256m -Xmx2048m 這類常用參數(shù)
#
# 4: 函數(shù) start() 給出了 4 種啟動項目的命令行,根據(jù)注釋中的提示自行選擇合適的方式
#
# ----------------------------------------------------------------------
# 啟動入口類杨赤,該腳本文件用于別的項目時要改這里
MAIN_CLASS=awb.TomcatServer
COMMAND="$1"
# Java 命令行參數(shù)敞斋,根據(jù)需要開啟下面的配置,改成自己需要的
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()
{
# 運行為后臺進(jìn)程疾牲,并在控制臺輸出信息
#java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} &
# 運行為后臺進(jìn)程植捎,并且不在控制臺輸出信息
# nohup java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} >/dev/null 2>&1 &
# 運行為后臺進(jìn)程,并且將信息輸出到 output.log 文件
nohup java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS} > output.out &
# 運行為非后臺進(jìn)程说敏,多用于開發(fā)階段鸥跟,快捷鍵 ctrl + c 可停止服務(wù)
# java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS}
}
function stop()
{
# 支持集群部署
kill `pgrep -f ${APP_BASE_PATH}` 2>/dev/null
# kill 命令不使用 -9 參數(shù)時,會回調(diào) onStop() 方法,確定不需要此回調(diào)建議使用 -9 參數(shù)
# 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("服務(wù)啟動中...");
// 端口和上下文路路徑
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");
// 端口監(jiān)聽
Connector connector = tomcat.getConnector();
connector.setPort(port);
// WebContent 的名稱要與打包的名稱對上医咨,使用當(dāng)前路徑
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();
// 服務(wù)連接
tomcat.getService().addConnector(connector);
log.info("web: http://localhost:" + port + path);
tomcat.getServer().await();
}
}
注意枫匾,上面的 GlobalConfig
是讀取的一個配置文件,主要用于動態(tài)配置端口后訪問上下文拟淮,可自定義或?qū)懰?/p>
項目結(jié)構(gòu)是傳統(tǒng)的servlet整合springmvc
打包
執(zhí)行
mvn package
或者用idea的插件
輸出如下:
window下雙擊 start.bat
啟動
沒毛病干茉,能正常訪問:http://localhost:8080/afa
還不影響xml等配置的修改
至此,自研國產(chǎn)web中間件完成很泊。