erp-java/scripts/setup-config-center.sh

197 lines
5.0 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Nacos配置中心设置脚本
# 为所有微服务创建Nacos配置
set -e
echo "开始设置Nacos配置中心..."
# Nacos服务器地址
NACOS_SERVER="localhost:8848"
NACOS_NAMESPACE="public"
NACOS_GROUP="DEFAULT_GROUP"
# 服务列表
SERVICES=(
"user-service"
"product-service"
"order-service"
"inventory-service"
"finance-service"
"admin-service"
"file-service"
"notification-service"
"tenant-service"
"permission-service"
)
# 创建共享配置
echo "创建共享配置..."
# 1. 公共配置
cat > /tmp/common-config.yaml << 'EOF'
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Shanghai
serialization:
write-dates-as-timestamps: false
http:
encoding:
charset: UTF-8
enabled: true
management:
endpoints:
web:
exposure:
include: health,info,metrics
endpoint:
health:
show-details: always
logging:
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
level:
root: INFO
com.erp: DEBUG
EOF
# 2. 数据源配置
cat > /tmp/datasource-config.yaml << 'EOF'
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/${DB_NAME}?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: ${DB_USER:erp_user}
password: ${DB_PASSWORD:erp123456}
hikari:
connection-timeout: 30000
maximum-pool-size: 20
minimum-idle: 5
connection-test-query: SELECT 1
EOF
# 3. Redis配置
cat > /tmp/redis-config.yaml << 'EOF'
spring:
redis:
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}
password: ${REDIS_PASSWORD:}
database: 0
timeout: 3000ms
lettuce:
pool:
max-active: 20
max-idle: 10
min-idle: 5
EOF
# 上传共享配置到Nacos
echo "上传共享配置到Nacos..."
# 这里需要Nacos API调用实际使用时需要安装nacos-client或使用curl
# 为每个服务创建配置
echo "为每个服务创建配置..."
for SERVICE in "${SERVICES[@]}"; do
echo "创建服务配置: $SERVICE"
# 服务特定配置
cat > /tmp/${SERVICE}-config.yaml << EOF
# ${SERVICE} 服务配置
server:
port: \${SERVER_PORT:8080}
spring:
application:
name: ${SERVICE}
# 服务特定配置
${SERVICE}:
enabled: true
version: 1.0.0
# 业务配置
service:
name: ${SERVICE}
description: ${SERVICE} 微服务
version: 1.0.0
# 监控配置
management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheus
metrics:
export:
prometheus:
enabled: true
tags:
application: ${SERVICE}
environment: \${ENV:dev}
EOF
echo " - 已创建 ${SERVICE}-config.yaml"
done
# 创建Nacos配置导入脚本
cat > /tmp/import-nacos-config.sh << 'EOF'
#!/bin/bash
# Nacos配置导入脚本
# 需要先启动Nacos服务
NACOS_SERVER="localhost:8848"
NACOS_USER="nacos"
NACOS_PASSWORD="nacos"
# 导入共享配置
echo "导入共享配置..."
curl -X POST "http://${NACOS_SERVER}/nacos/v1/cs/configs" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "dataId=common-config.yaml&group=DEFAULT_GROUP&content=$(cat /tmp/common-config.yaml | jq -Rs . | sed 's/"/\\"/g')&type=yaml"
curl -X POST "http://${NACOS_SERVER}/nacos/v1/cs/configs" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "dataId=datasource-config.yaml&group=DEFAULT_GROUP&content=$(cat /tmp/datasource-config.yaml | jq -Rs . | sed 's/"/\\"/g')&type=yaml"
curl -X POST "http://${NACOS_SERVER}/nacos/v1/cs/configs" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "dataId=redis-config.yaml&group=DEFAULT_GROUP&content=$(cat /tmp/redis-config.yaml | jq -Rs . | sed 's/"/\\"/g')&type=yaml"
# 导入服务配置
for SERVICE in user-service product-service order-service inventory-service finance-service admin-service file-service notification-service tenant-service permission-service; do
echo "导入服务配置: $SERVICE"
curl -X POST "http://${NACOS_SERVER}/nacos/v1/cs/configs" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "dataId=${SERVICE}-config.yaml&group=DEFAULT_GROUP&content=$(cat /tmp/${SERVICE}-config.yaml | jq -Rs . | sed 's/"/\\"/g')&type=yaml"
done
echo "Nacos配置导入完成"
EOF
chmod +x /tmp/import-nacos-config.sh
echo ""
echo "配置中心设置完成!"
echo ""
echo "下一步操作:"
echo "1. 启动Nacos服务:"
echo " cd /root/.openclaw/workspace/erp-java-backend/nacos"
echo " docker-compose -f docker-compose.standalone.yml up -d"
echo ""
echo "2. 导入配置到Nacos:"
echo " /tmp/import-nacos-config.sh"
echo ""
echo "3. 启动微服务会自动从Nacos获取配置"
echo ""
echo "配置文件位置:"
echo " - 共享配置: /tmp/common-config.yaml"
echo " - 数据源配置: /tmp/datasource-config.yaml"
echo " - Redis配置: /tmp/redis-config.yaml"
echo " - 各服务配置: /tmp/*-config.yaml"
echo " - 导入脚本: /tmp/import-nacos-config.sh"