zhangjf 330ec6dea9 refactor: 简化多租户路由配置,基于 Nacos 元数据动态匹配
问题:tenant.routing.services 配置在每个服务中重复定义 vip-tenants

解决方案:
1. TenantRoutingProperties 简化
   - 移除 services 映射(vip-tenants 列表)
   - 保留全局配置:enabled, fallback-to-shared, shared-services
   - 路由逻辑改为基于实例元数据动态匹配

2. 配置简化
   - Gateway: 只需全局配置,无需定义各服务的 vip-tenants
   - 服务实例: 只需在 Nacos metadata 中声明 tenant-group
   - 负载均衡器: 从实例 metadata 读取 tenant-group 进行匹配

3. 架构变化
   修改前:配置文件定义 vip-tenants 列表
   修改后:实例注册时声明 tenant-group,负载均衡器动态匹配

示例:
  共享实例 metadata: { tenant-group: "" }
  VIP 实例 metadata: { tenant-group: "TENANT_VIP_001" }
  请求匹配 → 路由到对应实例
2026-02-19 21:18:58 +08:00

146 lines
3.3 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

server:
port: 8000
spring:
application:
name: fund-gateway
# Redis配置(用于限流)
data:
redis:
host: localhost
port: 6379
password: zjf@123456
database: 1
cloud:
compatibility-verifier:
enabled: false
# Nacos 服务发现
nacos:
discovery:
server-addr: localhost:8848
namespace: fund-platform
group: DEFAULT_GROUP
username: nacos
password: nacos
# Sentinel配置
sentinel:
transport:
dashboard: localhost:8080 # Sentinel Dashboard地址(可选)
port: 8719 # Sentinel客户端端口
eager: true # 服务启动时立即初始化
# 负载均衡配置
loadbalancer:
enabled: true
cache:
enabled: false # 开发环境禁用缓存便于调试
gateway:
# 默认限流配置
default-filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 100 # 每秒补充令牌数
redis-rate-limiter.burstCapacity: 200 # 令牌桶最大容量
key-resolver: "#{@ipKeyResolver}"
# 全局跨域配置
globalcors:
cors-configurations:
'[/**]':
allowedOriginPatterns: "*"
allowedMethods: "*"
allowedHeaders: "*"
allowCredentials: true
maxAge: 3600
routes:
# 系统管理服务 (使用负载均衡)
- id: fund-sys
uri: lb://fund-sys
predicates:
- Path=/sys/**
filters:
- StripPrefix=1
# 客户管理服务
- id: fund-cust
uri: lb://fund-cust
predicates:
- Path=/cust/**
filters:
- StripPrefix=1
# 项目管理服务
- id: fund-proj
uri: lb://fund-proj
predicates:
- Path=/proj/**
filters:
- StripPrefix=1
# 用款申请服务
- id: fund-req
uri: lb://fund-req
predicates:
- Path=/req/**
filters:
- StripPrefix=1
# 支出管理服务
- id: fund-exp
uri: lb://fund-exp
predicates:
- Path=/exp/**
filters:
- StripPrefix=1
# 收款管理服务
- id: fund-receipt
uri: lb://fund-receipt
predicates:
- Path=/receipt/**
filters:
- StripPrefix=1
# 报表服务
- id: fund-report
uri: lb://fund-report
predicates:
- Path=/report/**
filters:
- StripPrefix=1
# 文件服务
- id: fund-file
uri: lb://fund-file
predicates:
- Path=/file/**
filters:
- StripPrefix=1
logging:
level:
org.springframework.cloud.gateway: DEBUG
com.fundplatform.common.loadbalancer: DEBUG
# 多租户路由配置Gateway 全局配置)
tenant:
routing:
enabled: true
tenant-header: X-Tenant-Id
tenant-group-header: X-Tenant-Group
group-separator: TENANT_
default-tenant-id: "1"
# 共享服务列表(不需要租户路由的服务)
shared-services:
- fund-gateway
- fund-report
- fund-file
# 默认回退策略
fallback-to-shared: true