有時,我們經常會在資料庫服務器上做一些定時備份的作業,最常用的方法就是寫個sh腳本,然后配置/etc/crontab定時策略即可,但它有缺點,我總結如下:
- 腳本基本相同,代碼需要在每個資料服務器上部署
- 由于代碼是分布的,并且是個代碼,所以管理不清晰,不直觀
- 控制不統一,你需要在每個服務器的/etc/crontab里配置策略
xxl-job
主要解決任務調度的問題,并且它是分布式的,可以有多個執行器,多個執行器可以進行集群策略的定時,包括輪訓,隨即,hash,LRU,LFU等等,下面使用xxl-job來實作這個定時備份的功能,
先準備腳本檔案
檔案可以直接寫在java代碼時,通過代碼去構建檔案,就不需要在每個服務器上添加了,需要為檔案添加“執行”的權限,
- backup.sh
db_user="root"
db_passwd="123456"
db_name="test"
name="$(date +"%Y%m%d%H%M%S")"
/usr/local/mysql/bin/mysqldump -u$db_user -p$db_passwd $db_name >>/home/backup/$name.sql
添加xxl-job的執行器的handler
@XxlJob(value="https://www.cnblogs.com/lori/p/doingSh",init = "init", destroy = "destroy")
public void doSh() throws Exception {
String command = "/root/backup.sh";
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
組態檔中指定xxl-job-admin的地址
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://192.168.3.181:8080/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=default_token
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=8898
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
添加執行器

添加任務

你的執行器開啟之后,它與會xxl-job-admin進行長連接,他們之間進行TCP的通訊,
作者:倉儲大叔,張占嶺,
榮譽:微軟MVP
QQ:853066980
支付寶掃一掃,為大叔打賞!

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/544055.html
標籤:架構設計
上一篇:稀土智能工廠調研紀實
