# 情绪博物馆后端微服务 基于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 ```bash # 下载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 ```bash # 创建数据库 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 ```bash redis-server ``` ### 2. 配置Nacos 访问 http://localhost:8848/nacos,创建以下配置: #### 命名空间 - 命名空间ID: `emotion-dev` - 命名空间名: `情绪博物馆开发环境` #### 配置文件 **common-mysql.yml** ```yaml 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** ```yaml 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** ```yaml 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. 启动微服务 #### 方式一:使用启动脚本(推荐) ```bash # 启动所有服务 ./start-services.sh # 停止所有服务 ./stop-services.sh ``` #### 方式二:手动启动 ```bash # 编译项目 mvn clean compile -DskipTests # 启动网关服务 cd emotion-gateway mvn spring-boot:run & # 启动用户服务 cd ../emotion-user mvn spring-boot:run & ``` ### 4. 验证服务状态 #### 方式一:使用测试脚本(推荐) ```bash # 运行完整测试 ./test-services.sh ``` #### 方式二:手动验证 ```bash # 健康检查 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 #### 用户注册 ```bash 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": "小测试" }' ``` #### 用户登录 ```bash curl -X POST http://localhost:8080/api/user/login \ -H "Content-Type: application/json" \ -d '{ "account": "test_user", "password": "123456" }' ``` #### 获取用户信息 ```bash curl -X GET http://localhost:8080/api/user/info/{userId} \ -H "Authorization: Bearer {token}" ``` ### Coze AI服务API #### 健康检查 ```bash curl http://localhost:8080/api/ai/coze/health ``` #### 测试AI对话 ```bash curl -X POST http://localhost:8080/api/ai/coze/test/message \ -H "Content-Type: application/json" \ -d "message=你好,我今天感觉有点焦虑&userId=test_user" ``` #### 测试情绪分析 ```bash curl -X POST http://localhost:8080/api/ai/coze/test/emotion \ -H "Content-Type: application/json" \ -d "text=我今天心情很好,阳光明媚" ``` #### 测试完整对话流程 ```bash 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. **创建模块** ```bash mkdir emotion-new-service cd emotion-new-service ``` 2. **创建pom.xml** ```xml com.emotionmuseum backend 1.0.0 emotion-new-service ``` 3. **添加到父工程** ```xml emotion-new-service ``` 4. **创建启动类** ```java @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` ### 日志查看 ```bash # 查看所有服务日志 tail -f logs/*.log # 查看特定服务日志 tail -f logs/emotion-user.log ``` ## 📊 监控 ### 健康检查端点 - 网关: http://localhost:8080/actuator/health - 用户服务: http://localhost:8081/actuator/health ### Prometheus指标 - 网关: http://localhost:8080/actuator/prometheus - 用户服务: http://localhost:8081/actuator/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](LICENSE) 文件了解详情。