feat: 完成Nacos配置优化和WebSocket集成

主要更新:
1. 统一所有微服务端口配置(19000-19008)
2. 为所有服务创建本地/测试/生产三套环境配置
3. 配置Nacos认证密码(本地:Peanut2817*#, 测试/生产:EmotionMuseum2025)
4. 优化网关路由配置,支持负载均衡和WebSocket
5. 新增emotion-websocket模块,支持实时聊天
6. 前端集成WebSocket,替代HTTP轮询
7. 添加配置验证和管理工具脚本

技术特性:
- 完整的环境隔离和服务发现
- WebSocket实时通信支持
- 负载均衡路由配置
- 跨域和安全配置
- 自动重连和心跳检测
This commit is contained in:
2025-07-17 18:10:45 +08:00
parent 9a3a8267b5
commit c77352877d
391 changed files with 46585 additions and 4294 deletions
@@ -0,0 +1,52 @@
2025-07-17T09:52:07.419+08:00 WARN 90872 --- [restartedMain] c.a.nacos.client.logging.NacosLogging : Load Logback Configuration of Nacos fail, message: Could not initialize Logback Nacos logging from classpath:nacos-logback.xml
2025-07-17T09:52:07.481+08:00 WARN 90872 --- [restartedMain] c.a.nacos.client.logging.NacosLogging : Load Logback Configuration of Nacos fail, message: Could not initialize Logback Nacos logging from classpath:nacos-logback.xml
2025-07-17T09:52:07.487+08:00 INFO 90872 --- [restartedMain] c.e.gateway.GatewayApplication : Starting GatewayApplication using Java 23.0.2 with PID 90872 (/Users/huazhongmin/peanut/AppleDevelop/EmotionMuseum/backend/emotion-gateway/target/classes started by huazhongmin in /Users/huazhongmin/peanut/AppleDevelop/EmotionMuseum/backend/emotion-gateway)
2025-07-17T09:52:07.488+08:00 DEBUG 90872 --- [restartedMain] c.e.gateway.GatewayApplication : Running with Spring Boot v3.0.2, Spring v6.0.4
2025-07-17T09:52:07.488+08:00 INFO 90872 --- [restartedMain] c.e.gateway.GatewayApplication : The following 1 profile is active: "local"
2025-07-17T09:52:07.549+08:00 INFO 90872 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2025-07-17T09:52:07.550+08:00 INFO 90872 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2025-07-17T09:52:08.606+08:00 INFO 90872 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2025-07-17T09:52:08.608+08:00 INFO 90872 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2025-07-17T09:52:08.627+08:00 INFO 90872 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 Redis repository interfaces.
2025-07-17T09:52:08.798+08:00 INFO 90872 --- [restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=5a741552-34ee-38c0-936b-699d9d791837
2025-07-17T09:52:09.026+08:00 INFO 90872 --- [restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2025-07-17T09:52:09.027+08:00 INFO 90872 --- [restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactorDeferringLoadBalancerFilterConfig' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactorDeferringLoadBalancerFilterConfig] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2025-07-17T09:52:09.028+08:00 INFO 90872 --- [restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'reactorDeferringLoadBalancerExchangeFilterFunction' of type [org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2025-07-17T09:52:10.147+08:00 DEBUG 90872 --- [restartedMain] o.s.c.gateway.config.GatewayProperties : Routes supplied from Gateway Properties: [RouteDefinition{id='emotion-user-route', predicates=[PredicateDefinition{name='Path', args={_genkey_0=/user/**}}], filters=[FilterDefinition{name='StripPrefix', args={_genkey_0=0}}], uri=http://localhost:19001, order=0, metadata={}}, RouteDefinition{id='emotion-captcha-route', predicates=[PredicateDefinition{name='Path', args={_genkey_0=/captcha/**}}], filters=[FilterDefinition{name='StripPrefix', args={_genkey_0=0}}], uri=http://localhost:19001, order=0, metadata={}}, RouteDefinition{id='emotion-oauth-route', predicates=[PredicateDefinition{name='Path', args={_genkey_0=/oauth/**}}], filters=[FilterDefinition{name='StripPrefix', args={_genkey_0=0}}], uri=http://localhost:19001, order=0, metadata={}}, RouteDefinition{id='emotion-ai-route', predicates=[PredicateDefinition{name='Path', args={_genkey_0=/ai/**}}], filters=[FilterDefinition{name='StripPrefix', args={_genkey_0=0}}], uri=http://localhost:19002, order=0, metadata={}}]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [After]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Before]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Between]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Cookie]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Header]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Host]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Method]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Path]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Query]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [ReadBody]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [RemoteAddr]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [XForwardedRemoteAddr]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight]
2025-07-17T09:52:10.329+08:00 INFO 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [CloudFoundryRouteService]
2025-07-17T09:52:10.352+08:00 INFO 90872 --- [restartedMain] c.a.c.s.g.s.SentinelSCGAutoConfiguration : [Sentinel SpringCloudGateway] register SentinelGatewayFilter with order: -2147483648
2025-07-17T09:52:10.417+08:00 DEBUG 90872 --- [restartedMain] o.s.w.r.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2025-07-17T09:52:10.524+08:00 INFO 90872 --- [restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2025-07-17T09:52:10.558+08:00 DEBUG 90872 --- [restartedMain] o.s.w.r.r.m.a.ControllerMethodResolver : ControllerAdvice beans: none
2025-07-17T09:52:10.591+08:00 INFO 90872 --- [restartedMain] c.a.c.s.g.s.SentinelSCGAutoConfiguration : [Sentinel SpringCloudGateway] register SentinelGatewayBlockExceptionHandler
2025-07-17T09:52:10.595+08:00 DEBUG 90872 --- [restartedMain] o.s.w.s.adapter.HttpWebHandlerAdapter : enableLoggingRequestDetails='false': form data and headers will be masked to prevent unsafe logging of potentially sensitive data
2025-07-17T09:52:10.746+08:00 INFO 90872 --- [restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2025-07-17T09:52:10.770+08:00 WARN 90872 --- [restartedMain] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
2025-07-17T09:52:10.851+08:00 INFO 90872 --- [restartedMain] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port 19000
2025-07-17T09:52:10.881+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition emotion-user-route applying {_genkey_0=/user/**} to Path
2025-07-17T09:52:10.894+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition emotion-user-route applying filter {_genkey_0=0} to StripPrefix
2025-07-17T09:52:10.898+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: emotion-user-route
2025-07-17T09:52:10.898+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition emotion-captcha-route applying {_genkey_0=/captcha/**} to Path
2025-07-17T09:52:10.899+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition emotion-captcha-route applying filter {_genkey_0=0} to StripPrefix
2025-07-17T09:52:10.900+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: emotion-captcha-route
2025-07-17T09:52:10.900+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition emotion-oauth-route applying {_genkey_0=/oauth/**} to Path
2025-07-17T09:52:10.900+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition emotion-oauth-route applying filter {_genkey_0=0} to StripPrefix
2025-07-17T09:52:10.901+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: emotion-oauth-route
2025-07-17T09:52:10.901+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition emotion-ai-route applying {_genkey_0=/ai/**} to Path
2025-07-17T09:52:10.902+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition emotion-ai-route applying filter {_genkey_0=0} to StripPrefix
2025-07-17T09:52:10.902+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: emotion-ai-route
2025-07-17T09:52:10.906+08:00 DEBUG 90872 --- [restartedMain] o.s.c.g.filter.GatewayMetricsFilter : New routes count: 4
2025-07-17T09:52:10.909+08:00 INFO 90872 --- [restartedMain] c.e.gateway.GatewayApplication : Started GatewayApplication in 3.87 seconds (process running for 4.294)
+11
View File
@@ -94,6 +94,17 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.emotionmuseum.gateway.GatewayApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
@@ -1,106 +1,5 @@
server:
port: 19000
# 本地开发环境配置
spring:
application:
name: emotion-gateway
# Redis配置
data:
redis:
host: localhost
port: 6379
password:
database: 0
timeout: 10000ms
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
# 网关配置
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
# 用户服务路由
- id: emotion-user-route
uri: http://localhost:19001
predicates:
- Path=/user/**
filters:
- StripPrefix=0
# 验证码服务路由
- id: emotion-captcha-route
uri: http://localhost:19001
predicates:
- Path=/captcha/**
filters:
- StripPrefix=0
# OAuth服务路由
- id: emotion-oauth-route
uri: http://localhost:19001
predicates:
- Path=/oauth/**
filters:
- StripPrefix=0
# AI服务路由
- id: emotion-ai-route
uri: http://localhost:19002
predicates:
- Path=/ai/**
filters:
- StripPrefix=0
# 记录服务路由
- id: emotion-record-route
uri: http://localhost:19003
predicates:
- Path=/record/**
filters:
- StripPrefix=0
# 成长服务路由
- id: emotion-growth-route
uri: http://localhost:19004
predicates:
- Path=/growth/**
filters:
- StripPrefix=0
# 探索服务路由
- id: emotion-explore-route
uri: http://localhost:19005
predicates:
- Path=/explore/**
filters:
- StripPrefix=0
# 奖励服务路由
- id: emotion-reward-route
uri: http://localhost:19006
predicates:
- Path=/reward/**
filters:
- StripPrefix=0
# 统计服务路由
- id: emotion-stats-route
uri: http://localhost:19007
predicates:
- Path=/stats/**
filters:
- StripPrefix=0
# Nacos配置
spring:
cloud:
nacos:
@@ -109,7 +8,16 @@ spring:
namespace:
group: DEFAULT_GROUP
enabled: true
username: nacos
password: Peanut2817*#
metadata:
version: 1.0.0
zone: local
register-enabled: true
ephemeral: true
cluster-name: DEFAULT
service: ${spring.application.name}
weight: 1
heart-beat-interval: 5000
heart-beat-timeout: 15000
ip-delete-timeout: 30000
@@ -119,6 +27,118 @@ spring:
group: DEFAULT_GROUP
file-extension: yml
enabled: false
username: nacos
password: Peanut2817*#
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
# 全局跨域配置
globalcors:
cors-configurations:
'[/**]':
allowed-origins: "*"
allowed-methods: "*"
allowed-headers: "*"
allow-credentials: true
routes:
# 用户服务路由 (包含认证功能)
- id: emotion-user-route
uri: lb://emotion-user
predicates:
- Path=/user/**
filters:
- StripPrefix=0
# 验证码服务路由
- id: emotion-captcha-route
uri: lb://emotion-user
predicates:
- Path=/captcha/**
filters:
- StripPrefix=0
# OAuth服务路由
- id: emotion-oauth-route
uri: lb://emotion-user
predicates:
- Path=/oauth/**
filters:
- StripPrefix=0
# AI服务路由
- id: emotion-ai-route
uri: lb://emotion-ai
predicates:
- Path=/ai/**
filters:
- StripPrefix=0
# WebSocket聊天服务路由
- id: emotion-websocket-route
uri: lb://emotion-websocket
predicates:
- Path=/websocket/**
filters:
- StripPrefix=0
# WebSocket连接路由 (支持WebSocket升级)
- id: emotion-websocket-ws-route
uri: lb://emotion-websocket
predicates:
- Path=/ws/**
filters:
- StripPrefix=0
# 情绪记录服务路由
- id: emotion-record-route
uri: lb://emotion-record
predicates:
- Path=/record/**
filters:
- StripPrefix=0
# 成长课题服务路由
- id: emotion-growth-route
uri: lb://emotion-growth
predicates:
- Path=/growth/**
filters:
- StripPrefix=0
# 地图探索服务路由
- id: emotion-explore-route
uri: lb://emotion-explore
predicates:
- Path=/explore/**
filters:
- StripPrefix=0
# 成就奖励服务路由
- id: emotion-reward-route
uri: lb://emotion-reward
predicates:
- Path=/reward/**
filters:
- StripPrefix=0
# 统计分析服务路由
- id: emotion-stats-route
uri: lb://emotion-stats
predicates:
- Path=/stats/**
filters:
- StripPrefix=0
# Redis配置
data:
redis:
host: localhost
port: 6379
password:
database: 0
# 日志配置
logging:
@@ -126,20 +146,5 @@ logging:
com.emotionmuseum: debug
org.springframework.cloud.gateway: debug
org.springframework.web: debug
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n"
file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n"
file:
name: logs/emotion-gateway-local.log
# 管理端点配置
management:
endpoints:
web:
exposure:
include: health,info,metrics,gateway
endpoint:
health:
show-details: always
gateway:
enabled: true
name: logs/emotion-gateway-local.log
@@ -1,105 +1,151 @@
server:
port: 9000
# 生产环境配置
spring:
application:
name: emotion-gateway
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
- com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration
data:
redis:
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}
database: 0
timeout: 3000ms
lettuce:
pool:
max-active: 20
max-idle: 10
min-idle: 5
max-wait: 3000ms
cloud:
nacos:
discovery:
server-addr: ${NACOS_SERVER_ADDR:localhost:8848}
namespace: public
server-addr: 47.111.10.27:8848
namespace: prod
group: DEFAULT_GROUP
enabled: true
ip: ${SERVER_IP:localhost}
username: nacos
password: EmotionMuseum2025
metadata:
version: 1.0.0
environment: prod
zone: prod
register-enabled: true
ephemeral: true
cluster-name: DEFAULT
service: ${spring.application.name}
weight: 1
heart-beat-interval: 5000
heart-beat-timeout: 15000
ip-delete-timeout: 30000
config:
server-addr: 47.111.10.27:8848
namespace: prod
group: DEFAULT_GROUP
file-extension: yml
enabled: false
username: nacos
password: EmotionMuseum2025
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
# 全局跨域配置
globalcors:
cors-configurations:
'[/**]':
allowed-origins: "*"
allowed-methods: "*"
allowed-headers: "*"
allow-credentials: true
routes:
# 用户服务路由
- id: emotion-user
# 用户服务路由 (包含认证功能)
- id: emotion-user-route
uri: lb://emotion-user
predicates:
- Path=/api/user/**
- Path=/user/**
filters:
- StripPrefix=0
# 验证码服务路由
- id: emotion-captcha-route
uri: lb://emotion-user
predicates:
- Path=/captcha/**
filters:
- StripPrefix=0
# OAuth服务路由
- id: emotion-oauth-route
uri: lb://emotion-user
predicates:
- Path=/oauth/**
filters:
- StripPrefix=0
# AI服务路由
- id: emotion-ai
- id: emotion-ai-route
uri: lb://emotion-ai
predicates:
- Path=/api/ai/**
# 记录服务路由
- id: emotion-record
- Path=/ai/**
filters:
- StripPrefix=0
# WebSocket聊天服务路由
- id: emotion-websocket-route
uri: lb://emotion-websocket
predicates:
- Path=/websocket/**
filters:
- StripPrefix=0
# WebSocket连接路由 (支持WebSocket升级)
- id: emotion-websocket-ws-route
uri: lb://emotion-websocket
predicates:
- Path=/ws/**
filters:
- StripPrefix=0
# 情绪记录服务路由
- id: emotion-record-route
uri: lb://emotion-record
predicates:
- Path=/api/record/**
- Path=/record/**
filters:
- StripPrefix=1
# 成长服务路由
- id: emotion-growth
- StripPrefix=0
# 成长课题服务路由
- id: emotion-growth-route
uri: lb://emotion-growth
predicates:
- Path=/api/growth/**
- Path=/growth/**
filters:
- StripPrefix=1
# 探索服务路由
- id: emotion-explore
- StripPrefix=0
# 地图探索服务路由
- id: emotion-explore-route
uri: lb://emotion-explore
predicates:
- Path=/api/explore/**
- Path=/explore/**
filters:
- StripPrefix=1
# 奖励服务路由
- id: emotion-reward
- StripPrefix=0
# 成就奖励服务路由
- id: emotion-reward-route
uri: lb://emotion-reward
predicates:
- Path=/api/reward/**
- Path=/reward/**
filters:
- StripPrefix=1
# 统计服务路由
- id: emotion-stats
- StripPrefix=0
# 统计分析服务路由
- id: emotion-stats-route
uri: lb://emotion-stats
predicates:
- Path=/api/stats/**
- Path=/stats/**
filters:
- StripPrefix=1
- StripPrefix=0
# Redis配置
data:
redis:
host: 47.111.10.27
port: 6379
password: EmotionMuseum2025*#
database: 0
# 日志配置
logging:
level:
com.emotionmuseum: INFO
org.springframework.cloud.gateway: DEBUG
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
# 管理端点
management:
endpoints:
web:
exposure:
include: health,info,gateway
endpoint:
health:
show-details: always
com.emotionmuseum: warn
org.springframework.cloud.gateway: warn
org.springframework.web: warn
com.alibaba.nacos: error
file:
name: logs/emotion-gateway-prod.log
@@ -0,0 +1,151 @@
# 测试环境配置
spring:
cloud:
nacos:
discovery:
server-addr: 47.111.10.27:8848
namespace: test
group: DEFAULT_GROUP
enabled: true
username: nacos
password: EmotionMuseum2025
metadata:
version: 1.0.0
zone: test
register-enabled: true
ephemeral: true
cluster-name: DEFAULT
service: ${spring.application.name}
weight: 1
heart-beat-interval: 5000
heart-beat-timeout: 15000
ip-delete-timeout: 30000
config:
server-addr: 47.111.10.27:8848
namespace: test
group: DEFAULT_GROUP
file-extension: yml
enabled: false
username: nacos
password: EmotionMuseum2025
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
# 全局跨域配置
globalcors:
cors-configurations:
"[/**]":
allowed-origins: "*"
allowed-methods: "*"
allowed-headers: "*"
allow-credentials: true
routes:
# 用户服务路由 (包含认证功能)
- id: emotion-user-route
uri: lb://emotion-user
predicates:
- Path=/user/**
filters:
- StripPrefix=0
# 验证码服务路由
- id: emotion-captcha-route
uri: lb://emotion-user
predicates:
- Path=/captcha/**
filters:
- StripPrefix=0
# OAuth服务路由
- id: emotion-oauth-route
uri: lb://emotion-user
predicates:
- Path=/oauth/**
filters:
- StripPrefix=0
# AI服务路由
- id: emotion-ai-route
uri: lb://emotion-ai
predicates:
- Path=/ai/**
filters:
- StripPrefix=0
# WebSocket聊天服务路由
- id: emotion-websocket-route
uri: lb://emotion-websocket
predicates:
- Path=/websocket/**
filters:
- StripPrefix=0
# WebSocket连接路由 (支持WebSocket升级)
- id: emotion-websocket-ws-route
uri: lb://emotion-websocket
predicates:
- Path=/ws/**
filters:
- StripPrefix=0
# 情绪记录服务路由
- id: emotion-record-route
uri: lb://emotion-record
predicates:
- Path=/record/**
filters:
- StripPrefix=0
# 成长课题服务路由
- id: emotion-growth-route
uri: lb://emotion-growth
predicates:
- Path=/growth/**
filters:
- StripPrefix=0
# 地图探索服务路由
- id: emotion-explore-route
uri: lb://emotion-explore
predicates:
- Path=/explore/**
filters:
- StripPrefix=0
# 成就奖励服务路由
- id: emotion-reward-route
uri: lb://emotion-reward
predicates:
- Path=/reward/**
filters:
- StripPrefix=0
# 统计分析服务路由
- id: emotion-stats-route
uri: lb://emotion-stats
predicates:
- Path=/stats/**
filters:
- StripPrefix=0
# Redis配置
data:
redis:
host: 47.111.10.27
port: 6379
password: EmotionMuseum2025*#
database: 0
# 日志配置
logging:
level:
com.emotionmuseum: info
org.springframework.cloud.gateway: info
org.springframework.web: info
com.alibaba.nacos: warn
file:
name: logs/emotion-gateway-test.log
@@ -4,40 +4,72 @@ server:
spring:
application:
name: emotion-gateway
# 配置文件激活
profiles:
active: dev
active: ${SPRING_PROFILES_ACTIVE:local}
# 允许Bean覆盖和循环引用
main:
allow-bean-definition-overriding: true
allow-circular-references: true
# 排除数据库自动配置
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
- com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration
# Redis配置
data:
redis:
host: localhost
port: 6379
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}
password: ${REDIS_PASSWORD:}
database: 0
timeout: 3000ms
timeout: 10000ms
lettuce:
pool:
max-active: 20
max-idle: 10
min-idle: 5
max-wait: 3000ms
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
# 网关和Nacos配置
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: emotion-dev
group: DEFAULT_GROUP
enabled: false
server-addr: ${NACOS_HOST:localhost}:${NACOS_PORT:8848}
namespace: ${NACOS_NAMESPACE:}
group: ${NACOS_GROUP:DEFAULT_GROUP}
enabled: ${NACOS_DISCOVERY_ENABLED:true}
username: ${NACOS_USERNAME:nacos}
password: ${NACOS_PASSWORD:nacos}
metadata:
version: 1.0.0
zone: ${NACOS_ZONE:default}
config:
enabled: false
server-addr: ${NACOS_HOST:localhost}:${NACOS_PORT:8848}
namespace: ${NACOS_NAMESPACE:}
group: ${NACOS_GROUP:DEFAULT_GROUP}
file-extension: yml
enabled: ${NACOS_CONFIG_ENABLED:false}
username: ${NACOS_USERNAME:nacos}
password: ${NACOS_PASSWORD:nacos}
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
# 认证服务路由
- id: emotion-auth
uri: lb://emotion-auth
predicates:
- Path=/api/auth/**
filters:
- StripPrefix=2
# 用户服务路由
- id: emotion-user
uri: lb://emotion-user
@@ -1,106 +1,5 @@
server:
port: 19000
# 本地开发环境配置
spring:
application:
name: emotion-gateway
# Redis配置
data:
redis:
host: localhost
port: 6379
password:
database: 0
timeout: 10000ms
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
# 网关配置
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
# 用户服务路由
- id: emotion-user-route
uri: http://localhost:19001
predicates:
- Path=/user/**
filters:
- StripPrefix=0
# 验证码服务路由
- id: emotion-captcha-route
uri: http://localhost:19001
predicates:
- Path=/captcha/**
filters:
- StripPrefix=0
# OAuth服务路由
- id: emotion-oauth-route
uri: http://localhost:19001
predicates:
- Path=/oauth/**
filters:
- StripPrefix=0
# AI服务路由
- id: emotion-ai-route
uri: http://localhost:19002
predicates:
- Path=/ai/**
filters:
- StripPrefix=0
# 记录服务路由
- id: emotion-record-route
uri: http://localhost:19003
predicates:
- Path=/record/**
filters:
- StripPrefix=0
# 成长服务路由
- id: emotion-growth-route
uri: http://localhost:19004
predicates:
- Path=/growth/**
filters:
- StripPrefix=0
# 探索服务路由
- id: emotion-explore-route
uri: http://localhost:19005
predicates:
- Path=/explore/**
filters:
- StripPrefix=0
# 奖励服务路由
- id: emotion-reward-route
uri: http://localhost:19006
predicates:
- Path=/reward/**
filters:
- StripPrefix=0
# 统计服务路由
- id: emotion-stats-route
uri: http://localhost:19007
predicates:
- Path=/stats/**
filters:
- StripPrefix=0
# Nacos配置
spring:
cloud:
nacos:
@@ -109,7 +8,16 @@ spring:
namespace:
group: DEFAULT_GROUP
enabled: true
username: nacos
password: Peanut2817*#
metadata:
version: 1.0.0
zone: local
register-enabled: true
ephemeral: true
cluster-name: DEFAULT
service: ${spring.application.name}
weight: 1
heart-beat-interval: 5000
heart-beat-timeout: 15000
ip-delete-timeout: 30000
@@ -119,6 +27,118 @@ spring:
group: DEFAULT_GROUP
file-extension: yml
enabled: false
username: nacos
password: Peanut2817*#
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
# 全局跨域配置
globalcors:
cors-configurations:
'[/**]':
allowed-origins: "*"
allowed-methods: "*"
allowed-headers: "*"
allow-credentials: true
routes:
# 用户服务路由 (包含认证功能)
- id: emotion-user-route
uri: lb://emotion-user
predicates:
- Path=/user/**
filters:
- StripPrefix=0
# 验证码服务路由
- id: emotion-captcha-route
uri: lb://emotion-user
predicates:
- Path=/captcha/**
filters:
- StripPrefix=0
# OAuth服务路由
- id: emotion-oauth-route
uri: lb://emotion-user
predicates:
- Path=/oauth/**
filters:
- StripPrefix=0
# AI服务路由
- id: emotion-ai-route
uri: lb://emotion-ai
predicates:
- Path=/ai/**
filters:
- StripPrefix=0
# WebSocket聊天服务路由
- id: emotion-websocket-route
uri: lb://emotion-websocket
predicates:
- Path=/websocket/**
filters:
- StripPrefix=0
# WebSocket连接路由 (支持WebSocket升级)
- id: emotion-websocket-ws-route
uri: lb://emotion-websocket
predicates:
- Path=/ws/**
filters:
- StripPrefix=0
# 情绪记录服务路由
- id: emotion-record-route
uri: lb://emotion-record
predicates:
- Path=/record/**
filters:
- StripPrefix=0
# 成长课题服务路由
- id: emotion-growth-route
uri: lb://emotion-growth
predicates:
- Path=/growth/**
filters:
- StripPrefix=0
# 地图探索服务路由
- id: emotion-explore-route
uri: lb://emotion-explore
predicates:
- Path=/explore/**
filters:
- StripPrefix=0
# 成就奖励服务路由
- id: emotion-reward-route
uri: lb://emotion-reward
predicates:
- Path=/reward/**
filters:
- StripPrefix=0
# 统计分析服务路由
- id: emotion-stats-route
uri: lb://emotion-stats
predicates:
- Path=/stats/**
filters:
- StripPrefix=0
# Redis配置
data:
redis:
host: localhost
port: 6379
password:
database: 0
# 日志配置
logging:
@@ -126,20 +146,5 @@ logging:
com.emotionmuseum: debug
org.springframework.cloud.gateway: debug
org.springframework.web: debug
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n"
file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n"
file:
name: logs/emotion-gateway-local.log
# 管理端点配置
management:
endpoints:
web:
exposure:
include: health,info,metrics,gateway
endpoint:
health:
show-details: always
gateway:
enabled: true
name: logs/emotion-gateway-local.log
@@ -1,105 +1,151 @@
server:
port: 9000
# 生产环境配置
spring:
application:
name: emotion-gateway
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
- com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration
data:
redis:
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}
database: 0
timeout: 3000ms
lettuce:
pool:
max-active: 20
max-idle: 10
min-idle: 5
max-wait: 3000ms
cloud:
nacos:
discovery:
server-addr: ${NACOS_SERVER_ADDR:localhost:8848}
namespace: public
server-addr: 47.111.10.27:8848
namespace: prod
group: DEFAULT_GROUP
enabled: true
ip: ${SERVER_IP:localhost}
username: nacos
password: EmotionMuseum2025
metadata:
version: 1.0.0
environment: prod
zone: prod
register-enabled: true
ephemeral: true
cluster-name: DEFAULT
service: ${spring.application.name}
weight: 1
heart-beat-interval: 5000
heart-beat-timeout: 15000
ip-delete-timeout: 30000
config:
server-addr: 47.111.10.27:8848
namespace: prod
group: DEFAULT_GROUP
file-extension: yml
enabled: false
username: nacos
password: EmotionMuseum2025
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
# 全局跨域配置
globalcors:
cors-configurations:
'[/**]':
allowed-origins: "*"
allowed-methods: "*"
allowed-headers: "*"
allow-credentials: true
routes:
# 用户服务路由
- id: emotion-user
# 用户服务路由 (包含认证功能)
- id: emotion-user-route
uri: lb://emotion-user
predicates:
- Path=/api/user/**
- Path=/user/**
filters:
- StripPrefix=0
# 验证码服务路由
- id: emotion-captcha-route
uri: lb://emotion-user
predicates:
- Path=/captcha/**
filters:
- StripPrefix=0
# OAuth服务路由
- id: emotion-oauth-route
uri: lb://emotion-user
predicates:
- Path=/oauth/**
filters:
- StripPrefix=0
# AI服务路由
- id: emotion-ai
- id: emotion-ai-route
uri: lb://emotion-ai
predicates:
- Path=/api/ai/**
# 记录服务路由
- id: emotion-record
- Path=/ai/**
filters:
- StripPrefix=0
# WebSocket聊天服务路由
- id: emotion-websocket-route
uri: lb://emotion-websocket
predicates:
- Path=/websocket/**
filters:
- StripPrefix=0
# WebSocket连接路由 (支持WebSocket升级)
- id: emotion-websocket-ws-route
uri: lb://emotion-websocket
predicates:
- Path=/ws/**
filters:
- StripPrefix=0
# 情绪记录服务路由
- id: emotion-record-route
uri: lb://emotion-record
predicates:
- Path=/api/record/**
- Path=/record/**
filters:
- StripPrefix=1
# 成长服务路由
- id: emotion-growth
- StripPrefix=0
# 成长课题服务路由
- id: emotion-growth-route
uri: lb://emotion-growth
predicates:
- Path=/api/growth/**
- Path=/growth/**
filters:
- StripPrefix=1
# 探索服务路由
- id: emotion-explore
- StripPrefix=0
# 地图探索服务路由
- id: emotion-explore-route
uri: lb://emotion-explore
predicates:
- Path=/api/explore/**
- Path=/explore/**
filters:
- StripPrefix=1
# 奖励服务路由
- id: emotion-reward
- StripPrefix=0
# 成就奖励服务路由
- id: emotion-reward-route
uri: lb://emotion-reward
predicates:
- Path=/api/reward/**
- Path=/reward/**
filters:
- StripPrefix=1
# 统计服务路由
- id: emotion-stats
- StripPrefix=0
# 统计分析服务路由
- id: emotion-stats-route
uri: lb://emotion-stats
predicates:
- Path=/api/stats/**
- Path=/stats/**
filters:
- StripPrefix=1
- StripPrefix=0
# Redis配置
data:
redis:
host: 47.111.10.27
port: 6379
password: EmotionMuseum2025*#
database: 0
# 日志配置
logging:
level:
com.emotionmuseum: INFO
org.springframework.cloud.gateway: DEBUG
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
# 管理端点
management:
endpoints:
web:
exposure:
include: health,info,gateway
endpoint:
health:
show-details: always
com.emotionmuseum: warn
org.springframework.cloud.gateway: warn
org.springframework.web: warn
com.alibaba.nacos: error
file:
name: logs/emotion-gateway-prod.log
@@ -0,0 +1,151 @@
# 测试环境配置
spring:
cloud:
nacos:
discovery:
server-addr: 47.111.10.27:8848
namespace: test
group: DEFAULT_GROUP
enabled: true
username: nacos
password: EmotionMuseum2025
metadata:
version: 1.0.0
zone: test
register-enabled: true
ephemeral: true
cluster-name: DEFAULT
service: ${spring.application.name}
weight: 1
heart-beat-interval: 5000
heart-beat-timeout: 15000
ip-delete-timeout: 30000
config:
server-addr: 47.111.10.27:8848
namespace: test
group: DEFAULT_GROUP
file-extension: yml
enabled: false
username: nacos
password: EmotionMuseum2025
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
# 全局跨域配置
globalcors:
cors-configurations:
"[/**]":
allowed-origins: "*"
allowed-methods: "*"
allowed-headers: "*"
allow-credentials: true
routes:
# 用户服务路由 (包含认证功能)
- id: emotion-user-route
uri: lb://emotion-user
predicates:
- Path=/user/**
filters:
- StripPrefix=0
# 验证码服务路由
- id: emotion-captcha-route
uri: lb://emotion-user
predicates:
- Path=/captcha/**
filters:
- StripPrefix=0
# OAuth服务路由
- id: emotion-oauth-route
uri: lb://emotion-user
predicates:
- Path=/oauth/**
filters:
- StripPrefix=0
# AI服务路由
- id: emotion-ai-route
uri: lb://emotion-ai
predicates:
- Path=/ai/**
filters:
- StripPrefix=0
# WebSocket聊天服务路由
- id: emotion-websocket-route
uri: lb://emotion-websocket
predicates:
- Path=/websocket/**
filters:
- StripPrefix=0
# WebSocket连接路由 (支持WebSocket升级)
- id: emotion-websocket-ws-route
uri: lb://emotion-websocket
predicates:
- Path=/ws/**
filters:
- StripPrefix=0
# 情绪记录服务路由
- id: emotion-record-route
uri: lb://emotion-record
predicates:
- Path=/record/**
filters:
- StripPrefix=0
# 成长课题服务路由
- id: emotion-growth-route
uri: lb://emotion-growth
predicates:
- Path=/growth/**
filters:
- StripPrefix=0
# 地图探索服务路由
- id: emotion-explore-route
uri: lb://emotion-explore
predicates:
- Path=/explore/**
filters:
- StripPrefix=0
# 成就奖励服务路由
- id: emotion-reward-route
uri: lb://emotion-reward
predicates:
- Path=/reward/**
filters:
- StripPrefix=0
# 统计分析服务路由
- id: emotion-stats-route
uri: lb://emotion-stats
predicates:
- Path=/stats/**
filters:
- StripPrefix=0
# Redis配置
data:
redis:
host: 47.111.10.27
port: 6379
password: EmotionMuseum2025*#
database: 0
# 日志配置
logging:
level:
com.emotionmuseum: info
org.springframework.cloud.gateway: info
org.springframework.web: info
com.alibaba.nacos: warn
file:
name: logs/emotion-gateway-test.log
@@ -4,40 +4,72 @@ server:
spring:
application:
name: emotion-gateway
# 配置文件激活
profiles:
active: dev
active: ${SPRING_PROFILES_ACTIVE:local}
# 允许Bean覆盖和循环引用
main:
allow-bean-definition-overriding: true
allow-circular-references: true
# 排除数据库自动配置
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
- com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration
# Redis配置
data:
redis:
host: localhost
port: 6379
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}
password: ${REDIS_PASSWORD:}
database: 0
timeout: 3000ms
timeout: 10000ms
lettuce:
pool:
max-active: 20
max-idle: 10
min-idle: 5
max-wait: 3000ms
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
# 网关和Nacos配置
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: emotion-dev
group: DEFAULT_GROUP
enabled: false
server-addr: ${NACOS_HOST:localhost}:${NACOS_PORT:8848}
namespace: ${NACOS_NAMESPACE:}
group: ${NACOS_GROUP:DEFAULT_GROUP}
enabled: ${NACOS_DISCOVERY_ENABLED:true}
username: ${NACOS_USERNAME:nacos}
password: ${NACOS_PASSWORD:nacos}
metadata:
version: 1.0.0
zone: ${NACOS_ZONE:default}
config:
enabled: false
server-addr: ${NACOS_HOST:localhost}:${NACOS_PORT:8848}
namespace: ${NACOS_NAMESPACE:}
group: ${NACOS_GROUP:DEFAULT_GROUP}
file-extension: yml
enabled: ${NACOS_CONFIG_ENABLED:false}
username: ${NACOS_USERNAME:nacos}
password: ${NACOS_PASSWORD:nacos}
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
# 认证服务路由
- id: emotion-auth
uri: lb://emotion-auth
predicates:
- Path=/api/auth/**
filters:
- StripPrefix=2
# 用户服务路由
- id: emotion-user
uri: lb://emotion-user
+157
View File
@@ -0,0 +1,157 @@
#!/bin/bash
# ============================================================================
# 网关路由测试脚本
# 测试所有微服务通过网关的路由转发功能
# ============================================================================
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 网关地址
GATEWAY_URL="http://localhost:19000"
echo -e "${BLUE}===========================================${NC}"
echo -e "${BLUE}网关路由测试${NC}"
echo -e "${BLUE}===========================================${NC}"
# 测试函数
test_route() {
local service_name=$1
local route_path=$2
local expected_port=$3
local description=$4
echo -e "${YELLOW}测试 $description ($service_name)${NC}"
echo -e "路由: $GATEWAY_URL$route_path"
echo -e "期望转发到: localhost:$expected_port"
# 测试健康检查端点
local health_url="$GATEWAY_URL$route_path/actuator/health"
response=$(curl -s -w "%{http_code}" -o /tmp/gateway_test_response "$health_url" 2>/dev/null)
http_code="${response: -3}"
if [ "$http_code" = "200" ]; then
echo -e "${GREEN}✅ 路由正常 (HTTP $http_code)${NC}"
# 显示响应内容
if [ -f /tmp/gateway_test_response ]; then
response_body=$(cat /tmp/gateway_test_response)
echo -e "${GREEN}响应: $response_body${NC}"
fi
elif [ "$http_code" = "404" ]; then
echo -e "${YELLOW}⚠️ 服务未启动或路径不存在 (HTTP $http_code)${NC}"
elif [ "$http_code" = "000" ]; then
echo -e "${RED}❌ 连接失败 - 网关可能未启动${NC}"
else
echo -e "${RED}❌ 路由异常 (HTTP $http_code)${NC}"
if [ -f /tmp/gateway_test_response ]; then
response_body=$(cat /tmp/gateway_test_response)
echo -e "${RED}错误响应: $response_body${NC}"
fi
fi
echo ""
}
# 首先测试网关本身
echo -e "${YELLOW}测试网关服务本身${NC}"
gateway_response=$(curl -s -w "%{http_code}" -o /tmp/gateway_health "$GATEWAY_URL/actuator/health" 2>/dev/null)
gateway_code="${gateway_response: -3}"
if [ "$gateway_code" = "200" ]; then
echo -e "${GREEN}✅ 网关服务正常运行${NC}"
if [ -f /tmp/gateway_health ]; then
gateway_health=$(cat /tmp/gateway_health)
echo -e "${GREEN}网关状态: $gateway_health${NC}"
fi
else
echo -e "${RED}❌ 网关服务未启动或异常 (HTTP $gateway_code)${NC}"
echo -e "${RED}请先启动网关服务: cd backend/emotion-gateway && mvn spring-boot:run${NC}"
exit 1
fi
echo ""
# 测试各个服务路由
echo -e "${BLUE}开始测试各服务路由...${NC}"
echo ""
# 用户服务路由
test_route "emotion-user" "/user" "19001" "用户服务"
# AI服务路由
test_route "emotion-ai" "/ai" "19002" "AI对话服务"
# WebSocket服务路由
test_route "emotion-websocket" "/websocket" "19007" "WebSocket聊天服务"
# 情绪记录服务路由
test_route "emotion-record" "/record" "19003" "情绪记录服务"
# 成长课题服务路由
test_route "emotion-growth" "/growth" "19004" "成长课题服务"
# 地图探索服务路由
test_route "emotion-explore" "/explore" "19005" "地图探索服务"
# 成就奖励服务路由
test_route "emotion-reward" "/reward" "19006" "成就奖励服务"
# 统计分析服务路由
test_route "emotion-stats" "/stats" "19008" "统计分析服务"
echo -e "${BLUE}===========================================${NC}"
# 测试特殊路由
echo -e "${YELLOW}测试特殊功能路由...${NC}"
echo ""
# 测试验证码路由
echo -e "${YELLOW}测试验证码服务路由${NC}"
captcha_url="$GATEWAY_URL/captcha/api/captcha/generate"
echo -e "URL: $captcha_url"
captcha_response=$(curl -s -w "%{http_code}" -o /tmp/captcha_test "$captcha_url" 2>/dev/null)
captcha_code="${captcha_response: -3}"
if [ "$captcha_code" = "200" ] || [ "$captcha_code" = "404" ]; then
echo -e "${GREEN}✅ 验证码路由转发正常${NC}"
else
echo -e "${RED}❌ 验证码路由异常 (HTTP $captcha_code)${NC}"
fi
echo ""
# 测试WebSocket特殊接口
echo -e "${YELLOW}测试WebSocket在线用户接口${NC}"
ws_users_url="$GATEWAY_URL/websocket/online-users"
echo -e "URL: $ws_users_url"
ws_response=$(curl -s -w "%{http_code}" -o /tmp/ws_test "$ws_users_url" 2>/dev/null)
ws_code="${ws_response: -3}"
if [ "$ws_code" = "200" ]; then
echo -e "${GREEN}✅ WebSocket REST API路由正常${NC}"
if [ -f /tmp/ws_test ]; then
ws_body=$(cat /tmp/ws_test)
echo -e "${GREEN}响应: $ws_body${NC}"
fi
elif [ "$ws_code" = "404" ]; then
echo -e "${YELLOW}⚠️ WebSocket服务未启动${NC}"
else
echo -e "${RED}❌ WebSocket路由异常 (HTTP $ws_code)${NC}"
fi
echo ""
echo -e "${BLUE}===========================================${NC}"
echo -e "${BLUE}测试完成${NC}"
echo -e "${BLUE}===========================================${NC}"
# 清理临时文件
rm -f /tmp/gateway_test_response /tmp/gateway_health /tmp/captcha_test /tmp/ws_test
echo -e "${YELLOW}提示:${NC}"
echo -e "1. 如果某些服务显示未启动,请使用以下命令启动:"
echo -e " ${GREEN}cd backend && ./start-services.sh${NC}"
echo -e "2. 单独启动某个服务:"
echo -e " ${GREEN}cd backend/emotion-xxx && mvn spring-boot:run -Dspring-boot.run.profiles=local${NC}"
echo -e "3. 查看网关日志:"
echo -e " ${GREEN}tail -f backend/logs/emotion-gateway-local.log${NC}"
@@ -0,0 +1,181 @@
# 网关服务配置更新总结
## 更新时间
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路由配置
```yaml
# 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. 新增跨域配置
```yaml
# 全局跨域配置
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服务
```bash
# 直接访问
curl http://localhost:19002/api/ai/chat/send
# 通过网关访问
curl http://localhost:19000/ai/api/ai/chat/send
```
### 2. 通过网关访问WebSocket服务
```bash
# REST API测试
curl http://localhost:19000/websocket/online-users
# WebSocket连接
ws://localhost:19000/ws/chat
```
### 3. 通过网关访问用户服务
```bash
# 用户注册
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. 检查网关状态
```bash
curl http://localhost:19000/actuator/health
```
### 2. 测试路由转发
```bash
# 测试用户服务路由
curl http://localhost:19000/user/actuator/health
# 测试AI服务路由
curl http://localhost:19000/ai/actuator/health
# 测试WebSocket服务路由
curl http://localhost:19000/websocket/online-users
```
### 3. 查看网关日志
```bash
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路由已添加
- ✅ 跨域配置已添加
- ⏳ 实际路由转发测试待进行