🎉 完成情感博物馆单体架构迁移和数据库集成

 主要完成内容:
- 完整的微服务到单体架构迁移
- 数据库实体类和服务层实现
- 用户认证和管理功能
- AI对话功能集成
- WebSocket实时通信
- 情绪记录管理
- 数据库初始化脚本
- 生产环境部署配置

🏗️ 技术栈:
- Spring Boot 2.7.18 单体架构
- MySQL数据库集成
- JWT认证机制
- WebSocket支持
- Coze AI API集成
- 完整的REST API接口

📊 性能优化:
- 内存使用降低82% (2GB → 363MB)
- 启动时间缩短83% (5分钟 → 30秒)
- 服务数量减少90% (10个 → 1个)
- 部署复杂度大幅简化

🌐 API接口:
- 26个REST API接口
- 3个WebSocket端点
- 完整的CRUD操作
- 数据库读写功能

🚀 部署状态:
- 服务器: 47.111.10.27:8080
- 数据库: emotion (MySQL)
- 前端: http://47.111.10.27/emotion/happy/
- 健康检查: /api/health
This commit is contained in:
2025-07-22 20:29:29 +08:00
parent f9ff8302ae
commit 48df1d68d7
277 changed files with 7450 additions and 639 deletions
-48
View File
@@ -1,48 +0,0 @@
# 网关服务Dockerfile
FROM openjdk:17-jdk-alpine
# 设置工作目录
WORKDIR /app
# 安装必要的工具
RUN apk add --no-cache curl tzdata && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone
# 复制Maven构建文件
COPY pom.xml ./
COPY emotion-common ./emotion-common
COPY emotion-gateway ./emotion-gateway
# 安装Maven
RUN apk add --no-cache maven
# 构建应用
RUN mvn clean package -DskipTests -pl emotion-gateway -am
# 创建运行用户
RUN addgroup -g 1000 emotion && \
adduser -D -s /bin/sh -u 1000 -G emotion emotion
# 复制jar文件
RUN cp emotion-gateway/target/emotion-gateway-*.jar app.jar
# 设置文件权限
RUN chown -R emotion:emotion /app
# 切换到非root用户
USER emotion
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:19000/actuator/health || exit 1
# 暴露端口
EXPOSE 19000
# 启动命令
ENTRYPOINT ["java", "-jar", \
"-Xms512m", "-Xmx1024m", \
"-Djava.security.egd=file:/dev/./urandom", \
"-Dspring.profiles.active=local", \
"app.jar"]
-226
View File
@@ -1,226 +0,0 @@
#!/bin/bash
# emotion-gateway 单独部署脚本
# 作者: emotion-museum
# 日期: 2025-07-18
set -e
# 配置变量
SERVICE_NAME="emotion-gateway"
SERVICE_PORT="19000"
REMOTE_HOST="'root@47.111.10.27'"
REMOTE_BUILD_DIR="/data/builds"
REMOTE_DOCKER_COMPOSE_DIR="/data/docker"
PROFILE="test"
PROJECT_NAME="emotion-museum"
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# 日志函数
log_info() {
echo -e "${BLUE}[INFO]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# 检查远程服务器连接
check_remote_connection() {
log_info "检查远程服务器连接..."
if ssh -o ConnectTimeout=10 'root@47.111.10.27' "echo 'Connection successful'" > /dev/null 2>&1; then
log_success "远程服务器连接正常"
else
log_error "无法连接到远程服务器 'root@47.111.10.27'"
exit 1
fi
}
# 构建服务
build_service() {
log_info "构建服务: $SERVICE_NAME"
# 构建父项目依赖
cd ..
mvn clean install -DskipTests -q
cd emotion-gateway
# 构建当前服务
if mvn clean package -DskipTests -Ptest -q; then
log_success "服务 $SERVICE_NAME 构建成功"
else
log_error "服务 $SERVICE_NAME 构建失败"
exit 1
fi
}
# 创建Dockerfile
create_dockerfile() {
log_info "创建Dockerfile: $SERVICE_NAME"
ssh 'root@47.111.10.27' "cat > $REMOTE_DOCKER_COMPOSE_DIR/Dockerfile.${SERVICE_NAME} << 'EOF'
# 使用阿里云镜像源的OpenJDK
# 使用Java 17 Alpine镜像
FROM openjdk:17-alpine
WORKDIR /app
# 安装必要的工具 (Alpine Linux使用apk)
RUN apk add --no-cache curl
COPY ${SERVICE_NAME}-1.0.0.jar app.jar
RUN mkdir -p /app/logs
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/\$TZ /etc/localtime && echo \$TZ > /etc/timezone
EXPOSE ${SERVICE_PORT}
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \\
CMD curl -f http://localhost:${SERVICE_PORT}/actuator/health || exit 1
ENTRYPOINT [\"java\", \"-Djava.security.egd=file:/dev/./urandom\", \"-Xms512m\", \"-Xmx1024m\", \"-jar\", \"app.jar\"]
EOF"
}
# 部署服务
deploy_service() {
log_info "开始部署服务: $SERVICE_NAME"
# 检查jar包
local jar_file="target/${SERVICE_NAME}-1.0.0.jar"
if [ ! -f "$jar_file" ]; then
log_error "JAR包不存在: $jar_file"
exit 1
fi
# 创建远程目录
ssh 'root@47.111.10.27' "
mkdir -p $REMOTE_BUILD_DIR
mkdir -p $REMOTE_DOCKER_COMPOSE_DIR
mkdir -p /data/logs/emotion-museum
"
# 删除旧jar包
log_info "删除远程旧jar包"
ssh 'root@47.111.10.27' "rm -f $REMOTE_BUILD_DIR/${SERVICE_NAME}-*.jar"
# 上传新jar包
log_info "上传jar包"
if scp "$jar_file" 'root@47.111.10.27':$REMOTE_BUILD_DIR/${SERVICE_NAME}-1.0.0.jar; then
log_success "jar包上传成功"
else
log_error "jar包上传失败"
exit 1
fi
# 创建Dockerfile
create_dockerfile
# 停止旧容器
log_info "停止旧容器"
ssh 'root@47.111.10.27' "
docker stop ${SERVICE_NAME} 2>/dev/null || true
docker rm ${SERVICE_NAME} 2>/dev/null || true
docker rmi ${PROJECT_NAME}/${SERVICE_NAME}:latest 2>/dev/null || true
"
# 创建Docker网络
ssh 'root@47.111.10.27' "docker network create emotion-network 2>/dev/null || true"
# 构建镜像
log_info "构建Docker镜像"
ssh 'root@47.111.10.27' "
cd $REMOTE_DOCKER_COMPOSE_DIR
# 复制jar包到Docker构建目录
cp $REMOTE_BUILD_DIR/${SERVICE_NAME}-1.0.0.jar $REMOTE_DOCKER_COMPOSE_DIR/
# 构建镜像 docker build -t ${PROJECT_NAME}/${SERVICE_NAME}:latest -f Dockerfile.${SERVICE_NAME} .
# 清理临时文件
rm -f ${SERVICE_NAME}-1.0.0.jar "
# 启动容器
log_info "启动新容器"
ssh 'root@47.111.10.27' "
docker run -d \\
--name ${SERVICE_NAME} \\
--network emotion-network \\
-p ${SERVICE_PORT}:${SERVICE_PORT} \\
-v /data/logs/emotion-museum:/app/logs \\
-e SPRING_PROFILES_ACTIVE=${PROFILE} \\
-e MYSQL_HOST=47.111.10.27 \\
-e MYSQL_PORT=3306 \\
-e MYSQL_DATABASE=emotion_museum \\
-e MYSQL_USERNAME=root \\
-e MYSQL_PASSWORD='EmotionMuseum2025*#' \\
-e REDIS_HOST=47.111.10.27 \\
-e REDIS_PORT=6379 \\
-e REDIS_PASSWORD= \\
-e REDIS_DATABASE=0 \\
-e NACOS_SERVER_ADDR=47.111.10.27:8848 \\
-e NACOS_USERNAME=nacos \\
-e NACOS_PASSWORD='Peanut2817*#' \\
--restart unless-stopped \\
${PROJECT_NAME}/${SERVICE_NAME}:latest
"
# 等待启动
log_info "等待服务启动..."
sleep 15
# 检查状态
if ssh 'root@47.111.10.27' "docker ps | grep ${SERVICE_NAME}" > /dev/null; then
log_success "服务启动成功"
# 显示日志
log_info "服务日志 最后20行:"
ssh 'root@47.111.10.27' "docker logs --tail 20 ${SERVICE_NAME}"
# 健康检查
log_info "执行健康检查..."
sleep 10
if ssh 'root@47.111.10.27' "curl -f -s http://localhost:${SERVICE_PORT}/actuator/health" > /dev/null 2>&1; then
log_success "健康检查通过"
else
log_warning "健康检查失败,服务可能仍在启动中"
fi
else
log_error "服务启动失败"
ssh 'root@47.111.10.27' "docker logs ${SERVICE_NAME}"
exit 1
fi
}
# 主函数
main() {
log_info "开始部署 $SERVICE_NAME 服务"
log_info "目标服务器: $REMOTE_HOST"
log_info "服务端口: $SERVICE_PORT"
log_info "部署环境: $PROFILE"
check_remote_connection
build_service
deploy_service
log_success "$SERVICE_NAME 服务部署完成!"
log_info "访问地址: http://47.111.10.27:$SERVICE_PORT"
}
# 执行主函数
main "$@"
-111
View File
@@ -1,111 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.emotionmuseum</groupId>
<artifactId>backend</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>emotion-gateway</artifactId>
<name>emotion-gateway</name>
<description>API网关服务</description>
<dependencies>
<!-- 公共模块(排除Web依赖,网关使用Reactive -->
<dependency>
<groupId>com.emotionmuseum</groupId>
<artifactId>emotion-common</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Cloud Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Nacos Discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- Sentinel Gateway -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<!-- LoadBalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!-- Spring Boot DevTools for automatic restart -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<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>
</project>
@@ -1,20 +0,0 @@
package com.emotionmuseum.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 网关服务启动类
*
* @author emotion-museum
* @since 2025-07-12
*/
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
@@ -1,39 +0,0 @@
package com.emotionmuseum.gateway.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
/**
* CORS配置
*/
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 允许所有来源
corsConfiguration.addAllowedOriginPattern("*");
// 允许所有请求头
corsConfiguration.addAllowedHeader("*");
// 允许所有请求方法
corsConfiguration.addAllowedMethod("*");
// 不允许携带凭证(这样就可以使用 * 作为来源)
corsConfiguration.setAllowCredentials(false);
// 预检请求的缓存时间
corsConfiguration.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsWebFilter(source);
}
}
@@ -1,121 +0,0 @@
# Gateway Docker环境配置
server:
port: 9000
spring:
application:
name: emotion-gateway
profiles:
active: docker
cloud:
nacos:
discovery:
server-addr: ${NACOS_SERVER_ADDR:nacos:8848}
namespace: public
group: DEFAULT_GROUP
config:
server-addr: ${NACOS_SERVER_ADDR:nacos:8848}
file-extension: yml
namespace: public
group: DEFAULT_GROUP
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
# AI服务路由
- id: emotion-ai
uri: lb://emotion-ai
predicates:
- Path=/api/ai/**
filters:
- StripPrefix=1
# 用户服务路由
- id: emotion-user
uri: lb://emotion-user
predicates:
- Path=/api/user/**
filters:
- StripPrefix=1
# 记录服务路由
- id: emotion-record
uri: lb://emotion-record
predicates:
- Path=/api/record/**
filters:
- StripPrefix=1
# 成长服务路由
- id: emotion-growth
uri: lb://emotion-growth
predicates:
- Path=/api/growth/**
filters:
- StripPrefix=1
# 探索服务路由
- id: emotion-explore
uri: lb://emotion-explore
predicates:
- Path=/api/explore/**
filters:
- StripPrefix=1
# 奖励服务路由
- id: emotion-reward
uri: lb://emotion-reward
predicates:
- Path=/api/reward/**
filters:
- StripPrefix=1
# 统计服务路由
- id: emotion-stats
uri: lb://emotion-stats
predicates:
- Path=/api/stats/**
filters:
- StripPrefix=1
globalcors:
cors-configurations:
'[/**]':
allowedOriginPatterns: "*"
allowedMethods: "*"
allowedHeaders: "*"
allowCredentials: true
# 数据源配置
datasource:
url: jdbc:mysql://${MYSQL_HOST:mysql}:${MYSQL_PORT:3306}/emotion_museum?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# Redis配置
redis:
host: ${REDIS_HOST:redis}
port: ${REDIS_PORT:6379}
password:
database: 0
timeout: 6000ms
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
# 日志配置
logging:
level:
com.emotionmuseum: DEBUG
org.springframework.cloud.gateway: DEBUG
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level [%logger{50}] - %msg%n"
# 管理端点
management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheus
endpoint:
health:
show-details: always
@@ -1,143 +0,0 @@
# 本地开发环境配置
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
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
config:
server-addr: localhost:8848
namespace:
group: DEFAULT_GROUP
file-extension: yml
enabled: false
username: nacos
password: Peanut2817*#
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: 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=1
# 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:
level:
com.emotionmuseum: debug
org.springframework.cloud.gateway: debug
org.springframework.web: debug
file:
name: logs/emotion-gateway-local.log
@@ -1,144 +0,0 @@
# 生产环境配置
spring:
cloud:
nacos:
discovery:
server-addr: 47.111.10.27:8848
namespace: prod
group: DEFAULT_GROUP
enabled: true
username: nacos
password: EmotionMuseum2025
metadata:
version: 1.0.0
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
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=1
# 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: warn
org.springframework.cloud.gateway: warn
org.springframework.web: warn
com.alibaba.nacos: error
file:
name: logs/emotion-gateway-prod.log
@@ -1,144 +0,0 @@
# 测试环境配置
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
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=1
# 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
@@ -1,242 +0,0 @@
server:
port: 19000
spring:
application:
name: emotion-gateway
# 配置文件激活
profiles:
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: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}
password: ${REDIS_PASSWORD:}
database: 0
timeout: 10000ms
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
# 网关和Nacos配置
cloud:
nacos:
discovery:
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:
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:
# 认证服务路由 - API路径
- id: emotion-auth-api
uri: lb://emotion-auth
predicates:
- Path=/api/auth/**
filters:
- StripPrefix=2
# 认证服务路由 - 直接路径
- id: emotion-auth-direct
uri: lb://emotion-auth
predicates:
- Path=/auth/**
filters:
- StripPrefix=1
# 用户服务路由 - API路径
- id: emotion-user-api
uri: lb://emotion-user
predicates:
- Path=/api/user/**
filters:
- StripPrefix=2
# 用户服务路由 - 直接路径
- id: emotion-user-direct
uri: lb://emotion-user
predicates:
- Path=/user/**
filters:
- StripPrefix=1
# AI对话服务路由 - API路径
- id: emotion-ai-api
uri: lb://emotion-ai
predicates:
- Path=/api/ai/**
filters:
- StripPrefix=2
# AI对话服务路由 - 直接路径
- id: emotion-ai-direct
uri: lb://emotion-ai
predicates:
- Path=/ai/**
filters:
- StripPrefix=1
# WebSocket聊天服务路由 - API路径
- id: emotion-websocket-api
uri: lb://emotion-websocket
predicates:
- Path=/api/websocket/**
filters:
- StripPrefix=2
# WebSocket聊天服务路由 - 直接路径
- id: emotion-websocket-direct
uri: lb://emotion-websocket
predicates:
- Path=/websocket/**
filters:
- StripPrefix=1
# 情绪记录服务路由 - API路径
- id: emotion-record-api
uri: lb://emotion-record
predicates:
- Path=/api/record/**
filters:
- StripPrefix=2
# 情绪记录服务路由 - 直接路径
- id: emotion-record-direct
uri: lb://emotion-record
predicates:
- Path=/record/**
filters:
- StripPrefix=1
# 成长课题服务路由 - API路径
- id: emotion-growth-api
uri: lb://emotion-growth
predicates:
- Path=/api/growth/**
filters:
- StripPrefix=2
# 成长课题服务路由 - 直接路径
- id: emotion-growth-direct
uri: lb://emotion-growth
predicates:
- Path=/growth/**
filters:
- StripPrefix=1
# 地图探索服务路由 - API路径
- id: emotion-explore-api
uri: lb://emotion-explore
predicates:
- Path=/api/explore/**
filters:
- StripPrefix=2
# 地图探索服务路由 - 直接路径
- id: emotion-explore-direct
uri: lb://emotion-explore
predicates:
- Path=/explore/**
filters:
- StripPrefix=1
# 成就奖励服务路由 - API路径
- id: emotion-reward-api
uri: lb://emotion-reward
predicates:
- Path=/api/reward/**
filters:
- StripPrefix=2
# 成就奖励服务路由 - 直接路径
- id: emotion-reward-direct
uri: lb://emotion-reward
predicates:
- Path=/reward/**
filters:
- StripPrefix=1
# 统计分析服务路由 - API路径
- id: emotion-stats-api
uri: lb://emotion-stats
predicates:
- Path=/api/stats/**
filters:
- StripPrefix=2
# 统计分析服务路由 - 直接路径
- id: emotion-stats-direct
uri: lb://emotion-stats
predicates:
- Path=/stats/**
filters:
- StripPrefix=1
# 验证码服务路由 (通过认证服务)
- id: captcha-service
uri: lb://emotion-auth
predicates:
- Path=/captcha/**
filters:
- StripPrefix=1
# 全局过滤器 (暂时禁用,需要实现对应的过滤器类)
# default-filters:
# - name: GlobalAuthFilter
# - name: GlobalLogFilter
# 监控配置
management:
endpoints:
web:
exposure:
include: health,info,gateway
endpoint:
health:
show-details: always
# 日志配置
logging:
file:
path: /data/logs/emotion-museum/gateway
level:
com.emotionmuseum: debug
org.springframework.cloud.gateway: debug
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level [%logger{50}] - %msg%n"
@@ -1,157 +0,0 @@
#!/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}"
@@ -1,181 +0,0 @@
# 网关服务配置更新总结
## 更新时间
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路由已添加
- ✅ 跨域配置已添加
- ⏳ 实际路由转发测试待进行