【golang/实践】局域网环境搭建go proxy(使用Athens)

说在前面

  • 外网环境:WSL 1.2.5.0
  • 内网环境:CentOS Linux release 7.3
  • 代理工具:Athens(version 0.2.0)
  • 外网go版本:go1.20.2 linux/amd64

关于Athens

  • 官网
  • 为什么使用这个?
    相比于go proxy,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"
    
  • DownloadModenone,这样在找不到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 &
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值