图片预览

滚轮缩放 · 拖动移动 · Esc 关闭

Skip to content

阶段二:网关依赖问题分析报告 ​

文档创建时间: 2025-12-19 文档创建者: Claude Code 文档版本: 1.0


问题概述 ​

pigx-standard-biz 是一个 Spring MVC 应用,但错误地引入了 pigx-common-gateway 依赖,导致:

  1. 引入了 Spring Cloud Gateway (WebFlux) 相关依赖
  2. Spring Boot 检测到同时存在 MVC 和 WebFlux,产生冲突
  3. 当前使用临时方案:在配置中排除 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 的任何功能。


为什么会有这个依赖? ​

可能的原因 ​

  1. 复制粘贴错误

    • 从 Gateway 服务的 pom.xml 复制依赖时未清理
  2. 误解依赖用途

    • 认为所有业务服务都需要这个"通用"依赖
    • 实际上这是 Gateway 专用模块
  3. 历史遗留

    • 早期版本可能有不同的架构设计
    • 后来重构后忘记移除

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 依赖 ​

  1. ✅ 分析依赖 (已完成)

    • 确认 pigx-common-gateway 的内容
    • 确认 pigx-standard-biz 未使用其功能
  2. ⏳ 修改 pom.xml

    • 删除 pigx-common-gateway 依赖
    • 清理 Maven 缓存
  3. ⏳ 更新 Nacos 配置

    • 删除 Gateway 自动配置排除
    • 保留 MongoDB 排除配置
  4. ⏳ 编译测试

    • 运行 mvn clean compile
    • 检查是否有编译错误
  5. ⏳ 启动验证

    • 启动服务
    • 验证服务正常运行
    • 验证 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 GatewayN/A
负载均衡Gateway Load BalancerOpenFeign + LoadBalancer
RedisReactive RedisLettuce Redis

C. 相关文档 ​


下一步:执行 Phase 2.1,移除 pigx-common-gateway 依赖并测试。