说在前面
- 外网环境:WSL 1.2.5.0
- 内网环境:CentOS Linux release 7.3
- 代理工具:Athens(version 0.2.0)
- 外网go版本:go1.20.2 linux/amd64
关于Athens
外网构建Athens
- 参照官网的构建流程即可,这里我选择的是直接编译源码。
git clone https://github.com/gomods/athens cd athens make build-ver VERSION="0.2.0"
- 注意,athens的编译有一大堆的依赖,最好在外网编译,这里直接是在
wsl
里构建的,可以设置一下go proxy
go env -w GOPROXY="https://mirrors.tencent.com/go/"
- 编译好之后得到一个二进制文件,然后把他拷贝到内网即可
配置内网Athens
- 将外网编译好的二进制文件挪到内网(CentOS7)后,是可以运行的,可以将Athens源码也一并挪到内网
# 目录结构如下 . ├── athens* ├── cache/ ├── config.dev.toml ├── run.sh
- 由于这边内网的规则比较严格,无法访问外网,所以在配置上可以这样:
# DownloadMode defines how Athens behaves when a module@version # is not found in storage. There are 4 options: # 1. "sync" (default): download the module synchronously and # return the results to the client. # 2. "async": return 404, but asynchronously store the module # in the storage backend. # 3. "redirect": return a 301 redirect status to the client # with the base URL as the DownloadRedirectURL from below. # 4. "async_redirect": same as option number 3 but it will # asynchronously store the module to the backend. # 5. "none": return 404 if a module is not found and do nothing. # 6. "file:<path>": will point to an HCL file that specifies # any of the 5 options above based on different import paths. # 7. "custom:<base64-encoded-hcl>" is the same as option 6 # but the file is fully encoded in the option. This is # useful for using an environment variable in serverless # deployments. # Env override: ATHENS_DOWNLOAD_MODE DownloadMode = "none" # StorageType sets the type of storage backend the proxy will use. # Possible values are memory, disk, mongo, gcp, minio, s3, azureblob, external # Defaults to memory # Env override: ATHENS_STORAGE_TYPE StorageType = "disk" [Storage] [Storage.Disk] # RootPath is the Athens Disk Root folder # Env override: ATHENS_DISK_STORAGE_ROOT RootPath = "/path/on/filesystem"
DownloadMode
填none
,这样在找不到module时,athens不会去尝试从VCS获取module,提高效率StorageType
按需选择即可,这里选择disk
,对应下面的RootPath
填存放module
的路径- 然后运行Athens
nohup ./athens -config_file=./config.dev.toml &
- 查看已缓存module
curl http://127.0.0.1:3000/catalog
怎么将module挪到内网
- 然后,可能就会有一个疑问,有些包,比如
github.com/owulveryck/onnx-go
,会有一大堆的依赖包,怎样才能将其挪到内网呢? - 尝试了文档中说的pacmod,这种方式只能打包单个module,并不能一起打包依赖,不太行。
(这里也了解了一些关于module在proxy存储格式的知识,可以看看文档) - 然后想到,既然外网都已经编译好了Athens,那为啥不直接使用呢?
- 外网配置
DownloadMode = "async_redirect" GoBinaryEnvVars = ["GOPROXY=https://goproxy.cn", "GOSUMDB=off"] StorageType = "disk" DownloadURL = "https://mirrors.tencent.com/go/" [Storage] [Storage.Disk] RootPath = "/path/on/filesystem"
DownloadMode
选择async_redirect
,这样就可以在外网使用go get的时候,将module存储下来。
StorageType
还是选择disk
,便于迁移
DownloadURL
可以选择个国内的proxy - 运行Athens后,将外网的go proxy指向Athens
go env -w GOPROXY="http://127.0.0.1:3000" # 这里我的go和athens在同一台机器上,直接用的127.0.0.1 # 同时可以将go sumdb关闭 go env -w GOSUMDB=off
- 然后使用go get
go get github.com/owulveryck/onnx-go
- 如果安装成功,那么你会发现
/path/on/filesystem
中存在了一堆文件,这个时候,你可能已经知道怎么做了。(直接将整个文件夹打包挪到内网解压到内网的/path/on/filesystem
)
总结
需要把DownloadURL
感觉不是很好用GoBinaryEnvVars
配置一下- 搭个proxy还搭了这么久,不太行(主要前面外网起athens的时候用的
nohup ./athens config_file=./config.dev.toml &
)