diff --git a/build-and-push.sh b/build-and-push.sh new file mode 100644 index 0000000..0fd9bff --- /dev/null +++ b/build-and-push.sh @@ -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 \ No newline at end of file diff --git a/consul/config/dev/data-service.json b/consul/config/dev/data-service.json index 1221d5d..64f03a6 100644 --- a/consul/config/dev/data-service.json +++ b/consul/config/dev/data-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "data-service-1", + "id": "data-service", "tags": [ "traefik.enable=true", "traefik.http.routers.data-service.rule=PathPrefix(`/api/data`) || PathPrefix(`/api/public`)", diff --git a/consul/config/dev/emotion-service.json b/consul/config/dev/emotion-service.json index 1371776..9494079 100644 --- a/consul/config/dev/emotion-service.json +++ b/consul/config/dev/emotion-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "emotion-service-1", + "id": "emotion-service", "tags": [ "traefik.enable=true", "traefik.http.routers.emotion-service.rule=PathPrefix(`/api/emotion`) || PathPrefix(`/api/sentiment`)", diff --git a/consul/config/dev/frontend.json b/consul/config/dev/frontend.json index d9a4318..7169a33 100644 --- a/consul/config/dev/frontend.json +++ b/consul/config/dev/frontend.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "frontend-1", + "id": "frontend", "tags": [ "traefik.enable=true", "traefik.http.routers.frontend.rule=PathPrefix(`/`) || PathPrefix(`/index.html`) || PathPrefix(`/static`)", diff --git a/consul/config/dev/quant-service.json b/consul/config/dev/quant-service.json index 5c8b8ef..c049613 100644 --- a/consul/config/dev/quant-service.json +++ b/consul/config/dev/quant-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "quant-service-1", + "id": "quant-service", "tags": [ "traefik.enable=true", "traefik.http.routers.quant-service.rule=PathPrefix(`/api/quant`) || PathPrefix(`/api/strategy`)", diff --git a/consul/config/dev/recommend-service.json b/consul/config/dev/recommend-service.json index a016ad5..883457e 100644 --- a/consul/config/dev/recommend-service.json +++ b/consul/config/dev/recommend-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "recommend-service-1", + "id": "recommend-service", "tags": [ "traefik.enable=true", "traefik.http.routers.recommend-service.rule=PathPrefix(`/api/recommend`) || PathPrefix(`/api/suggest`)", diff --git a/consul/config/dev/user-service.json b/consul/config/dev/user-service.json index 3231f67..3bc7c0a 100644 --- a/consul/config/dev/user-service.json +++ b/consul/config/dev/user-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "user-service-1", + "id": "user-service", "tags": [ "traefik.enable=true", "traefik.http.routers.user-service.rule=PathPrefix(`/api/user`) || PathPrefix(`/api/auth`)", diff --git a/consul/config/prod/data-service.json b/consul/config/prod/data-service.json index 1221d5d..64f03a6 100644 --- a/consul/config/prod/data-service.json +++ b/consul/config/prod/data-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "data-service-1", + "id": "data-service", "tags": [ "traefik.enable=true", "traefik.http.routers.data-service.rule=PathPrefix(`/api/data`) || PathPrefix(`/api/public`)", diff --git a/consul/config/prod/emotion-service.json b/consul/config/prod/emotion-service.json index 1371776..9494079 100644 --- a/consul/config/prod/emotion-service.json +++ b/consul/config/prod/emotion-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "emotion-service-1", + "id": "emotion-service", "tags": [ "traefik.enable=true", "traefik.http.routers.emotion-service.rule=PathPrefix(`/api/emotion`) || PathPrefix(`/api/sentiment`)", diff --git a/consul/config/prod/frontend.json b/consul/config/prod/frontend.json index d9a4318..7169a33 100644 --- a/consul/config/prod/frontend.json +++ b/consul/config/prod/frontend.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "frontend-1", + "id": "frontend", "tags": [ "traefik.enable=true", "traefik.http.routers.frontend.rule=PathPrefix(`/`) || PathPrefix(`/index.html`) || PathPrefix(`/static`)", diff --git a/consul/config/prod/quant-service.json b/consul/config/prod/quant-service.json index 5c8b8ef..c049613 100644 --- a/consul/config/prod/quant-service.json +++ b/consul/config/prod/quant-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "quant-service-1", + "id": "quant-service", "tags": [ "traefik.enable=true", "traefik.http.routers.quant-service.rule=PathPrefix(`/api/quant`) || PathPrefix(`/api/strategy`)", diff --git a/consul/config/prod/recommend-service.json b/consul/config/prod/recommend-service.json index a016ad5..883457e 100644 --- a/consul/config/prod/recommend-service.json +++ b/consul/config/prod/recommend-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "recommend-service-1", + "id": "recommend-service", "tags": [ "traefik.enable=true", "traefik.http.routers.recommend-service.rule=PathPrefix(`/api/recommend`) || PathPrefix(`/api/suggest`)", diff --git a/consul/config/prod/user-service.json b/consul/config/prod/user-service.json index 3231f67..3bc7c0a 100644 --- a/consul/config/prod/user-service.json +++ b/consul/config/prod/user-service.json @@ -6,7 +6,7 @@ "interval": "10s", "timeout": "5s" }, - "id": "user-service-1", + "id": "user-service", "tags": [ "traefik.enable=true", "traefik.http.routers.user-service.rule=PathPrefix(`/api/user`) || PathPrefix(`/api/auth`)", diff --git a/docker-compose.aliyun.yml b/docker-compose.aliyun.yml new file mode 100644 index 0000000..630494e --- /dev/null +++ b/docker-compose.aliyun.yml @@ -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 \ No newline at end of file diff --git a/run-services.sh b/run-services.sh new file mode 100644 index 0000000..22dcaf2 --- /dev/null +++ b/run-services.sh @@ -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 \ No newline at end of file