replication是一个Gerrit插件,可以从从服务器拉取代码,分担Gerrit主代码服务器的压力。配置一个或多个Gerrit从服务器,使用replication插件即时同步主服务器代码,然后普通用户从从服务器上面拉取代码,push代码的目标仍然是主服务器,解决主服务器性能问题,大概流程如下图:
ssh配置
配置主服务器ssh,在根目录下面的.ssh目录:
配置config文件,填写从服务器地址信息
Host 192.168.1.1
User gerrit
Hostname 192.168.1.1
IdentityFile ~/.ssh/id_rsa
PreferredAuthentications publickey
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
编辑authorized_keys文件,加入从服务器的id_rsa.pub内容
配置从服务器ssh,在根目录下面的.ssh目录:
编辑authorized_keys文件,加入主服务器的id_rsa.pub内容
在主服务器上面配置Gerrit replication,位于Gerrit安装目录的etc/replication.config:
配置meta/external-ids信息需要配置All-Projects权限,添加Global Capabilities->Access Database后才有权限fetch refs/meta/external-ids分支,查看账号配置信息
[remote "从服务器名称"]
//url为从服务器地址,可以填写多个
url = gerrit@192.168.1.1:/home/gerrit/gerritCD/git/${name}.git
projects = android* //匹配Android开头的所有仓库,可以填写多个,如果不填写仓库则会全部同步
push = +refs/*:refs/* //同步所有仓库,包括All-projects和All-Users仓库,对于完全备份的服务器是必选项
push = +refs/heads/*:refs/heads/* //同步所有的分支
push = +refs/tags/*:refs/tags/*
push = +refs/changes/*:refs/changes/* //同步提交信息
//同步配置信息仓库
push = +refs/meta/external-ids:refs/meta/external-ids
push = +refs/starred-changes/*:refs/starred-changes/*
push = +refs/sequences/accounts:refs/sequences/accounts
push = +refs/groups/*:refs/groups/*
push = +refs/meta/group-names:refs/meta/group-names
push = +refs/sequences/groups:refs/sequences/groups
push = +refs/meta/config:refs/meta/config
mirror = true
replicatePermissions=true
threads = 16 //同时同步线程数
replicationDelay = 5 //延迟时间,单位是秒
adminUrl = gerrit@192.168.1.1:/home/gerrit/gerritCD/git/${name}.git
重新加载replication插件
ssh -p 29418 user@192.168.1.0 gerrit plugin reload replication
首次同步需要ssh授权,因此需要先在服务器上手动触发一次:
ssh -p 29418 user@192.168.1.0 replication start project
在弹出的授权框中输入yes,然后Gerrit在后台就可以自动同步。
replication常用命令
#查看帮助信息
ssh -p 29418 user@192.168.1.0 replication start --help
#同步所有仓库
ssh -p 29418 user@192.168.1.0 replication start --all
#重新加载插件
ssh -p 29418 user@192.168.1.0 gerrit plugin reload replication
#同步单个仓库
ssh -p 29418 user@192.168.1.0 replication start project
同步仓库:
sudo java -jar /home/gerrit/bin/gerrit.war reindex --index projects -d /home/gerrit/gerritCD/
同步changes:
sudo java -jar /home/gerrit/bin/gerrit.war reindex --index changes -d /home/gerrit/gerritCD/
同步groups:
sudo java -jar /home/gerrit/bin/gerrit.war reindex --index groups -d /home/gerrit/gerritCD/
同步accounts:
sudo java -jar /home/gerrit/bin/gerrit.war reindex --index accounts -d /home/gerrit/gerritCD/
配置gitconfig
编辑根目录下面的gitconfig文件:
主服务器地址:192.168.1.0
从服务器地址:192.168.1.1
[user]
email = *.com
name = user
[color]
ui = auto
#url中为实际使用的地址,insteadOf为被替换的地址
[url "ssh://192.168.1.1:29418"]
insteadOf = ssh://192.168.1.0:29418
#url中为实际使用的地址,pushInsteadOf为被替换的地址
[url "ssh://192.168.1.0:29418"]
pushInsteadOf = ssh://192.168.1.1:29418
配置gitconfig以后,从主服务器拉取代码,实际使用的服务器是从服务器192.168.1.1;向服务器提交代码,只能提交到主服务器,从服务器对用户不可见。
官方文档
About
This plugin can automatically push any changes Gerrit Code Review makes to its managed Git repositories to another system. Usually this would be configured to provide mirroring of changes, for warm-standby backups, or a load-balanced public mirror farm.
The replication runs on a short delay. This gives the server a small time window to batch updates going to the same project, such as when a user uploads multiple changes at once.
Typically replication should be done over SSH, with a passwordless public/private key pair. On a trusted network it is also possible to use replication over the insecure (but much faster due to no authentication overhead or encryption) git:// protocol, by enabling the receive-pack
service on the receiving system, but this configuration is not recommended. It is also possible to specify a local path as replication target. This makes e.g. sense if a network share is mounted to which the repositories should be replicated.
In multi-primary scenario, any replication work which is already in-flight or completed by the other nodes is not performed to avoid extra work. This is because, the storage for replication events is shared between multiple primaries.(The storage location is specified in the config using: replication.eventsDirectory
).
Replication of account data (NoteDb)
To replicate the account data in NoteDb the following branches from the All-Users
repository must be replicated:
refs/users/*
(user branches)refs/meta/external-ids
(external IDs)refs/starred-changes/*
(star labels, not needed for Gerrit slaves)refs/sequences/accounts
(account sequence numbers, not needed for Gerrit slaves)
Enabling Replication
If replicating over SSH, ensure the host key of the remote system(s) is already in the Gerrit user's ~/.ssh/known_h