#!/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}"