aidmt-ms/docs/gateway-routes-config-fix.md

3.7 KiB
Raw Blame History

网关路由配置修复说明

问题分析

您的网关配置中已经包含了 ruoyi-systemruoyi-manage 的路由,但是 缺少 Swagger 文档的专门路由

当前路由配置

  • ruoyi-system/system/**
  • ruoyi-manage/manage/**

Swagger 文档访问路径

  • ruoyi-system Swagger → /ruoyi-system/v3/api-docs
  • ruoyi-manage Swagger → /ruoyi-manage/v3/api-docs

问题/ruoyi-system/v3/api-docs/ruoyi-manage/v3/api-docs 无法匹配现有的路由规则!

解决方案

在 Nacos 配置中心的 ruoyi-gateway-dev.yml 文件中,添加 Swagger 文档的专门路由:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
      routes:
        # 认证中心
        - id: ruoyi-auth
          uri: lb://ruoyi-auth
          predicates:
            - Path=/auth/**
          filters:
            - CacheRequestBody
            - ValidateCodeFilter
            - StripPrefix=1
        # 代码生成
        - id: ruoyi-gen
          uri: lb://ruoyi-gen
          predicates:
            - Path=/code/**
          filters:
            - StripPrefix=1
        # 定时任务
        - id: ruoyi-job
          uri: lb://ruoyi-job
          predicates:
            - Path=/schedule/**
          filters:
            - StripPrefix=1
        # 系统模块
        - id: ruoyi-system
          uri: lb://ruoyi-system
          predicates:
            - Path=/system/**
          filters:
            - StripPrefix=1
        # 系统模块 Swagger 文档(新增)
        - id: ruoyi-system-swagger
          uri: lb://ruoyi-system
          predicates:
            - Path=/ruoyi-system/v3/api-docs/**
          filters:
            - StripPrefix=1
        # 文件服务
        - id: ruoyi-file
          uri: lb://ruoyi-file
          predicates:
            - Path=/file/**
          filters:
            - StripPrefix=1
        # 物业施工作业管理系统
        - id: ruoyi-manage
          uri: lb://ruoyi-manage
          predicates:
            - Path=/manage/**
          filters:
            - StripPrefix=1
        # 物业施工作业管理系统 Swagger 文档(新增)
        - id: ruoyi-manage-swagger
          uri: lb://ruoyi-manage
          predicates:
            - Path=/ruoyi-manage/v3/api-docs/**
          filters:
            - StripPrefix=1

操作步骤

  1. 登录 Nacos 控制台:http://127.0.0.1:8848/nacos
  2. 进入"配置管理" -> "配置列表"
  3. 找到 ruoyi-gateway-dev.yml 配置文件
  4. routes 列表中添加上述两个 Swagger 路由配置
  5. 点击"发布"保存配置
  6. 重启网关服务ruoyi-gateway

验证

配置完成后,访问以下 URL 验证:

  1. Swagger 文档

    • http://localhost:8080/ruoyi-system/v3/api-docs - 应该返回系统模块的 API 文档 JSON
    • http://localhost:8080/ruoyi-manage/v3/api-docs - 应该返回管理模块的 API 文档 JSON
  2. Swagger UI

    • http://localhost:8080/swagger-ui.htmlhttp://localhost:8080/doc.html
    • 在服务下拉列表中应该能看到 ruoyi-systemruoyi-manage
    • 选择服务后应该能看到"用户管理"和"部门管理"的接口
  3. 业务接口

    • http://localhost:8080/system/user/list - 用户列表
    • http://localhost:8080/system/dept/list - 部门列表

注意事项

  • 路由配置修改后必须重启网关服务才能生效
  • StripPrefix=1 表示去掉路径的第一段(如 /ruoyi-system/v3/api-docs 会变成 /v3/api-docs 转发到后端服务)
  • Swagger 文档路径已经在白名单中(/*/v3/api-docs),不需要认证即可访问