被 Wall 逼疯的开发者们

5,512 阅读4分钟

前言

开发配置环境或更新时,经常被网络搞得精疲力竭,403 Forbidden443 TimeoutSSL_ERROR_SYSCALL等各种错误,不经历如小媳妇盼夫归般的等待进度条######## 9.1%慢慢爬行,clone至 99% 时突然error的绝望心情,是不会理解这种心情的 😤 。

究其原因,是国外网络被屏蔽、速度慢,掉线等各种因素造成的;想学习时,可能花了一天配置环境,环境配置好早已没了心情。幸运的是国内高校和大公司都提供了镜像源,速度和稳定性都大幅度提升,国内直连,比付费 VPN 速度更快,稳定性更高。

镜像源可简单理解为“复制的下载源”,例如下载一部电影,原始链接https://movie.***.com是国外服务器的下载链接,国内直接下载可能速度只有 20KB;这时如果国内有人花费一整天把电影下载后,把这部电影放到国内服务器上,并向外开放下载地址https://movie.***.cn,这时国内有人想下载这部电影,使用后面分享的这个链接,下载速度可能就达到 2MB 了;这里的比喻可能不太恰当,仅用于理解镜像源。

本文提供 brew、pypi、docker,github 等部分解决方案,详细说明可查看镜像源的帮助文档。

镜像源

国内镜像源做的比较好的是中科大和清华,其他大公司也提供了镜像源,例如阿里、腾讯,网易等,一般情况下中科大和清华镜像源完全能满足开发需求,以中科大镜像源举例。

中科大镜像源地址 https://mirrors.ustc.edu.cn/,这是自动解析线路,还可以根据自己的网络选择如下速度快的线路:

如何设置镜像源,可查看帮助文档https://mirrors.ustc.edu.cn/help/

其他镜像源

brew

替换 brew 源

# 替换 brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替换 homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 替换 Homebrew Cask
cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# 对于 bash 用户,替换 Homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

# 对于 zsh 用户,替换 Homebrew-bottles
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

还原 brew 为官方地址

Homebrew-bottles还原,打开.zshrc.bash_profile文件,删除上面添加的HOMEBREW_BOTTLE_DOMAIN行即可。

# 重置 brew.git 为官方地址
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git

# 重置 homebrew-core.git 为官方地址
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git

# 重置 homebrew-cask 为官方地址
cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
git remote set-url origin https://github.com/Homebrew/homebrew-cask

pypi(pip)

pip 是 Python 包管理工具,pip 命令默认使用的是国外的 pypi 镜像(pypi.python.org),速度慢易掉线,切换为国内镜像源。

方法 1:

# 临时使用
pip install -i https://mirrors.ustc.edu.cn/pypi/web/simple package
# 设为默认,升级 pip 到最新的版本 (>=10.0.0) 后进行配置
pip install -i https://mirrors.ustc.edu.cn/pypi/web/simple pip -U
pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple

方法 2:

当方法 1 不起作用时,可尝试此方法,在当前用户根目录下~/.pip/pip.conf,写入如下配置,没有文件夹和文件创建即可。

[[tool.poetry.source]]
name = "tsinghua"
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true

Docker

安装完 Docker CE 之后,替换掉官方的 Docker Registry 服务,可加快拉取 Docker 镜像的速度,不同系统的更换方法不一致,参考镜像源帮助文档即可。

以 macOS Catalina 的 Docker 为例,在 Preferences -> Docker Engine 中添加以下配置,重启 Docker:

"registry-mirrors" : [
   "http://docker.mirrors.ustc.edu.cn",
   "http://hub-mirror.c.163.com",
   "https://mirror.ccs.tencentyun.com",
   "http://registry.docker-cn.com"
 ],
 "insecure-registries" : [
   "registry.docker-cn.com",
   "docker.mirrors.ustc.edu.cn"
 ],

CocoaPods

CocoaPods 的 trunk 仓库大部分时候尚可,当速度慢或者失败时,可尝试如下配置。

对于旧版的 CocoaPods 可以使用如下方法使用 tuna 的镜像:

pod repo remove master
pod repo add master https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
pod repo update

新版的 CocoaPods 不允许用pod repo add直接添加master库了,但是依然可以:

cd ~/.cocoapods/repos 
pod repo remove master
git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master

最后进入自己的工程,在自己工程的podFile第一行加上:

source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'

github

github 域名污染

部分网络可能由于域名污染,github 出现访问失败、图片加载不出来的情况,这时候可以参考修改 hosts 文件,添加 IP与域名映射,通过直接访问 IP 地址的方式来访问,结合switchhosts,可以实现自动修改更新 hosts 文件。

github 的 IP 自动指向可参考GitHub520,此方法有效但作用有限,github 经常访问失败的同学可尝试一下。

github 下载速度慢

git clone较大项目时,速度慢、总是失败的情况,可使用 VPN 加速,效果明显,此方法只适合有 VPN 或其他工具的用户。

例如 VPN 的 socks5 端口是 49526,参考下方socks5协议配置:

# socks5协议,49526 端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy socks5://127.0.0.1:49526
git config --global https.https://github.com.proxy socks5://127.0.0.1:49526

# http协议,49527 端口修改成自己的本地代理端口127.0.0.1:49527
git config --global http.proxy http://127.0.0.1:49527
git config --global https.proxy https://127.0.0.1:49527

# http协议,49527 端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy https://127.0.0.1:49527
git config --global https.https://github.com.proxy https://127.0.0.1:49527

# reset 代理设置
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy