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:
@@ -0,0 +1,268 @@
|
||||
# Emotion WebSocket 聊天服务
|
||||
|
||||
## 概述
|
||||
|
||||
emotion-websocket 是情绪博物馆项目的WebSocket聊天微服务,提供实时聊天功能,支持用户与AI的实时对话。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- ✅ WebSocket实时通信
|
||||
- ✅ 用户与AI实时对话
|
||||
- ✅ 会话管理
|
||||
- ✅ 消息状态跟踪
|
||||
- ✅ 心跳检测
|
||||
- ✅ 在线用户管理
|
||||
- ✅ 消息广播
|
||||
- ✅ 异步AI响应处理
|
||||
|
||||
## 技术栈
|
||||
|
||||
- Spring Boot 3.0.2
|
||||
- Spring WebSocket
|
||||
- STOMP协议
|
||||
- SockJS
|
||||
- Spring Cloud Alibaba
|
||||
- Nacos服务发现
|
||||
- OpenFeign服务调用
|
||||
- MyBatis Plus
|
||||
- MySQL
|
||||
- Redis
|
||||
|
||||
## 端口配置
|
||||
|
||||
- 服务端口: 19007
|
||||
- WebSocket端点: `/ws/chat`
|
||||
|
||||
## API接口
|
||||
|
||||
### WebSocket端点
|
||||
|
||||
```
|
||||
ws://localhost:19007/ws/chat
|
||||
```
|
||||
|
||||
### STOMP消息映射
|
||||
|
||||
- `/app/chat.send` - 发送聊天消息
|
||||
- `/app/chat.connect` - 用户连接
|
||||
- `/app/chat.disconnect` - 用户断开连接
|
||||
- `/app/chat.heartbeat` - 心跳检测
|
||||
|
||||
### 订阅端点
|
||||
|
||||
- `/user/queue/messages` - 用户私有消息
|
||||
- `/topic/conversation/{conversationId}` - 会话消息
|
||||
- `/topic/broadcast` - 广播消息
|
||||
|
||||
### REST API
|
||||
|
||||
#### 发送测试消息
|
||||
```http
|
||||
POST /websocket/send?userId={userId}&message={message}
|
||||
```
|
||||
|
||||
#### 广播测试消息
|
||||
```http
|
||||
POST /websocket/broadcast?message={message}
|
||||
```
|
||||
|
||||
#### 获取在线用户
|
||||
```http
|
||||
GET /websocket/online-users
|
||||
```
|
||||
|
||||
## 消息格式
|
||||
|
||||
### 聊天请求 (ChatRequest)
|
||||
```json
|
||||
{
|
||||
"conversationId": "会话ID",
|
||||
"content": "消息内容",
|
||||
"senderId": "发送者ID",
|
||||
"senderType": "USER|GUEST|AI|SYSTEM",
|
||||
"messageType": "TEXT|TYPING|SYSTEM|ERROR|HEARTBEAT|CONNECTION|AI_THINKING"
|
||||
}
|
||||
```
|
||||
|
||||
### WebSocket消息 (WebSocketMessage)
|
||||
```json
|
||||
{
|
||||
"messageId": "消息ID",
|
||||
"conversationId": "会话ID",
|
||||
"type": "TEXT|TYPING|SYSTEM|ERROR|HEARTBEAT|CONNECTION|AI_THINKING",
|
||||
"content": "消息内容",
|
||||
"senderId": "发送者ID",
|
||||
"senderType": "USER|GUEST|AI|SYSTEM",
|
||||
"status": "SENDING|SENT|DELIVERED|READ|FAILED",
|
||||
"createTime": "2025-07-17 15:30:00",
|
||||
"data": {}
|
||||
}
|
||||
```
|
||||
|
||||
## 启动方式
|
||||
|
||||
### 本地开发启动
|
||||
|
||||
1. 确保MySQL和Redis服务已启动
|
||||
2. 确保Nacos服务已启动
|
||||
3. 启动emotion-ai服务(WebSocket服务依赖AI服务)
|
||||
|
||||
```bash
|
||||
# 进入项目根目录
|
||||
cd backend
|
||||
|
||||
# 启动单个服务
|
||||
cd emotion-websocket
|
||||
mvn spring-boot:run -Dspring-boot.run.profiles=local
|
||||
|
||||
# 或使用统一启动脚本
|
||||
./start-services.sh
|
||||
```
|
||||
|
||||
### Docker启动
|
||||
|
||||
```bash
|
||||
# 构建镜像
|
||||
cd emotion-websocket
|
||||
docker build -t emotion-websocket:1.0.0 .
|
||||
|
||||
# 运行容器
|
||||
docker run -d \
|
||||
--name emotion-websocket \
|
||||
-p 19007:19007 \
|
||||
-e SPRING_PROFILES_ACTIVE=prod \
|
||||
emotion-websocket:1.0.0
|
||||
```
|
||||
|
||||
## 测试方法
|
||||
|
||||
### 1. 使用内置测试页面
|
||||
|
||||
访问: http://localhost:19007/websocket-test.html
|
||||
|
||||
### 2. 使用JavaScript客户端
|
||||
|
||||
```javascript
|
||||
// 连接WebSocket
|
||||
const socket = new SockJS('http://localhost:19007/ws/chat');
|
||||
const stompClient = Stomp.over(socket);
|
||||
|
||||
stompClient.connect({}, function (frame) {
|
||||
console.log('Connected: ' + frame);
|
||||
|
||||
// 订阅消息
|
||||
stompClient.subscribe('/user/queue/messages', function (message) {
|
||||
const messageData = JSON.parse(message.body);
|
||||
console.log('Received:', messageData);
|
||||
});
|
||||
|
||||
// 发送消息
|
||||
const chatRequest = {
|
||||
content: "Hello AI!",
|
||||
senderId: "test-user",
|
||||
senderType: "USER",
|
||||
messageType: "TEXT",
|
||||
conversationId: "test-conversation"
|
||||
};
|
||||
|
||||
stompClient.send("/app/chat.send", {}, JSON.stringify(chatRequest));
|
||||
});
|
||||
```
|
||||
|
||||
### 3. 使用REST API测试
|
||||
|
||||
```bash
|
||||
# 发送测试消息
|
||||
curl -X POST "http://localhost:19007/websocket/send?userId=test-user&message=Hello"
|
||||
|
||||
# 广播消息
|
||||
curl -X POST "http://localhost:19007/websocket/broadcast?message=System Message"
|
||||
|
||||
# 查看在线用户
|
||||
curl -X GET "http://localhost:19007/websocket/online-users"
|
||||
```
|
||||
|
||||
## 配置说明
|
||||
|
||||
### application.yml
|
||||
```yaml
|
||||
server:
|
||||
port: 19007
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: emotion-websocket
|
||||
```
|
||||
|
||||
### application-local.yml
|
||||
```yaml
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: localhost:8848
|
||||
```
|
||||
|
||||
## 日志配置
|
||||
|
||||
日志文件位置: `logs/emotion-websocket.log`
|
||||
|
||||
查看日志:
|
||||
```bash
|
||||
tail -f logs/emotion-websocket.log
|
||||
```
|
||||
|
||||
## 监控端点
|
||||
|
||||
- 健康检查: http://localhost:19007/actuator/health
|
||||
- 指标监控: http://localhost:19007/actuator/metrics
|
||||
- Prometheus: http://localhost:19007/actuator/prometheus
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. WebSocket服务依赖emotion-ai服务,请确保AI服务已启动
|
||||
2. 需要配置正确的Nacos服务发现地址
|
||||
3. 确保数据库连接配置正确
|
||||
4. 生产环境需要配置适当的跨域策略
|
||||
5. 建议配置负载均衡和会话粘性
|
||||
|
||||
## 故障排查
|
||||
|
||||
### 常见问题
|
||||
|
||||
1. **连接失败**
|
||||
- 检查服务是否启动: `curl http://localhost:19007/actuator/health`
|
||||
- 检查端口是否被占用: `lsof -i :19007`
|
||||
|
||||
2. **AI回复失败**
|
||||
- 检查emotion-ai服务是否正常
|
||||
- 查看日志中的Feign调用错误
|
||||
|
||||
3. **消息发送失败**
|
||||
- 检查WebSocket连接状态
|
||||
- 查看浏览器控制台错误信息
|
||||
|
||||
### 日志级别调整
|
||||
|
||||
```yaml
|
||||
logging:
|
||||
level:
|
||||
com.emotionmuseum.websocket: DEBUG
|
||||
org.springframework.web.socket: DEBUG
|
||||
```
|
||||
|
||||
## 开发指南
|
||||
|
||||
### 添加新的消息类型
|
||||
|
||||
1. 在`WebSocketMessage.MessageType`枚举中添加新类型
|
||||
2. 在`ChatWebSocketController`中添加对应的处理方法
|
||||
3. 在`ChatWebSocketServiceImpl`中实现具体逻辑
|
||||
|
||||
### 扩展功能
|
||||
|
||||
- 添加文件传输支持
|
||||
- 实现消息持久化
|
||||
- 添加消息加密
|
||||
- 实现群聊功能
|
||||
- 添加消息撤回功能
|
||||
Reference in New Issue
Block a user