实践篇: 08-部署代码依赖代理仓库

在开发和构建过程中,应用程序通常需要下载大量的第三方依赖包(如 Go 模块、npm 包、PyPI 包、Maven 构件)。直接从公共源下载可能速度慢、不稳定,或者在无法访问外网的 CI/CD 环境中不可行。

代理仓库 (Artifact Proxy / Repository Manager) 可以解决这些问题。它作为内部的依赖缓存服务器:

  • 加速下载: 首次下载后缓存依赖,后续请求直接从内网获取。
  • 提高稳定性: 减少对外部公共源的依赖。
  • 离线支持: 缓存的依赖可在离线环境中使用。
  • (可选) 托管私有包: 可以发布和管理公司内部的私有库。

本篇将指导你使用 Docker Compose 部署一套常用的代理仓库服务:

环境说明:

  • 路径二用户 (模拟企业环境): 确保 hosts 文件中 goproxy.leops.local, verdaccio.leops.local, nexus.leops.local 指向你的服务器 IP。后续 Nginx 配置会将这些域名代理到相应的服务端口。
  • 路径一用户 (本地环境): 你将使用 http://localhost:<端口号> 访问这些代理服务。

整合部署 (使用 Docker Compose)

我们将把三个服务整合到一个 docker-compose.yml 文件中进行部署。

  1. 创建目录结构:
# 切换到合适的安装目录,例如 /data
cd /data
# 创建主目录
mkdir dependency-proxies && cd dependency-proxies
# 分别为各服务创建数据和配置目录
mkdir -p goproxy/cache
mkdir -p verdaccio/conf verdaccio/storage verdaccio/plugins
mkdir -p nexus/data
  1. 准备 Verdaccio 配置文件 (verdaccio/conf/config.yaml):

创建 verdaccio/conf/config.yaml 文件,并填入以下内容,配置上游代理为淘宝镜像源:

# Verdaccio 存储和插件目录
storage: /verdaccio/storage/data
plugins: /verdaccio/plugins

# Web UI 配置
web:
  title: Verdaccio

# 用户认证 (默认允许匿名访问和发布,生产环境应加强)
auth:
  htpasswd:
    file: /verdaccio/storage/htpasswd# 如果需要用户认证,取消注释并创建 htpasswd 文件

# 上游代理配置
uplinks:
  # 定义一个名为 'npmjs' 的上游,指向淘宝镜像源
  npmjs:
    url: https://registry.npmmirror.com/
    timeout: 30s# 增加超时时间

# 包访问规则
packages:
  # 范围包 (@scope/*)
  '@*/*':
    access: $all# 允许所有人访问 (拉取)
    publish: $authenticated# 仅允许认证用户发布
    unpublish: $authenticated# 仅允许认证用户取消发布
    proxy: npmjs# 如果本地没有,从 npmjs 上游拉取

# 非范围包
'**':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs

# 服务器监听端口 (容器内部)
listen: 0.0.0.0:4873

max_body_size: 100mb

# 日志配置
logs: { type:stdout, format:pretty, level:info }

# 启用包的 README 显示
middlewares:
  audit:
    enabled: true

i18n:
  web: zh-CN
  1. 创建 docker-compose.yml 文件:
services:
  # --- Go 依赖代理 ---
goproxy:
    container_name: goproxy
    image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/ghcr.io/goproxy/goproxy:0.19.2
    restart: always
    # 容器内监听 8080, 代理 goproxy.cn, 不代理私有仓库 git.leops.local
    command: "-listen=0.0.0.0:80 -cacheDir=/go/cache -proxy https://goproxy.cn -exclude 'git.leops.local'"
    ports:
      - "8082:80"
    volumes:
      - ./goproxy/cache:/go/cache# 持久化缓存
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
    environment:
      - TZ=Asia/Shanghai

