Files
happy-life-star/backend
peanut 8bfc2649e5 feat: 完善部署脚本系统,添加所有微服务模块
🚀 新增服务:
- emotion-explore (端口19005) - 探索服务
- emotion-reward (端口19006) - 奖励服务
- emotion-stats (端口19009) - 统计服务

🔧 脚本优化:
- 修复所有部署脚本的SSH连接格式为严格的 ssh 'root@47.111.10.27'
- 重新创建所有单服务部署脚本,确保语法正确
- 统一所有脚本的模板和格式
- 修复端口冲突问题 (emotion-stats从19008改为19009)

 完整服务列表:
- emotion-gateway:19000 - API网关
- emotion-user:19001 - 用户服务
- emotion-ai:19002 - AI聊天服务
- emotion-record:19003 - 记录服务
- emotion-growth:19004 - 成长服务
- emotion-explore:19005 - 探索服务
- emotion-reward:19006 - 奖励服务
- emotion-websocket:19007 - WebSocket服务
- emotion-auth:19008 - 认证服务
- emotion-stats:19009 - 统计服务

🔐 安全配置:
- 所有脚本使用SSH公钥认证
- 统一的MySQL密码配置: EmotionMuseum2025*#
- 统一的Nacos密码配置: Peanut2817*#

 测试验证:
- 所有脚本语法检查通过 
- SSH连接测试成功 
- 远程服务器Docker环境正常 
- 脚本执行权限设置完成 
2025-07-18 12:54:24 +08:00
..

情绪博物馆后端微服务

基于Spring Cloud Alibaba 2022.0.0.0的微服务架构,为情绪博物馆iOS应用提供后端API服务。

🏗️ 架构概览

技术栈

  • Spring Boot: 3.0.2
  • Spring Cloud: 2022.0.0
  • Spring Cloud Alibaba: 2022.0.0.0
  • JDK: 17+
  • MySQL: 8.0+
  • Redis: 7.0+
  • Nacos: 2.2.0+

微服务列表

服务名称 端口 描述 状态
emotion-gateway 8080 API网关 已实现
emotion-user 8081 用户服务 已实现
emotion-ai 8082 AI对话服务 已实现
emotion-record 8083 情绪记录服务 已实现
emotion-growth 8084 成长课题服务 已实现
emotion-explore 8085 地图探索服务 已实现
emotion-reward 8086 成就奖励服务 已实现
emotion-stats 8087 统计分析服务 已实现

🚀 快速开始

环境要求

  • JDK 17+
  • Maven 3.6+
  • MySQL 8.0+
  • Redis 7.0+
  • Nacos 2.2.0+

1. 环境准备

启动Nacos

# 下载Nacos 2.2.0
wget https://github.com/alibaba/nacos/releases/download/2.2.0/nacos-server-2.2.0.tar.gz
tar -xzf nacos-server-2.2.0.tar.gz
cd nacos/bin

# 单机模式启动
sh startup.sh -m standalone

# 访问控制台: http://localhost:8848/nacos
# 默认用户名/密码: nacos/nacos

启动MySQL

# 创建数据库
mysql -u root -p
CREATE DATABASE emotion_museum DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

# 导入数据库结构
mysql -u root -p emotion_museum < ../mysql_deploy_database.sql

启动Redis

redis-server

2. 配置Nacos

访问 http://localhost:8848/nacos,创建以下配置

命名空间

  • 命名空间ID: emotion-dev
  • 命名空间名: 情绪博物馆开发环境

配置文件

common-mysql.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/emotion_museum?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    hikari:
      minimum-idle: 5
      maximum-pool-size: 20
      idle-timeout: 600000
      max-lifetime: 1800000
      connection-timeout: 30000

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id-type: assign_uuid
      logic-delete-field: deleted
      logic-delete-value: 1
      logic-not-delete-value: 0

common-redis.yml

spring:
  data:
    redis:
      host: localhost
      port: 6379
      password:
      database: 0
      timeout: 3000ms
      lettuce:
        pool:
          max-active: 20
          max-idle: 10
          min-idle: 5
          max-wait: 3000ms

coze-config.yml

coze:
  base-url: https://api.coze.cn
  api-key: your-coze-api-key
  bot-id: your-bot-id
  user-id: emotion-museum-user
  timeout: 60
  max-retries: 3
  stream: false
  model:
    temperature: 0.7
    max-tokens: 1000
    top-p: 0.9
    frequency-penalty: 0.0
    presence-penalty: 0.0

3. 启动微服务

方式一:使用启动脚本(推荐)

# 启动所有服务
./start-services.sh

# 停止所有服务
./stop-services.sh

方式二:手动启动

# 编译项目
mvn clean compile -DskipTests

# 启动网关服务
cd emotion-gateway
mvn spring-boot:run &

# 启动用户服务
cd ../emotion-user
mvn spring-boot:run &

