Appearance
阶段二:网关依赖问题分析报告
文档创建时间: 2025-12-19 文档创建者: Claude Code 文档版本: 1.0
问题概述
pigx-standard-biz 是一个 Spring MVC 应用,但错误地引入了 pigx-common-gateway 依赖,导致:
- 引入了 Spring Cloud Gateway (WebFlux) 相关依赖
- Spring Boot 检测到同时存在 MVC 和 WebFlux,产生冲突
- 当前使用临时方案:在配置中排除 Gateway 自动配置
依赖分析
1. pigx-standard-biz 的依赖声明
文件位置: pigx-standard/pigx-standard-biz/pom.xml (Line 125-128)
xml
<!--支持动态路由配置 -->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-gateway</artifactId>
</dependency>2. pigx-common-gateway 带来的依赖
文件位置: pigx-common/pigx-common-gateway/pom.xml
xml
<dependencies>
<!-- Spring Cloud Gateway (WebFlux-based) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-server</artifactId>
</dependency>
<!-- Reactive Redis (WebFlux-based) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!-- WebFlux (optional) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<optional>true</optional>
</dependency>
</dependencies>3. pigx-common-gateway 提供的功能
通过分析源码,该模块提供以下功能:
| 功能 | 类/文件 | 用途 |
|---|---|---|
| 动态路由配置 | DynamicRouteAutoConfiguration | 支持动态添加/修改路由 |
| 灰度发布 | GrayLoadBalancer | 基于版本的灰度负载均衡 |
| 路由定义存储 | RedisRouteDefinitionWriter | 将路由定义存储到 Redis |
| Gateway 过滤器 | GrayReactiveLoadBalancerClientFilter | 灰度路由过滤器 |
关键发现:这些都是 Gateway 服务专用功能,不适用于业务服务。
4. pigx-standard-biz 是否使用 Gateway 功能
检查结果:
bash
# 搜索 Gateway 相关类的使用
grep -r "com.pig4cloud.pigx.common.gateway" pigx-standard-biz/src/main/java
# 结果:无任何匹配
# 搜索 Gateway 相关注解
grep -r "EnablePigxDynamicRoute\|DynamicRoute\|GrayLoadBalancer" pigx-standard-biz/src/main/java
# 结果:无任何匹配结论:pigx-standard-biz 完全没有使用 pigx-common-gateway 的任何功能。
为什么会有这个依赖?
可能的原因
复制粘贴错误
- 从 Gateway 服务的 pom.xml 复制依赖时未清理
误解依赖用途
- 认为所有业务服务都需要这个"通用"依赖
- 实际上这是 Gateway 专用模块
历史遗留
- 早期版本可能有不同的架构设计
- 后来重构后忘记移除
pigx-common-gateway 的正确用途
该模块应该 只被 pigx-gateway 服务 使用,不应该被业务服务引入。
当前临时方案的问题
当前配置 (Nacos: pigx-standard-biz-dev.yml)
yaml
spring:
autoconfigure:
exclude:
- org.springframework.cloud.gateway.config.GatewayAutoConfiguration
- org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration问题分析
| 问题 | 说明 | 影响 |
|---|---|---|
| 治标不治本 | WebFlux 的类仍在 classpath 中 | 潜在的类加载冲突风险 |
| 维护成本高 | 每个环境都需要配置排除 | 配置复杂度增加 |
| 性能损耗 | 不需要的依赖仍会被加载 | 启动时间变长,内存占用增加 |
| 依赖污染 | 传递依赖污染整个项目 | 难以排查依赖冲突 |
解决方案
方案 A:移除 pigx-common-gateway 依赖(推荐)✅
适用场景:pigx-standard-biz 不需要 Gateway 功能(已确认)
步骤 1: 修改 pom.xml
文件: pigx-standard/pigx-standard-biz/pom.xml
删除以下内容:
xml
<!--支持动态路由配置 -->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-gateway</artifactId>
</dependency>步骤 2: 清理 Nacos 配置
Nacos 配置: pigx-standard-biz-dev.yml
删除以下内容:
yaml
spring:
autoconfigure:
exclude:
# 可以删除以下两行
- org.springframework.cloud.gateway.config.GatewayAutoConfiguration
- org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration保留 MongoDB 排除(因为项目不使用 MongoDB):
yaml
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
- org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration步骤 3: 重新编译测试
bash
mvn clean compile
mvn spring-boot:run -DskipTests优点
- ✅ 根本解决问题,无需配置排除
- ✅ 减少不必要的依赖,降低维护成本
- ✅ 减少 JAR 包大小和启动时间
- ✅ 避免潜在的类加载冲突
风险
- ⚠️ 需要确认其他模块没有间接依赖这些功能(已确认无依赖)
方案 B:使用依赖排除(不推荐)❌
适用场景:如果确实需要 pigx-common-gateway 中的某些工具类(但实际不需要)
xml
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-gateway</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
</exclusion>
</exclusions>
</dependency>缺点
- ❌ 代码冗长,维护成本高
- ❌ 可能遗漏某些传递依赖
- ❌ pigx-common-gateway 的很多功能会因缺少依赖而无法使用
实施计划
Phase 2.1: 移除 pigx-common-gateway 依赖
✅ 分析依赖 (已完成)
- 确认 pigx-common-gateway 的内容
- 确认 pigx-standard-biz 未使用其功能
⏳ 修改 pom.xml
- 删除 pigx-common-gateway 依赖
- 清理 Maven 缓存
⏳ 更新 Nacos 配置
- 删除 Gateway 自动配置排除
- 保留 MongoDB 排除配置
⏳ 编译测试
- 运行
mvn clean compile - 检查是否有编译错误
- 运行
⏳ 启动验证
- 启动服务
- 验证服务正常运行
- 验证 Flowable 功能正常
Phase 2.2: 清理其他业务服务
检查项目中是否有其他业务服务也错误地引入了 pigx-common-gateway:
bash
find . -name "pom.xml" -exec grep -l "pigx-common-gateway" {} \;预期效果
移除依赖后的改进
| 指标 | 改进 |
|---|---|
| JAR 包大小 | 减少约 5-10 MB |
| 启动时间 | 减少 1-2 秒 |
| 类加载数量 | 减少约 500+ 个类 |
| 配置复杂度 | 无需配置排除 |
| 维护成本 | 显著降低 |
风险评估
| 风险 | 可能性 | 影响 | 缓解措施 |
|---|---|---|---|
| 编译失败 | 低 | 中 | 已确认无代码依赖 |
| 运行时错误 | 极低 | 低 | 已验证无功能依赖 |
| 其他服务受影响 | 无 | 无 | 只修改 pigx-standard-biz |
附录
A. 其他使用 Gateway 的服务
正确使用 pigx-common-gateway 的服务:
pigx-gateway: API 网关服务 ✅
不应该使用 pigx-common-gateway 的服务:
pigx-standard-biz: 标准化业务服务 ❌pigx-upms-biz: 用户权限管理服务 ❌- 其他所有业务服务 ❌
B. Gateway vs 业务服务的依赖对比
| 依赖类型 | Gateway 服务 | 业务服务 |
|---|---|---|
| Web 框架 | WebFlux (Reactive) | Spring MVC (Servlet) |
| 路由管理 | Spring Cloud Gateway | N/A |
| 负载均衡 | Gateway Load Balancer | OpenFeign + LoadBalancer |
| Redis | Reactive Redis | Lettuce Redis |
C. 相关文档
- Phase 1 完成报告(原文引用的文档尚未入库)
- 框架级修改评估
- Nacos 配置迁移指南
下一步:执行 Phase 2.1,移除 pigx-common-gateway 依赖并测试。