Skip to content

jacob-qu/devops-platform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Artifactory Proxy

一个基于 Go 语言实现的 Artifactory v7.77.7 制品库代理服务,支持缓存、请求限流等特性。

功能特性

  • 基本代理: 转发请求到 Artifactory,支持所有 HTTP 方法
  • 缓存功能: 自动缓存已下载的制品,减少后端请求
  • 请求限流: 使用令牌桶算法实现请求限流,防止过载
  • 健康检查: 提供健康检查端点
  • 统计信息: 提供缓存和限流统计信息

项目结构

devops-platform/
├── main.go                 # 主程序入口
├── go.mod                  # Go 模块定义
├── config/
│   ├── config.go          # 配置管理
│   └── config_test.go     # 配置测试
└── proxy/
    ├── proxy.go           # 代理核心逻辑
    ├── proxy_test.go      # 代理测试
    ├── cache.go           # 缓存实现
    ├── cache_test.go      # 缓存测试
    ├── ratelimit.go       # 限流器
    └── ratelimit_test.go  # 限流器测试

环境要求

  • Go 1.21 或更高版本

安装

  1. 克隆仓库:
git clone <repository-url>
cd devops-platform
  1. 安装依赖:
go mod download

配置

通过环境变量配置代理服务:

环境变量 默认值 说明
SERVER_PORT 8080 服务监听端口
SERVER_HOST 0.0.0.0 服务监听地址
ARTIFACTORY_URL http://localhost:8081 Artifactory 服务器地址
ARTIFACTORY_API_KEY - Artifactory API 密钥
ARTIFACTORY_USERNAME - Artifactory 用户名
ARTIFACTORY_PASSWORD - Artifactory 密码
CACHE_ENABLED true 是否启用缓存
CACHE_DIR ./cache 缓存目录
CACHE_MAX_SIZE_MB 1024 缓存最大大小 (MB)
CACHE_TTL 24h 缓存过期时间
CACHE_CLEANUP_INTERVAL 1h 缓存清理间隔
RATE_LIMIT_ENABLED true 是否启用限流
RATE_LIMIT_REQUESTS 100 时间窗口内的请求数
RATE_LIMIT_WINDOW 60s 限流时间窗口
RATE_LIMIT_BURST_SIZE 10 突发请求大小

使用

启动服务

go run main.go

使用环境变量启动

export ARTIFACTORY_URL=https://your-artifactory.com/artifactory
export ARTIFACTORY_API_KEY=your-api-key
export SERVER_PORT=9090
go run main.go

构建可执行文件

go build -o artifactory-proxy
./artifactory-proxy

API 端点

健康检查

GET /health

响应:

{"status":"healthy"}

缓存统计

GET /cache/stats

响应:

{
  "enabled": true,
  "size_bytes": 102400,
  "item_count": 5
}

限流统计

GET /ratelimit/stats

响应:

{
  "enabled": true,
  "tokens_available": 95.5,
  "requests_per_window": 100,
  "burst_size": 10
}

代理请求

GET /<repo>/<artifact-path>
HEAD /<repo>/<artifact-path>
POST /<repo>/<artifact-path>
PUT /<repo>/<artifact-path>
DELETE /<repo>/<artifact-path>

示例:

# 下载制品
curl http://localhost:8080/libs-release-local/com/example/my-app/1.0/my-app-1.0.jar

# 检查制品是否存在
curl -I http://localhost:8080/libs-release-local/com/example/my-app/1.0/my-app-1.0.jar

# 上传制品
curl -X PUT -T my-app-1.0.jar http://localhost:8080/libs-release-local/com/example/my-app/1.0/my-app-1.0.jar

测试

运行所有测试:

go test -v ./...

运行特定包的测试:

go test -v ./proxy
go test -v ./config

运行测试并显示覆盖率:

go test -v -cover ./...

测试覆盖

项目包含完整的测试用例:

  • config_test.go: 测试配置加载和环境变量解析
  • proxy_test.go: 测试代理功能、缓存、限流等
  • cache_test.go: 测试缓存的各种场景
  • ratelimit_test.go: 测试限流器功能

认证

支持两种认证方式:

  1. API Key (推荐):
export ARTIFACTORY_API_KEY=your-api-key
  1. 用户名/密码:
export ARTIFACTORY_USERNAME=your-username
export ARTIFACTORY_PASSWORD=your-password

性能优化

  • 缓存: 缓存成功响应的 GET 和 HEAD 请求
  • 限流: 使用令牌桶算法控制请求速率
  • 连接复用: HTTP 客户端自动复用连接
  • 超时控制: 可配置的读写超时

许可证

MIT License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors