c77352877d
主要更新: 1. 统一所有微服务端口配置(19000-19008) 2. 为所有服务创建本地/测试/生产三套环境配置 3. 配置Nacos认证密码(本地:Peanut2817*#, 测试/生产:EmotionMuseum2025) 4. 优化网关路由配置,支持负载均衡和WebSocket 5. 新增emotion-websocket模块,支持实时聊天 6. 前端集成WebSocket,替代HTTP轮询 7. 添加配置验证和管理工具脚本 技术特性: - 完整的环境隔离和服务发现 - WebSocket实时通信支持 - 负载均衡路由配置 - 跨域和安全配置 - 自动重连和心跳检测
158 lines
5.4 KiB
Bash
Executable File
158 lines
5.4 KiB
Bash
Executable File
#!/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}"
|