# --- Node.js (npm) 依赖代理 ---
verdaccio:
    container_name: verdaccio
    image: verdaccio/verdaccio:6.1
    restart: always
    ports:
      - "8083:4873"
    volumes:
      - ./verdaccio/storage:/verdaccio/storage# 持久化存储
      - ./verdaccio/conf:/verdaccio/conf# 挂载配置目录
      - ./verdaccio/plugins:/verdaccio/plugins# 挂载插件目录
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
    environment:
      - TZ=Asia/Shanghai
      - VERDACCIO_PORT=4873
      - VERDACCIO_PUBLIC_URL=//verdaccio.leops.local

# --- Python (pip) & Java (Maven/Gradle) 依赖代理 ---
nexus:
    container_name: nexus
    image: sonatype/nexus3:3.79.1-alpine
    restart: always
    user: root
    ports:
      - "8084:8081"
    volumes:
      - ./nexus/data:/nexus-data
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
    environment:
      - TZ=Asia/Shanghai
  1. 启动服务:
sudo docker compose up -d

Nexus 首次启动需要较长时间初始化,请耐心等待。

Nginx 代理配置 (路径二用户)

如果你使用了路径二的环境,需要配置 Nginx 将域名转发到对应的容器端口。在 Nginx 的 conf.d 目录下 (例如准备篇中的 /data/nginx/nginx_data/conf.d`) 创建对应的配置文件:

goproxy.conf:

server {
    listen 80;
    server_name goproxy.leops.local;

    access_log /var/log/nginx/goproxy_access.log;
    error_log /var/log/nginx/goproxy_error.log;

    location / {
        proxy_pass http://192.168.77.140:8082;
        proxy_set_header  Host              $http_host;
        proxy_set_header  X-Real-IP         $remote_addr;
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;

        proxy_buffering off;
        proxy_request_buffering off;

        proxy_send_timeout 900;
        proxy_read_timeout 900;
    }
}

verdaccio.conf:

server {
    listen 80;
    server_name verdaccio.leops.local;

    access_log /var/log/nginx/verdaccio_access.log;
    error_log /var/log/nginx/verdaccio_error.log;

    location / {
        proxy_pass http://192.168.77.140:8083;
        proxy_set_header  Host              $http_host;
        proxy_set_header  X-Real-IP         $remote_addr;
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;

        proxy_buffering off;
        proxy_request_buffering off;

        proxy_send_timeout 900;
        proxy_read_timeout 900;
    }
}

nexus.conf:

server {
    listen 80;
    server_name nexus.leops.local;

    access_log /var/log/nginx/nexus_access.log;
    error_log /var/log/nginx/nexus_error.log;

    location / {
        proxy_pass http://10.8.10.129:8084;
        proxy_set_header  Host              $http_host;
        proxy_set_header  X-Real-IP         $remote_addr;
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;

        proxy_buffering off;
        proxy_request_buffering off;

        proxy_send_timeout 900;
        proxy_read_timeout 900;
    }
}

创建完这些配置文件后,重新加载 Nginx 配置:

# 在运行 Nginx 的服务器上执行
docker exec nginx-proxy sh -c "nginx -t && nginx -s reload"

配置 Nexus (用于 Python 和 Java)

Nexus 启动后,需要通过其 Web UI 进行配置。

  1. 获取初始管理员密码:
# 等待 Nexus 完全启动 (可能需要几分钟)
sudo cat ./nexus/data/admin.password

# 复制显示的密码。
  1. 登录 Nexus UI:
  • 访问 http://<your_host_ip>:8081 (路径一用户) 或 http://nexus.leops.local (路径二用户,需配置 Nginx 代理)。
  • 使用用户名 admin 和上一步获取的密码登录。
  • 首次登录会引导你修改密码并配置匿名访问权限(建议禁止匿名访问以提高安全性)。
  1. 配置 PyPI 代理:
  • 点击左侧菜单的齿轮图标 (Administration) -> Repositories。
  • 点击 “Create repository”。
    • 选择 “pypi (proxy)”。
    • Name: pypi-proxy (或其他你喜欢的名字)
    • Remote storage: https://mirrors.aliyun.com/pypi/simple/ (或其他国内源)
    • Blob store: 默认即可。
    • 点击 “Create repository”。
  • 再次点击 “Create repository”。
    • 选择 “pypi (group)”。
    • Name: pypi-group
    • Blob store: 默认即可。
    • 将右侧 “Available” 下的 pypi-proxy 和 pypi-hosted (如果需要发布私有包) 移动到左侧 “Members”。
    • 点击 “Create repository”。
  • 你的 PyPI 代理地址将是: http://nexus.leops.local/repository/pypi-group/simple
  1. 配置 Maven 代理:
  • 点击 “Create repository” -> “maven2 (proxy)”。
    • Name: maven-aliyun
    • Remote storage: https://maven.aliyun.com/repository/public/
    • Layout policy: Strict (推荐)
    • 点击 “Create repository”。
  • 点击 “Create repository” -> “maven2 (group)”。
    • Name: maven-public
    • 将 maven-aliyun, maven-central (自带的中央仓库代理), maven-releases, maven-snapshots (如果需要托管私有构件) 加入 “Members”。
    • 点击 “Create repository”。
  • 你的 Maven 代理地址将是: http://nexus.leops.local/repository/maven-public/

配置客户端使用代理

部署好代理服务后,需要配置你的开发环境或 Dockerfile 来使用它们。

  1. 配置 Go 使用 goproxy:
    设置环境变量 GOPROXY。

本地环境/Dockerfile:

export GOPROXY=http://goproxy.leops.local,direct
export GOPRIVATE=git.leops.local
  1. 配置 npm 使用 Verdaccio:

本地环境:

npm config set registry http://verdaccio.leops.local/
npm config get registry

Dockerfile 中: 可以在 RUN npm install 前加上 --registry 参数,或在项目中添加 .npmrc 文件:

# Dockerfile (路径一示例)
RUN npm install --registry=http://verdaccio.leops.local

# 或者 COPY .npmrc 文件
# COPY .npmrc /app/.npmrc
# RUN npm install

.npmrc 文件内容:

registry=http://verdaccio.leops.local
  1. 配置 pip 使用 Nexus PyPI 代理:

修 改 pip.conf (Linux/Mac: ~/.config/pip/pip.conf 或 ~/.pip/pip.conf; Windows: %APPDATA%\pip\pip.ini) 文件,或者在 pip install 时使用 -i 参数。

pip.conf / pip.ini:

[global]
index-url = http://nexus.leops.local/repository/pypi-group/simple

[install]
trusted-host = nexus.leops.local

Dockerfile 中:

# 路径一示例 (使用 -i 参数)
RUN pip install --no-cache-dir -i http://nexus.leops.local/repository/pypi-group/simple --trusted-host nexus.leops.local -r requirements.txt

# 或者 COPY pip.conf 文件
# COPY pip.conf /etc/pip.conf
# RUN pip install --no-cache-dir -r requirements.txt
  1. 配置 Maven 使用 Nexus Maven 代理:

修改 Maven 的 settings.xml 文件 (通常在 ~/.m2/settings.xml)。

settings.xml:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<mirrors>
    <mirror>
      <id>nexus</id>
      <name>Nexus Repository</name>
      <url>http://nexus.leops.local/repository/maven-public/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
</mirrors>

<profiles>
    <profile>
      <id>nexus-profile</id>
      <repositories>
        <repository>
          <id>nexus</id>
          <url>http://nexus.leops.local/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus</id>
          <url>http://nexus.leops.local/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
</profiles>

<activeProfiles>
    <activeProfile>nexus-profile</activeProfile>
</activeProfiles>

</settings>

Dockerfile 中: 通常将配置好的 settings.xml 文件 COPY 到镜像中 Maven 使用的位置 (如 /usr/share/maven/conf/settings.xml 或 /root/.m2/settings.xml)。

部署并配置好这些代理仓库后,你的团队在进行开发和构建时将能享受到更快的依赖下载速度和更高的稳定性。记得将客户端配置方法分享给团队成员!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

企鹅侠客

您的打赏是我创作旅程中的关键燃

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值