#!/bin/bash # ============================================================================ # 批量更新所有微服务的Nacos密码配置 # 本地环境: Peanut2817*# # 测试和生产环境: EmotionMuseum2025 # ============================================================================ # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color echo -e "${BLUE}===========================================${NC}" echo -e "${BLUE}批量更新微服务Nacos密码配置${NC}" echo -e "${BLUE}===========================================${NC}" # 服务列表 SERVICES="emotion-user emotion-ai emotion-record emotion-growth emotion-explore emotion-reward emotion-websocket emotion-stats" # 更新本地环境密码 update_local_password() { local service_name=$1 local config_file="backend/${service_name}/src/main/resources/application-local.yml" if [ ! -f "$config_file" ]; then echo -e "${RED} ❌ 配置文件不存在: $config_file${NC}" return 1 fi # 使用sed替换密码 sed -i.bak 's/password: nacos$/password: Peanut2817*#/g' "$config_file" # 检查是否替换成功 if grep -q "password: Peanut2817*#" "$config_file"; then echo -e "${GREEN} ✅ 本地环境密码更新成功${NC}" rm -f "${config_file}.bak" return 0 else echo -e "${RED} ❌ 本地环境密码更新失败${NC}" return 1 fi } # 更新测试环境密码 update_test_password() { local service_name=$1 local config_file="backend/${service_name}/src/main/resources/application-test.yml" if [ ! -f "$config_file" ]; then echo -e "${RED} ❌ 配置文件不存在: $config_file${NC}" return 1 fi # 使用sed替换密码 sed -i.bak 's/password: nacos$/password: EmotionMuseum2025/g' "$config_file" # 检查是否替换成功 if grep -q "password: EmotionMuseum2025" "$config_file"; then echo -e "${GREEN} ✅ 测试环境密码更新成功${NC}" rm -f "${config_file}.bak" return 0 else echo -e "${RED} ❌ 测试环境密码更新失败${NC}" return 1 fi } # 更新生产环境密码 update_prod_password() { local service_name=$1 local config_file="backend/${service_name}/src/main/resources/application-prod.yml" if [ ! -f "$config_file" ]; then echo -e "${RED} ❌ 配置文件不存在: $config_file${NC}" return 1 fi # 使用sed替换密码 sed -i.bak 's/password: nacos$/password: EmotionMuseum2025/g' "$config_file" # 检查是否替换成功 if grep -q "password: EmotionMuseum2025" "$config_file"; then echo -e "${GREEN} ✅ 生产环境密码更新成功${NC}" rm -f "${config_file}.bak" return 0 else echo -e "${RED} ❌ 生产环境密码更新失败${NC}" return 1 fi } # 统计结果 total_services=0 success_services=0 failed_services=0 # 处理每个服务 for service_name in $SERVICES; do echo -e "${YELLOW}更新服务: $service_name${NC}" total_services=$((total_services + 1)) # 检查服务目录是否存在 if [ ! -d "backend/$service_name" ]; then echo -e "${RED} ❌ 服务目录不存在: backend/$service_name${NC}" failed_services=$((failed_services + 1)) continue fi # 更新各环境密码 service_success=true if ! update_local_password "$service_name"; then service_success=false fi if ! update_test_password "$service_name"; then service_success=false fi if ! update_prod_password "$service_name"; then service_success=false fi if [ "$service_success" = true ]; then echo -e "${GREEN} ✅ $service_name 密码更新完成${NC}" success_services=$((success_services + 1)) else echo -e "${RED} ❌ $service_name 密码更新失败${NC}" failed_services=$((failed_services + 1)) fi echo "" done # 显示统计结果 echo -e "${BLUE}===========================================${NC}" echo -e "${BLUE}密码更新结果统计${NC}" echo -e "${BLUE}===========================================${NC}" echo -e "总服务数: ${BLUE}$total_services${NC}" echo -e "更新成功: ${GREEN}$success_services${NC}" echo -e "更新失败: ${RED}$failed_services${NC}" if [ $failed_services -eq 0 ]; then echo -e "${GREEN}🎉 所有服务密码更新完成!${NC}" echo "" echo -e "${YELLOW}密码配置:${NC}" echo -e "本地环境: ${GREEN}Peanut2817*#${NC}" echo -e "测试环境: ${GREEN}EmotionMuseum2025${NC}" echo -e "生产环境: ${GREEN}EmotionMuseum2025${NC}" exit 0 else echo -e "${RED}❌ 有 $failed_services 个服务密码更新失败${NC}" exit 1 fi