使用csync2和lsyncd飞速同步数据Word文件下载.docx
- 文档编号:7820015
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:18
- 大小:300.54KB
使用csync2和lsyncd飞速同步数据Word文件下载.docx
《使用csync2和lsyncd飞速同步数据Word文件下载.docx》由会员分享,可在线阅读,更多相关《使用csync2和lsyncd飞速同步数据Word文件下载.docx(18页珍藏版)》请在冰点文库上搜索。
2)apollo上的lsyncd检测到onCreateinotify事件,触发启动csync2开始同步
3)csync2复制test.gif到所有其他节点上
4)几乎同一时间内
chronos上的lsyncd检测到自己的/var/www/html/images目录中的onCreateinotify事件,触发启动csync2开始同步
hermes上的lsyncd检测到自己的/var/www/html/images目录中的onCreateinotify事件,触发启动csync2开始同步
这时chronos和hermes之间就产生了竞争条件,因为csync2的上传操作几乎瞬间就完成了。
chronos和hermes之间会就"
谁的文件为最新"
产生竞争,经过一系列计算最终才会确定下来。
如果每个文件都经历这样一个过程,那么可以想象,效率是多么的差劲。
而所谓的"
结构如下图所示:
而在这种"
结构中,就解决了上边提到的竞争问题,因为数据只允许单向流动。
3)csync2复制test.gif到chronos上
4)chronos上的lsyncd检测到自己的/var/www/html/images目录中的onCreateinotify事件,触发启动csync2开始同步
5)csync2复制test.gif到hermes上
6)hermes上的lsyncd检测到自己的/var/www/html/images目录中的onCreateinotify事件,触发启动csync2开始同步
7)hermes想要复制test.gif到apollo上,但此时apollo上已经有了test.gif,经过比对以后,这个复制不会继续下去。
大家可以看到,这种"
结构有效的避免了节点间的竞争关系,不过这种结构也有自己的缺点,那就是一旦其中某一台节点死机或csync2服务停止,则整个链条的完整就被打破,各节点间数据就会出现不同步。
在这种情况下,我们只能修复好损坏的节点以后,手工强制同步一次。
因此,是否能接受这样的损失,也是考虑采用这种"
结构的一个关键问题。
另外,单纯采用"
结构只是改善了一点竞争问题,但是效率并没有特别显著的提升。
为什么呢?
这就涉及到了这个方案中的另一重要组成部分--lsyncd。
原文作者与lsyncd的开发者做了大量交流,发现lsyncd采用的是per-file的触发机制。
per-file触发,顾名思义就是每个文件触发,也就是说lsyncd监视某一个目录的文件变化情况,只要发现有一个文件发生了改变,则其立刻会触发csync2进行同步操作,而这个同步操作只是针对发生改变的这个文件,而不是整个目录。
那这样不是很好吗?
如果在变动文件数量很少,且变动不集中的时候,当然是很好的。
但是,当目录中的文件频繁发生改变,且变动时间相对集中的时候,这种机制就不那么让人满意了。
因为每次检测到一个文件变化,就要启动一次csync2,假设1秒钟内有30个文件发生了改变呢?
可以想象性能会差到什么程度。
因此,原文作者用lua语言自己编写了lsyncd的配置文件,接管了eventcall的操作,将per-file触发改成了批量触发。
这个改动有效解决了频繁启动csync2的问题。
至于原文作者写的那个lsyncd的配置文件是如何实现的,有兴趣的朋友可以自己看看。
另外,因为这个改动是如此有效果,因此lsyncd的开发者已经把这个配置文件收入了lsyncd源码的sample-config中了。
下边是具体编译安装的过程。
STEP1.在各节点上分别编译安装csync2
[apollo]#cd/usr/local/src
[apollo]#yuminstallcvs
[apollo]#cvs-d:
pserver:
anonymous@www.sqlite.org:
/sqlitelogin#usepassword"
anonymous"
/sqlitecheckoutsqlite
[apollo]#cdsqlite
[apollo]#cvsupdate-rversion_2
[apollo]#./configure&
&
make
[apollo]#tar-czvfsqlite.tgzsqlite#makeatarballforstaticcompilebycsync2
[apollo]#wget
[apollo]#tar-zxvfcsync2-1.34.tar.gz
[apollo]#yuminstalllibrsynclibrsync-devellibtasn1-devellibtasn1sqlite-develsqlitebisonbison-develbison-runtimeflex
[apollo]#./configure--with-libsqlite-source=../sqlite.tgz
[apollo]#make&
makeinstall&
makecert
然后修改/etc/services加入如下一行:
csync230865/tcp#Csync2
接下来修改创建/etc/xinetd.d/csync2
#cd/etc/xinetd.d
#vicsync2
加入内容如下:
servicecsync2
{
disable=no
port=30865
socket_type=stream
protocol=tcp
wait=no
user=root
passenv=PATH
server=/usr/local/sbin/csync2
server_args=csync2-i
}
以上步骤在chronos和hermes上也都要同样执行。
STEP2.在第一个节点上创建共享秘钥和配置文件
创建csync2节点间的共享秘钥
[apollo]#csync2-k/etc/csync2/csync2.key
创建csync2默认的配置文件/etc/csync2/csync2.cfg,这个配置文件的作用是用来完成各节点间的初始化同步。
nossl**;
groupweb
hostapollo;
hostchronos;
hosthermes;
key/etc/csync2/csync2.key;
include/var/www/html;
exclude*~.*;
autonone;
创建apollo节点的独立配置文件/etc/csync2/csync2_apollo.cfg,这个配置文件是以后正式用到的配置文件。
注意host(chronos);
这一行,就是这样一行定义了"
的同步结构。
groupapollo
host(chronos);
STEP3.复制配置文件和密钥到其他节点,并相应修改。
将第一个节点中生成的配置文件复制到剩余的其他节点中,并根据主机名做相应的修改。
例如在chronos中
[chronos]#rm-f/etc/csync2/*
[chronos]#scproot@apollo:
/etc/csync2/*/etc/csync2/
[chronos]#cd/etc/csync2
[chronos]#mvsync2_apollo.cfgcsync2_chronos.cfg
然后修改/etc/csync2/csync2_chronos.cfg,内容如下:
groupchronos
host(hermes);
同样的,在hermes上
[hermes]#rm-f/etc/csync2/*
[hermes]#scproot@apollo:
[hermes]#cd/etc/csync2
[hermes]#mvsync2_apollo.cfgcsync2_hermes.cfg
修改/etc/csync2/csync2_hermes.cfg,内容如下:
grouphermes
host(apollo);
STEP4.在各节点上分别安装lsyncd
从源码安装lsyncd
[apollo]#tarzxvflsyncd-2.0.4.tar.gz
[apollo]#cdlsyncd-2.0.4
[apollo]#./configure
[apollo]#make
[apollo]#makeinstall
或者采用rpm安装
[apollo]#wgethttp:
//apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/lsyncd-2.0.4-1.el5.rf.x86_64.rpm
[apollo]#rpm-ivhlsyncd-2.0.4-1.el5.rf.x86_64.rpm
STEP5.在各节点上分别配置lsyncd
在各节点上编辑/etc/lsyncd/lsyncd.conf配置文件,内容如下:
-----
--Userconfigurationfileforlsyncd.
--
--Thisexamplesynchronizesonespecificdirectorythroughmultiplenodes,
--bycombiningcsync2andlsyncdasmonitoringtools.
--Itavoidsanyraceconditionsgeneratedbylsyncd,whiledetectingand
--processingmultipleinotifyeventsinbatch,oneachnodemonitoredby
--csync2daemon.
--@author
FlorenMunteanu
--@link
settings={
logident="
lsyncd"
logfacility="
user"
logfile="
/var/log/lsyncd/lsyncd.log"
statusFile="
/var/log/lsyncd/status.log"
statusInterval=1
initSync={
delay=1,
maxProcesses=1,
action=function(inlet)
localconfig=inlet.getConfig()
localelist=inlet.getEvents(function(event)
returnevent.etype~="
Blanket"
end)
localdirectory=string.sub(config.source,1,-2)
localpaths=elist.getPaths(function(etype,path)
return"
\t"
..config.syncid.."
:
"
..directory..path
log("
Normal"
"
Processingsyncinglist:
\n"
table.concat(paths,"
))
spawn(elist,"
/usr/sbin/csync2"
-C"
config.syncid,"
-x"
)
end,
collect=function(agent,exitcode)
localconfig=agent.config
ifnotagent.isListandagent.etype=="
then
ifexitcode==0then
Startupof'
'
instancefinished."
elseifconfig.exitcodesandconfig.exitcodes[exitcode]=="
again"
Retryingstartupof'
instance."
else
Error"
Failureonstartupof'
terminate(-1)
end
return
localrc=config.exitcodesandconfig.exitcodes[exitcode]
ifrc=="
die"
returnrc
ifagent.isListthen
Retryingeventslistonexitcode="
exitcode)
Finishedeventslist="
Retrying"
agent.etype,"
on"
agent.sourcePath,"
="
Finished"
init=function(inlet)
localevent=inlet.createBlanketEvent()
Recursivestartupsync:
"
config.source)
spawn(event,"
prepare=function(config)
ifnotconfig.syncidthen
error("
Missing'
syncid'
parameter."
4)
localc="
csync2_"
.cfg"
localf,err=io.open("
/etc/csync2/"
..c,"
r"
ifnotfthen
Invalid'
parameter:
..err,4)
f:
close()
localsources={
["
/var/www/html"
]="
apollo"
forkey,valueinpairs(sources)do
sync{initSync,source=key,syncid=value}
根据自己的需要,调整souces数组的值,可以使用如下格式:
/var/www/html/images"
/var/www/html/customavatars"
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 使用 csync2 lsyncd 飞速 同步 数据