基于《TongWeb V7.0 嵌入式版 JavaEE标准容器用户指南》精炼,仅保留 Spring Boot 2.x 相关内容。
1. 环境要求
| 组件 | 要求 |
|---|---|
| Java | JDK 8+ (HTTP/2 需 8u252+) |
| 内存 | ≥ 512MB |
| 磁盘 | ≥ 1024MB |
| Spring Boot | 2.0.x ~ 2.7.x |
| Spring Cloud | Finchley / Greenwich / Hoxton / 2020.0.x / 2021.0.x / 2022.0.x |
JavaEE 规范支持
| 规范 | 版本 |
|---|---|
| Servlet | 4.0 |
| JSP | 2.3 |
| Expression Language | 3.0 |
| WebSocket | 1.1 |
| JASPIC | 1.1 |
2. 安装与引入
2.1 安装资源 jar 包到本地仓库
解压 tongweb-embed-7.0.E.{n}.zip,运行安装脚本:
bash
# Linux
./installMavenJar.sh
# Windows
./installMavenJar.bat
安装到企业私仓:
bash
./deployMavenJar.sh <私仓完整URL> <server-id>
# 示例
./deployMavenJar.sh http://127.0.0.1:8081/nexus/content/repositories/central/ central
需在
settings.xml中配置<server>的密钥信息。
2.2 方式一:pom.xml 直接配置依赖(推荐开发阶段)
基础 Starter
xml
<!-- 排除 Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入 TongWeb Starter -->
<dependency>
<groupId>com.tongweb.springboot</groupId>
<artifactId>tongweb-spring-boot-starter-2.x</artifactId>
<version>7.0.E.{n}</version>
</dependency>
WebSocket 支持
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.tongweb.springboot</groupId>
<artifactId>tongweb-spring-boot-websocket-2.x</artifactId>
<version>7.0.E.{n}</version>
</dependency>
<!-- 仍需 tongweb-spring-boot-starter-2.x 和排除 Tomcat -->
JSP 支持(可选)
xml
<dependency>
<groupId>com.tongweb</groupId>
<artifactId>tongweb-jsp</artifactId>
<version>7.0.E.{n}</version>
</dependency>
2.3 方式二:嵌入可执行 jar 包(已编译场景)
使用转换工具自动转换:
bash
# 1. 将 license.dat 放到待转换 jar 同级目录
# 2. 解压 tongweb-embed 和 tongweb-transform 包
# 3. 运行 transform 脚本
手动替换:打开 jar → 进入 BOOT-INF/lib → 删除 Tomcat 相关 jar → 添加 TongWeb jar:
| 必须添加 | 可选添加 |
|---|---|
tongweb-embed-core-7.0.E.{n}.jar |
tongweb-jsp-7.0.E.{n}.jar |
tongweb-embed-el-7.0.E.{n}.jar |
tongweb-gmssl-1.0.0.jar |
tongweb-spring-boot-starter-2.x-7.0.E.{n}.jar |
tongweb-embed-websocket-7.0.E.{n}.jar |
tongweb-javax-annotation-1.2.jar |
tongweb-spring-boot-websocket-2.x-7.0.E.{n}.jar |
| 需删除的 Tomcat jar: |
tomcat-embed-core-x.x.x.jartomcat-embed-el-x.x.x.jartomcat-embed-websocket-x.x.x.jar
2.4 方式三:嵌入可执行 war 包
打开 war → 进入 WEB-INF/lib → 删除 Tomcat 相关 jar(含 spring-boot-starter-tomcat)→ 添加 TongWeb jar(同方式二)。
2.5 方式四:作为外部 jar 包
- 在
src/main/lib目录放入 TongWeb jar pom.xml中配置 resources:
xml
<resources>
<resource>
<directory>src/main/lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
3. License 授权认证
3.1 本地认证(license.dat)
application.properties 方式:
properties
server.tongweb.license.type=file
server.tongweb.license.path=D:\\license.dat
# 或 classpath 方式
server.tongweb.license.path=classpath:license.dat
启动参数方式:
bash
java -Dserver.tongweb.license.type=file \
-Dserver.tongweb.license.path=/home/root/license.dat \
-jar your-app.jar
3.2 License Server 远程认证
properties
server.tongweb.license.type=remote
server.tongweb.license.license-ips=127.0.0.1:8888
server.tongweb.license.license-public-key=<从 License Server 控制台获取>
4. 安全管理
4.1 国密 SSL
安装: 解压 tongweb-gmssl-1.0.0.zip,运行 installMavenJar 脚本。
pom.xml:
xml
<dependency>
<groupId>com.tongweb</groupId>
<artifactId>tongweb-gmssl</artifactId>
<version>1.0.0</version>
</dependency>
配置:
properties
server.ssl.enabled=true
server.ssl.protocol=GMSSLv1.1
server.ssl.key-store=classpath:sm2.enc.pfx
server.ssl.key-store-password=xxxx
server.ssl.key-store-type=PKCS12
server.ssl.trust-store=classpath:sm2.sig.pfx
server.ssl.trust-store-password=xxxx
server.ssl.trust-store-type=PKCS12
如需 HTTP/2 + 国密,添加
server.http2.enabled=true。证书制作:
opensslpkcs12 -export -in sm2.enc.cert.pem -inkey sm2.enc.key.pem -out sm2.enc.pfx
4.2 HTTPS
properties
server.ssl.enabled=true
server.ssl.protocol=TLS
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=xxxx
server.ssl.client-auth=none
server.ssl.trust-store=classpath:truststore.pfx
server.ssl.trust-store-password=xxxx
server.ssl.trust-store-type=JKS
4.3 umask 支持
启动脚本设置 umask:
bash
UMASK="0027"
umask $UMASK
java -jar -Dcom.tongweb.container.security.SecurityListener.UMASK=$UMASK your-app.jar
SecurityListener 配置:
properties
server.tongweb.security-listener.enabled=true
server.tongweb.security-listener.minimum-umask=0000
server.tongweb.security-listener.remove-root-user=true
server.tongweb.security-listener.checked-os-users=test
5. 日志支持
5.1 应用日志
properties
logging.pattern.console=%d{yyyy-MM-dd-HH:mm:ss.SSS}[%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy-MM-dd-HH:mm:ss.SSS}[%thread] %-5level %logger- %msg%n
logging.level.root=info
logging.level.com.tongweb=info
5.2 访问日志 --- 传统模式
properties
server.tongweb.basedir=./
server.tongweb.accesslog.enabled=true
server.tongweb.accesslog.pattern=common # 或 combined
常用 pattern 变量:
| 变量 | 含义 |
|---|---|
%a |
远程主机 IP |
%A |
本地 IP |
%h |
远程主机名 |
%m |
请求方法 |
%r |
请求第一行 |
%s |
HTTP 状态码 |
%D |
请求处理时间(ms) |
%T |
请求处理时间(s) |
%I |
当前线程名 |
%{xxx}i |
请求头 |
%{xxx}o |
响应头 |
5.3 访问日志 --- 扩展模式
properties
server.tongweb.basedir=./
server.tongweb.accesslog.enabled=true
server.tongweb.accesslog.log-extend=true
server.tongweb.accesslog.pattern=date c-ip cs-method cs-uri
扩展模式格式标识: date, c-ip, cs-method, cs-uri, cs-uri-query, sc-status, time, time-taken, x-threadname 等。
5.4 审计日志
properties
server.tongweb.audit-log.path=./
server.tongweb.audit-log.enable=true
server.tongweb.audit-log.name=audit.log
audit-log.path已配置时,audit-log.name只写文件名,不含路径。
5.5 国际化
properties
server.tongweb.language=zh
server.tongweb.country=CN
6. Filter 配置
6.1 跨域访问 Filter
properties
server.tongweb.filter.cors.enabled=true
server.tongweb.filter.cors.allowed-origins=http://localhost:8092 # 或 * (不推荐)
server.tongweb.filter.cors.allowed-methods=GET,POST,HEAD,OPTIONS
server.tongweb.filter.cors.allowed-headers=Origin,Accept,X-Requested-With,Content-Type
server.tongweb.filter.cors.exposed-headers=Content-Type,Authorization
server.tongweb.filter.cors.support-credentials=false # 设为 true 时 allowed-origins 不能用 *
server.tongweb.filter.cors.preflight-max-age=1800
6.2 黑白名单 Filter
properties
server.tongweb.filter.remote.enabled=true
server.tongweb.filter.remote.allow-addr=192.168.1.*
server.tongweb.filter.remote.deny-addr=10.0.0.1
server.tongweb.filter.remote.deny-status=403
7. 自定义异常处理
7.1 错误页面配置
Step 1: 关闭 Spring Boot 自带错误页面 MVC:
java
@SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class)
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Step 2: 配置 ErrorReportValve(HTML 格式):
properties
server.tongweb.error-report-valve.enabled=true
server.tongweb.error-report-valve.show-server-info=false
server.tongweb.error-report-valve.show-report=true
或 JsonErrorReportValve(JSON 格式,与 ErrorReportValve 同时启用时仅此生效):
properties
server.tongweb.json-error-report-valve.enabled=true
7.2 网络吞吐熔断
properties
server.tongweb.semaphore.enabled=true
server.tongweb.semaphore.concurrency=10
server.tongweb.semaphore.fairness=false
server.tongweb.semaphore.block=true
server.tongweb.semaphore.interruptible=false
7.3 挂起线程熔断
properties
server.tongweb.stuck-thread-detection.threshold=600 # 线程阻塞判定阈值(秒),0=禁用
server.tongweb.stuck-thread-detection.interrupt-thread-threshold=1200 # 中断阈值(秒),-1=不开启
8. 监控支持(Actuator)
8.1 引入依赖
xml
<dependency>
<groupId>com.tongweb.springboot</groupId>
<artifactId>tongweb-spring-boot-starter-2.x</artifactId>
<version>7.0.E.{n}</version>
</dependency>
<dependency>
<groupId>com.tongweb.springboot</groupId>
<artifactId>tongweb-spring-boot-monitor-starter-2.x</artifactId>
<version>7.0.E.{n}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
8.2 配置
properties
management.server.port=9099
management.endpoints.web.base-path=/actuator
management.endpoints.web.exposure.include=*
8.3 访问监控
- 全量指标:
http://<ip>:9099/actuator/monitor - 单个指标:
http://<ip>:9099/actuator/monitor/jvm.buffer.memory.used - 带 tag 过滤:
http://<ip>:9099/actuator/monitor/jvm.memory.committed?tag=area:nonheap,id:Metaspace - Prometheus:
http://<ip>:9099/actuator/monitor/prometheus
8.4 监控安全配置
properties
management.access.iplist=192.168.1.100 # 允许访问的 IP
management.blocked.iplist=127.0.0.1 # 屏蔽的 IP
8.5 服务器元信息
properties
server.tongweb.actuator-monitor.show-server-info=true
9. 数据源支持
9.1 引入依赖
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.tongweb.springboot</groupId>
<artifactId>tongweb-spring-boot-data-jdbc-starter-2.x</artifactId>
<version>7.0.E.{n}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
9.2 配置数据源
properties
# 必须指定 TongWeb 数据源类型
server.tongweb.datasource.type=com.tongweb.hulk.HulkDataSource
server.tongweb.datasource.url=jdbc:mysql://localhost:3306/mydb
server.tongweb.datasource.username=root
server.tongweb.datasource.password=xxxx
server.tongweb.datasource.driver-class-name=com.mysql.jdbc.Driver
缺少
type配置时,将回退到 Spring Boot 默认数据源。
9.3 Hulk 数据源高级配置
properties
# 连接池
server.tongweb.datasource.hulk.max-pool-size=20
server.tongweb.datasource.hulk.min-idle=5
server.tongweb.datasource.hulk.connection-timeout=30000
server.tongweb.datasource.hulk.idle-timeout=600000
server.tongweb.datasource.hulk.max-lifetime=1800000
server.tongweb.datasource.hulk.validation-timeout=5000
# 泄漏检测
server.tongweb.datasource.hulk.remove-abandoned=true
server.tongweb.datasource.hulk.remove-abandoned-time=60
# 慢 SQL 监控
server.tongweb.datasource.hulk.slow-sql-enabled=true
server.tongweb.datasource.hulk.slow-sql-filter-time=1000
10. 类加载支持
10.1 类加载隔离
使用 TongWeb 定制 Maven 插件打包:
xml
<plugin>
<groupId>com.tongweb.springboot</groupId>
<artifactId>tongweb-spring-boot-maven-plugin</artifactId>
<version>7.0.E.{n}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
使用此插件后,启动命令从
mvn spring-boot:run改为mvn tongweb-spring-boot:run。
10.2 类冲突检查
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader</artifactId>
</dependency>
启动时自动检测不同版本的同名依赖并输出告警。
10.3 依赖缺失告警
默认启用。如需禁用,排除 tongweb-dependencies-check:
xml
<dependency>
<groupId>com.tongweb.springboot</groupId>
<artifactId>tongweb-spring-boot-starter-2.x</artifactId>
<exclusions>
<exclusion>
<groupId>com.tongweb</groupId>
<artifactId>tongweb-dependencies-check</artifactId>
</exclusion>
</exclusions>
</dependency>
11. 优雅停机
适用于 Spring Boot 2.3.0+
停机信号: Linux
kill -15 <pid>/ WindowsCtrl+C
properties
server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=30s
| 模式 | 说明 |
|---|---|
GRACEFUL |
处理完当前任务后停机 |
IMMEDIATE |
立即停机 |
12. 附录:核心配置参数速查
License 配置
| 配置项 | 说明 | 默认值 |
|---|---|---|
server.tongweb.license.type |
file 或 remote |
file |
server.tongweb.license.path |
license 文件路径 | classpath:license.dat |
server.tongweb.license.license-ips |
License Server 地址 | - |
server.tongweb.license.license-public-key |
请求加密公钥 | - |
Web 通用配置
| 配置项 | 说明 | 默认值 |
|---|---|---|
server.tongweb.max-threads |
最大工作线程数 | 200 |
server.tongweb.min-spare-threads |
最小空闲线程 | 10 |
server.tongweb.max-connections |
最大连接数 | 10000 |
server.tongweb.accept-count |
最大等待队列 | 100 |
server.tongweb.max-keep-alive-requests |
KeepAlive 最大请求数 | 100 |
server.tongweb.connection-timeout |
连接超时(ms) | 60000 |
server.tongweb.async-timeout |
异步请求超时(ms) | 30000 |
server.tongweb.max-http-post-size |
最大 POST 大小 | 2MB |
server.tongweb.uri-encoding |
URI 解码字符集 | UTF-8 |
server.tongweb.io-mode |
IO 模式 | NIO |
server.tongweb.tcp-no-delay |
TCP 无延迟 | true |
server.tongweb.processor-cache |
Processor 缓存数 | 200 |
性能优化参数
| 配置项 | 说明 | 默认值 |
|---|---|---|
server.compression.enabled |
响应压缩 | false |
server.compression.min-response-size |
压缩最小响应大小 | 2048 |
server.http2.enabled |
HTTP/2 | false |
server.tongweb.resource.allow-caching |
静态资源缓存 | true |
server.tongweb.resource.cache-max-size |
缓存最大值(KB) | 10240 |
server.tongweb.resource.cache-ttl |
缓存过期时间(ms) | 5000 |
说明:目的是学习和研究,若涉及版权问题,请联系删除