首页 技术 正文
技术 2022年11月21日
0 收藏 663 点赞 3,790 浏览 2133 个字

一、背景

crontab 是 Linux 系统里面最简单易用的定时任务管理工具,相信绝大多数开发和运维都用到过,很多业务系统的定时任务都是通过 crontab 来定义的,时间长了后会发现存在很多问题:

  • 大量的 crontab 任务散布在各台服务器,带来了很高的维护成本
  • 任务没有按时执行,甚至失败了很久才发现,需要重试或排查
  • crontab 分散在很多集群上,需要一台一台去看日志分析,头都大了
  • crontab 存在单点问题,对于不能重复执行的定时任务很伤脑筋
  • 我 X,crontab 被误删了,没备份?
  • 我 Q,服务器要迁移,crontab 上的历史任务都是什么鬼?问了一圈居然都不知道

因此,我们非常需要一个集中管理定时任务系统,相信这也是的饱受 crontab 煎熬的运维或开发的心声。

二、安装

安装包:

cronsun

wget https://github.com/shunfei/cronsun/releases/download/v0.3.5/cronsun-v0.3.5-linux-amd64.zip (unzip解压)

mongodb

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.2.tgz (tar -xzvf 解压)

etcd

wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz (tar -xzvf 解压)

三、启动

1.mongodb

 1 cd /opt/mongodb-v4.0.2
2
3 mkdir -p data/db
4
5 mkdir logs
6
7 touch mongodb.conf
8
9 vim mongodb.com
10
11 dbpath=/opt/mongodb-v4.0.2/data/db
12
13 logpath=/opt/mongodb-v4.0.2/logs/mongo.log
14
15 bind_ip=0.0.0.0 #默认是127.0.0.1, 多节点需要允许其他的ip连接, 所有配置为0.0.0.0,允许其他ip连接
16
17 logappend=true
18
19 fork=true
20
21 port=27017
22
23 nohup bin/mongod -f mongodb.conf > /dev/null 2>&1 &

2.etcd

$nohup ./etcd –advertise-client-urls=http://0.0.0.0:2379 –listen-client-urls=http://0.0.0.0:2379 –data-dir=/opt/etcd-v3.4.13/data   >/dev/null 2>&1 &

注意:默认监听地址是127.0.0.1,允许远程连接需要改为0.0.0.0

3.cronsun

1 cd /opt/ cronsun-v0.3.5
2
3 nohup ./cronweb -conf conf/base.json > /dev/null 2>&1 &
4
5 nohup ./cronnode -conf conf/base.json > /dev/null 2>&1 &

四、添加节点

 1 scp -r root@192.168.0.119:/opt/cronsun-v0.3.5 /opt
2
3 vim /opt/cronsun-v0.3.5/conf/db.json
4 "Hosts": [
5 "192.168.0.119:27017" # 这里改为mongodb监听的ip和端口
6 ],
7
8 vim /opt//cronsun-v0.3.5/conf/etcd.json
9 "Endpoints":[
10 "http://192.168.0.119:2379" # 这里改为etcd监听的ip和端口
11 ],
12
13 nohup ./cronnode -conf conf/base.json > /dev/null 2>&1 &

五、测试

打开 http://ip:7079 刷新页面 , 会发现增加节点了

测试两台机子都会执行任务

六:任务监控

发现配置好mail.json

 1 {
2 "Enable": true,
3 "To": ["***@qq.com"],
4 "HttpAPI": "http://xxx.xxx.xxx.xx:xxxx/cronsun(这行改成自己写的接口,失败会自动调用,数据格式是raw的json格式)",
5 "#Keepalive": "如果此时间段内没有邮件发送,则关闭 SMTP 连接,单位/秒",
6 "Keepalive": 60,
7 "#doc": "https://godoc.org/github.com/go-gomail/gomail#Dialer",
8 "Host": "smtp.163.com",
9 "Port": 25,
10 "Username": "***@163.com",
11 "Password": "***",
12 "SSL": false,
13 "#LocalName": "LocalName is the hostname sent to the SMTP server with the HELO command. By default, 'localhost' is sent.",
14 "LocalName": ""
15 }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905