当前位置:网站首页>Quartz分布式实现

Quartz分布式实现

2022-08-09 10:45:00 猿小飞

1.项目背景

以前项目都是单体项目,在线上环境发布的时候,都是单机版。后来使用的用户越来越多了。就需要做负载了。因为项目在实际使用的过程中,使用到了定时任务。所以就引入了Quartz框架。

2.出现的问题

2.1.时间不同步的问题

[2021-12-14 11:24:45,956 WARN ] [QuartzScheduler_devScheduler_1-localhost.localdomain1639452248424_ClusterManager] (org.springframework.scheduling.quartz.LocalDataSourceJobStore:?) - This scheduler instance (localhost.localdomain1639452248424) is still active but was recovered by another instance in the cluster.  This may cause inconsistent behavior.

 当我们应用服务器,出现时间不步的时候,另外一台服务器就会报上面这个错误。

注意了,这个错误出现的频率和你定时事务触发的时间有关,比如你的定时任务是1分钟触发一次,这个错误就会一分钟出现一次。

2.1.1.如何解决这个问题--ntp服务

1.安装 ntp服务进行多台服务器时间同步

rpm -qa|grep ntp

 使用该命令,来查询是否已经安装了ntp服务。

ntp基础概念:ntp的基础知识 - 90swj - 博客园

2.如果没有安装,则可以通过yun命令进行安装

yum -y install ntp

 安装成功后,会在/etc目录下,会有ntp.conf配置文件。

3.修改配置信息,新增这样的信息:

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

 代表允许192.168网段的服务器与此服务器进行时间同步。

 参数说明:

用restrict控管权限
nomodify – 用户端不能更改ntp服务器的时间参数
noquery – 用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap – 不提供trap远端登陆
restrict 192.168.1.129 nomodify notrap nopeer noquery          //当前节点IP地址

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap  //集群所在网段的网关(Gateway),子网掩码

修改前:

 主节点:

fudge 127.127.1.0 stratum 10 的含义? 

fudge 127.127.1.0 stratum 10通常上面还有一行不能少server 127.127.1.0 意思
是当没有时间同步来源的时候以自身的硬件时钟为准,这里的stratum是代表层级,默认是10。

特别注意:这里如果把server都屏蔽掉,运行一段时间后,他不和外部的网络时间同步,也会出现自己跑快的现象。所以解决方法是,我们可以连一下 阿里云的授时网站:

# 国家授时中心
210.72.145.44
# 阿里云
ntp.aliyun.com

s1a.time.edu.cn 北京邮电大学
s1b.time.edu.cn 清华大学
s1c.time.edu.cn 北京大学
s1d.time.edu.cn 东南大学
s1e.time.edu.cn 清华大学
s2a.time.edu.cn 清华大学
s2b.time.edu.cn 清华大学
s2c.time.edu.cn 北京邮电大学
s2d.time.edu.cn 西南地区网络中心
s2e.time.edu.cn 西北地区网络中心
s2f.time.edu.cn 东北地区网络中心
s2g.time.edu.cn 华东南地区网络中心
s2h.time.edu.cn 四川大学网络管理中心
s2j.time.edu.cn 大连理工大学网络中心
s2k.time.edu.cn CERNET桂林主节点
s2m.time.edu.cn 北京大学
ntp.sjtu.edu.cn 202.120.2.101 上海交通大学

实际最后我修改的配置文件为:

 4.启动服务

service ntpd start

4.1.开机自启动

chkconfig ntpd on

5.查看ntp服务器有无和上层ntp连通 

ntpstat

6.客户端服务器也安装ntp服务,同时修改配置文件

7.客户端也需要开机自启动

chkconfig ntpd on

微信扫一扫:关注我个人订阅号“猿小飞”,更多精彩文章在这里及时发布: 

原网站

版权声明
本文为[猿小飞]所创,转载请带上原文链接,感谢
https://yangmy.blog.csdn.net/article/details/121923182