4. 验证服务状态

方式一:使用测试脚本(推荐)

# 运行完整测试
./test-services.sh

方式二:手动验证

# 健康检查
curl http://localhost:8080/actuator/health  # 网关服务
curl http://localhost:8081/actuator/health  # 用户服务
curl http://localhost:8082/actuator/health  # AI对话服务
curl http://localhost:8083/actuator/health  # 情绪记录服务
curl http://localhost:8084/actuator/health  # 成长课题服务
curl http://localhost:8085/actuator/health  # 地图探索服务
curl http://localhost:8086/actuator/health  # 成就奖励服务
curl http://localhost:8087/actuator/health  # 统计分析服务

# Nacos服务列表
curl http://localhost:8848/nacos/v1/ns/service/list?pageNo=1&pageSize=10

📡 API文档

用户服务API

用户注册

curl -X POST http://localhost:8080/api/user/register \
  -H "Content-Type: application/json" \
  -d '{
    "account": "test_user",
    "password": "123456",
    "username": "测试用户",
    "email": "test@example.com",
    "phone": "13800138000",
    "nickname": "小测试"
  }'

用户登录

curl -X POST http://localhost:8080/api/user/login \
  -H "Content-Type: application/json" \
  -d '{
    "account": "test_user",
    "password": "123456"
  }'

获取用户信息

curl -X GET http://localhost:8080/api/user/info/{userId} \
  -H "Authorization: Bearer {token}"

Coze AI服务API

健康检查

curl http://localhost:8080/api/ai/coze/health

测试AI对话

curl -X POST http://localhost:8080/api/ai/coze/test/message \
  -H "Content-Type: application/json" \
  -d "message=你好,我今天感觉有点焦虑&userId=test_user"

测试情绪分析

curl -X POST http://localhost:8080/api/ai/coze/test/emotion \
  -H "Content-Type: application/json" \
  -d "text=我今天心情很好,阳光明媚"

测试完整对话流程

curl -X POST http://localhost:8080/api/ai/coze/test/full-chat \
  -H "Content-Type: application/json" \
  -d "userMessage=我最近工作压力很大,感觉很累&userId=test_user"

🔧 开发指南

项目结构

backend/
├── emotion-common/          # 公共模块
│   ├── src/main/java/
│   │   └── com/emotionmuseum/common/
│   │       ├── entity/      # 基础实体
│   │       ├── result/      # 统一响应
│   │       └── util/        # 工具类
├── emotion-gateway/         # 网关服务
├── emotion-user/           # 用户服务
├── emotion-ai/             # AI对话服务(待实现)
├── emotion-record/         # 情绪记录服务(待实现)
├── emotion-growth/         # 成长课题服务(待实现)
├── emotion-explore/        # 地图探索服务(待实现)
├── emotion-reward/         # 成就奖励服务(待实现)
├── emotion-stats/          # 统计分析服务(待实现)
├── start-services.sh       # 启动脚本
├── stop-services.sh        # 停止脚本
└── pom.xml                 # 父工程POM

添加新微服务

  1. 创建模块
mkdir emotion-new-service
cd emotion-new-service
  1. 创建pom.xml
<parent>
    <groupId>com.emotionmuseum</groupId>
    <artifactId>backend</artifactId>
    <version>1.0.0</version>
</parent>
<artifactId>emotion-new-service</artifactId>
  1. 添加到父工程
<modules>
    <module>emotion-new-service</module>
</modules>
  1. 创建启动类
@SpringBootApplication(scanBasePackages = {"com.emotionmuseum"})
@EnableDiscoveryClient
@MapperScan("com.emotionmuseum.newservice.mapper")
public class NewServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(NewServiceApplication.class, args);
    }
}

🐛 故障排除

常见问题

  1. Nacos连接失败

    • 检查Nacos是否启动:curl http://localhost:8848/nacos/v1/ns/operator/metrics
    • 检查命名空间是否创建
    • 检查配置文件是否正确
  2. 数据库连接失败

    • 检查MySQL是否启动:mysqladmin ping
    • 检查数据库是否创建
    • 检查用户名密码是否正确
  3. Redis连接失败

    • 检查Redis是否启动:redis-cli ping
    • 检查端口是否正确
  4. 服务启动失败

    • 查看日志文件:tail -f logs/emotion-*.log
    • 检查端口是否被占用:lsof -i :8080

日志查看

# 查看所有服务日志
tail -f logs/*.log

# 查看特定服务日志
tail -f logs/emotion-user.log

📊 监控

健康检查端点

Prometheus指标

🤝 贡献指南

  1. Fork项目
  2. 创建功能分支:git checkout -b feature/new-feature
  3. 提交更改:git commit -am 'Add new feature'
  4. 推送分支:git push origin feature/new-feature
  5. 提交Pull Request

📄 许可证

本项目采用MIT许可证 - 查看 LICENSE 文件了解详情。