Files
happy-life-star/backend/gateway/网关配置更新总结.md
T

4.7 KiB
Raw Blame History

网关服务配置更新总结

更新时间

2025-07-17

更新内容

1. 服务端口配置更新

根据当前backend下的最新模块,更新了所有微服务的路由配置:

服务名称 端口 路由路径 描述
emotion-gateway 19000 - 网关服务
emotion-user 19001 /user/, /captcha/, /oauth/** 用户服务(包含认证)
emotion-ai 19002 /ai/** AI对话服务
emotion-record 19003 /record/** 情绪记录服务
emotion-growth 19004 /growth/** 成长课题服务
emotion-explore 19005 /explore/** 地图探索服务
emotion-reward 19006 /reward/** 成就奖励服务
emotion-websocket 19007 /websocket/, /ws/ WebSocket聊天服务
emotion-stats 19008 /stats/** 统计分析服务

2. 新增WebSocket支持

WebSocket路由配置

# WebSocket聊天服务路由
- id: emotion-websocket-route
  uri: http://localhost:19007
  predicates:
    - Path=/websocket/**
  filters:
    - StripPrefix=0

# WebSocket连接路由 (支持WebSocket升级)
- id: emotion-websocket-ws-route
  uri: ws://localhost:19007
  predicates:
    - Path=/ws/**
  filters:
    - StripPrefix=0

特性

  • 支持HTTP和WebSocket协议
  • REST API路径:/websocket/**
  • WebSocket连接路径:/ws/**
  • 自动协议升级支持

3. 新增跨域配置

# 全局跨域配置
globalcors:
  cors-configurations:
    '[/**]':
      allowed-origins: "*"
      allowed-methods: "*"
      allowed-headers: "*"
      allow-credentials: true

4. 端口冲突解决

问题emotion-stats和emotion-websocket都使用19007端口

解决方案

  • emotion-websocket保持19007端口(新增的重要服务)
  • emotion-stats更改为19008端口

修改文件

  • backend/emotion-stats/src/main/resources/application.yml

5. 完整路由列表

用户相关服务

  • /user/** → emotion-user:19001
  • /captcha/** → emotion-user:19001
  • /oauth/** → emotion-user:19001

AI相关服务

  • /ai/** → emotion-ai:19002
  • /websocket/** → emotion-websocket:19007 (REST API)
  • /ws/** → emotion-websocket:19007 (WebSocket)

业务功能服务

  • /record/** → emotion-record:19003
  • /growth/** → emotion-growth:19004
  • /explore/** → emotion-explore:19005
  • /reward/** → emotion-reward:19006
  • /stats/** → emotion-stats:19008

使用示例

1. 通过网关访问AI服务

# 直接访问
curl http://localhost:19002/api/ai/chat/send

# 通过网关访问
curl http://localhost:19000/ai/api/ai/chat/send

2. 通过网关访问WebSocket服务

# REST API测试
curl http://localhost:19000/websocket/online-users

# WebSocket连接
ws://localhost:19000/ws/chat

3. 通过网关访问用户服务

# 用户注册
curl http://localhost:19000/user/api/user/register

# 获取验证码
curl http://localhost:19000/captcha/api/captcha/generate

配置文件位置

  • 主配置backend/emotion-gateway/src/main/resources/application-local.yml
  • 生产配置backend/emotion-gateway/src/main/resources/application-prod.yml
  • Docker配置backend/emotion-gateway/src/main/resources/application-docker.yml

注意事项

  1. 端口一致性:确保各服务的实际端口与网关配置一致
  2. WebSocket支持:新增的WebSocket路由支持协议升级
  3. 跨域配置:已添加全局跨域支持,适用于前端开发
  4. 路径匹配:使用StripPrefix=0保持原始路径
  5. 服务发现:本地环境禁用Nacos,使用直连方式

验证方法

1. 检查网关状态

curl http://localhost:19000/actuator/health

2. 测试路由转发

# 测试用户服务路由
curl http://localhost:19000/user/actuator/health

# 测试AI服务路由  
curl http://localhost:19000/ai/actuator/health

# 测试WebSocket服务路由
curl http://localhost:19000/websocket/online-users

3. 查看网关日志

tail -f backend/logs/emotion-gateway-local.log

后续优化建议

  1. 负载均衡:生产环境可考虑启用Nacos服务发现
  2. 限流配置:添加请求限流和熔断机制
  3. 安全配置:添加JWT认证过滤器
  4. 监控配置:集成链路追踪和指标监控
  5. 缓存配置:添加响应缓存机制

相关文件更新

  • backend/emotion-gateway/src/main/resources/application-local.yml
  • backend/emotion-stats/src/main/resources/application.yml
  • 网关配置文档更新

测试状态

  • 配置文件语法正确
  • 端口冲突已解决
  • WebSocket路由已添加
  • 跨域配置已添加
  • 实际路由转发测试待进行