refactor(consul): 统一服务ID命名并添加部署脚本

- 移除服务ID中的"-1"后缀以保持命名一致性
- 新增build-and-push.sh和run-services.sh部署脚本
- 添加docker-compose.aliyun.yml配置文件
This commit is contained in:
fanfpy 2025-07-17 11:24:39 +08:00
parent 66c8049c64
commit f12448c7d8
15 changed files with 228 additions and 12 deletions

64
build-and-push.sh Normal file
View File

@ -0,0 +1,64 @@
#!/bin/bash
set -e
# 固定版本号为latest
VERSION="latest"
REGISTRY="crpi-amr0dt5e5pywqwaf.cn-hangzhou.personal.cr.aliyuncs.com"
NAMESPACE="testrrr"
USERNAME="2324802641zhengniannian@gmail.com"
SERVICES=("data-service" "emotion-service" "frontend" "quant-service" "recommend-service" "user-service")
# 登录阿里云Docker仓库
echo "登录阿里云Docker仓库..."
docker login --username=$USERNAME $REGISTRY
if [ $? -ne 0 ]; then
echo "登录失败,请检查用户名和密码"
exit 1
fi
# 构建并推送所有服务镜像
for service in "${SERVICES[@]}"; do
echo "\n==================== 处理 $service ===================="
SERVICE_DIR="services/$service"
IMAGE_NAME="$REGISTRY/$NAMESPACE/$service:$VERSION"
# 检查服务目录是否存在
if [ ! -d "$SERVICE_DIR" ]; then
echo "错误: 服务目录 $SERVICE_DIR 不存在"
exit 1
fi
# 前端服务需要特殊处理npm构建
if [ "$service" = "frontend" ]; then
echo "正在前端项目构建..."
cd $SERVICE_DIR
npm install
npm run build
if [ $? -ne 0 ]; then
echo "前端构建失败"
exit 1
fi
cd - > /dev/null
fi
# 构建Docker镜像
echo "正在构建镜像: $IMAGE_NAME"
docker build -t $IMAGE_NAME $SERVICE_DIR
if [ $? -ne 0 ]; then
echo "镜像构建失败"
exit 1
fi
# 推送Docker镜像
echo "正在推送镜像: $IMAGE_NAME"
docker push $IMAGE_NAME
if [ $? -ne 0 ]; then
echo "镜像推送失败"
exit 1
fi
echo "$service 处理完成"
done
echo "\n所有服务镜像构建和推送成功完成"
exit 0

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "data-service-1", "id": "data-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.data-service.rule=PathPrefix(`/api/data`) || PathPrefix(`/api/public`)", "traefik.http.routers.data-service.rule=PathPrefix(`/api/data`) || PathPrefix(`/api/public`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "emotion-service-1", "id": "emotion-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.emotion-service.rule=PathPrefix(`/api/emotion`) || PathPrefix(`/api/sentiment`)", "traefik.http.routers.emotion-service.rule=PathPrefix(`/api/emotion`) || PathPrefix(`/api/sentiment`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "frontend-1", "id": "frontend",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.frontend.rule=PathPrefix(`/`) || PathPrefix(`/index.html`) || PathPrefix(`/static`)", "traefik.http.routers.frontend.rule=PathPrefix(`/`) || PathPrefix(`/index.html`) || PathPrefix(`/static`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "quant-service-1", "id": "quant-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.quant-service.rule=PathPrefix(`/api/quant`) || PathPrefix(`/api/strategy`)", "traefik.http.routers.quant-service.rule=PathPrefix(`/api/quant`) || PathPrefix(`/api/strategy`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "recommend-service-1", "id": "recommend-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.recommend-service.rule=PathPrefix(`/api/recommend`) || PathPrefix(`/api/suggest`)", "traefik.http.routers.recommend-service.rule=PathPrefix(`/api/recommend`) || PathPrefix(`/api/suggest`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "user-service-1", "id": "user-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.user-service.rule=PathPrefix(`/api/user`) || PathPrefix(`/api/auth`)", "traefik.http.routers.user-service.rule=PathPrefix(`/api/user`) || PathPrefix(`/api/auth`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "data-service-1", "id": "data-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.data-service.rule=PathPrefix(`/api/data`) || PathPrefix(`/api/public`)", "traefik.http.routers.data-service.rule=PathPrefix(`/api/data`) || PathPrefix(`/api/public`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "emotion-service-1", "id": "emotion-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.emotion-service.rule=PathPrefix(`/api/emotion`) || PathPrefix(`/api/sentiment`)", "traefik.http.routers.emotion-service.rule=PathPrefix(`/api/emotion`) || PathPrefix(`/api/sentiment`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "frontend-1", "id": "frontend",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.frontend.rule=PathPrefix(`/`) || PathPrefix(`/index.html`) || PathPrefix(`/static`)", "traefik.http.routers.frontend.rule=PathPrefix(`/`) || PathPrefix(`/index.html`) || PathPrefix(`/static`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "quant-service-1", "id": "quant-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.quant-service.rule=PathPrefix(`/api/quant`) || PathPrefix(`/api/strategy`)", "traefik.http.routers.quant-service.rule=PathPrefix(`/api/quant`) || PathPrefix(`/api/strategy`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "recommend-service-1", "id": "recommend-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.recommend-service.rule=PathPrefix(`/api/recommend`) || PathPrefix(`/api/suggest`)", "traefik.http.routers.recommend-service.rule=PathPrefix(`/api/recommend`) || PathPrefix(`/api/suggest`)",

View File

@ -6,7 +6,7 @@
"interval": "10s", "interval": "10s",
"timeout": "5s" "timeout": "5s"
}, },
"id": "user-service-1", "id": "user-service",
"tags": [ "tags": [
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.user-service.rule=PathPrefix(`/api/user`) || PathPrefix(`/api/auth`)", "traefik.http.routers.user-service.rule=PathPrefix(`/api/user`) || PathPrefix(`/api/auth`)",

110
docker-compose.aliyun.yml Normal file
View File

@ -0,0 +1,110 @@
version: '3.8'
networks:
microservice-network:
driver: bridge
services:
consul:
image: consul:1.11.4
container_name: ari-consul
command: consul agent -server -bootstrap-expect=1 ${CONSUL_DEV_MODE} -config-dir=/consul/config -client=0.0.0.0
ports:
- "8500:8500"
- "8600:8600/udp"
volumes:
- ./consul/config:/consul/config
environment:
- CONSUL_DEV_MODE=-dev
networks:
- microservice-network
restart: unless-stopped
traefik:
image: traefik:v2.9
container_name: ari-traefik
command:
- "--providers.consulcatalog=true"
- "--providers.consulcatalog.endpoint.address=ari-consul:8500"
- "--providers.consulcatalog.service.name=traefik"
- "--api.dashboard=true"
- "--log.level=INFO"
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- ./traefik/certs:/etc/traefik/certs
- ./traefik/config:/etc/traefik/config
- /var/run/docker.sock:/var/run/docker.sock:ro
depends_on:
- consul
networks:
- microservice-network
restart: unless-stopped
data-service:
image: crpi-amr0dt5e5pywqwaf.cn-hangzhou.personal.cr.aliyuncs.com/testrrr/data-service:latest
container_name: data-service
ports:
- "8001:80"
networks:
- microservice-network
depends_on:
- consul
restart: unless-stopped
emotion-service:
image: crpi-amr0dt5e5pywqwaf.cn-hangzhou.personal.cr.aliyuncs.com/testrrr/emotion-service:latest
container_name: emotion-service
ports:
- "8002:80"
networks:
- microservice-network
depends_on:
- consul
restart: unless-stopped
frontend:
image: crpi-amr0dt5e5pywqwaf.cn-hangzhou.personal.cr.aliyuncs.com/testrrr/frontend:latest
container_name: frontend
ports:
- "8000:80"
networks:
- microservice-network
depends_on:
- consul
restart: unless-stopped
quant-service:
image: crpi-amr0dt5e5pywqwaf.cn-hangzhou.personal.cr.aliyuncs.com/testrrr/quant-service:latest
container_name: quant-service
ports:
- "8005:80"
networks:
- microservice-network
depends_on:
- consul
restart: unless-stopped
recommend-service:
image: crpi-amr0dt5e5pywqwaf.cn-hangzhou.personal.cr.aliyuncs.com/testrrr/recommend-service:latest
container_name: recommend-service
ports:
- "8003:80"
networks:
- microservice-network
depends_on:
- consul
restart: unless-stopped
user-service:
image: crpi-amr0dt5e5pywqwaf.cn-hangzhou.personal.cr.aliyuncs.com/testrrr/user-service:latest
container_name: user-service
ports:
- "8004:80"
networks:
- microservice-network
depends_on:
- consul
restart: unless-stopped

42
run-services.sh Normal file
View File

@ -0,0 +1,42 @@
#!/bin/bash
set -e
# 配置参数
REGISTRY="crpi-amr0dt5e5pywqwaf.cn-hangzhou.personal.cr.aliyuncs.com"
VERSION="latest"
COMPOSE_FILE="docker-compose.aliyun.yml"
USERNAME="2324802641zhengniannian@gmail.com"
# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
echo "错误: Docker未安装请先安装Docker"
exit 1
fi
# 检查Docker Compose是否安装使用docker compose子命令
if ! docker compose version &> /dev/null; then
echo "错误: Docker Compose未安装请先安装Docker Compose"
exit 1
fi
# 登录阿里云Docker仓库
echo "登录阿里云Docker仓库..."
docker login --username=$USERNAME $REGISTRY
if [ $? -ne 0 ]; then
echo "错误: 仓库登录失败,请检查用户名和密码"
exit 1
fi
# 启动服务
echo "正在启动服务 (版本: $VERSION)..."
VERSION=$VERSION docker compose -f $COMPOSE_FILE up -d
# 检查服务状态
echo "\n服务状态:"
docker compose -f $COMPOSE_FILE ps
# 显示日志命令提示
echo "\n查看服务日志命令: docker compose -f $COMPOSE_FILE logs -f"
echo "停止服务命令: docker compose -f $COMPOSE_FILE down"
echo "\n服务启动完成"
exit 0