本期将提供quartz集群能力

  • 集群案例分析: 
    上一期的邮件发送功能,若在服务需要部署多节点,但定时任务不支持集群,因此,多节点定时任务势必会同时运行, 
    若向用户发送邮件通知,这种情况下会向用户发送两次一模一样的邮件,N个节点会发送N次邮件,严重不符合业务场景, 
    若提供集群能力,则多节点间应分担邮件发送的工作而不是各节点做重复的工作,因此在部署多节点的时候定时任务也需要提供集群能力。
  • 个人见解: 
    1. quartz集群分为水平集群和垂直集群,水平集群即将定时任务节点部署在不同的服务器,水平集群最大的问题就是时钟同步问题, 
      quartz集群强烈要求时钟同步,若时钟不能同步,则会导致集群中各个节点状态紊乱,造成不可预知的后果,请自行搜索服务器时钟同步
      若能保证时钟同步,水平集群能保证服务的可靠性,其中一个节点挂掉或其中一个服务器宕机,其他节点依然正常服务;垂直集群则是集群各节点部署在同一台服务器, 
      时钟同步自然不是问题,但存在单点故障问题,服务器宕机会严重影响服务的可用性。因此,要结合实际情况来考虑集群方案
    2. 由于集群中强烈要求时钟同步,因此不管是垂直集群还是水平集群,本地开发决不能连接线上环境(本地也是集群模式),这样的话势必会破坏集群,但本地若是非集群模式, 
      则可以依情况来连接线上环境。
    3. quartz集群和redis这样的集群实现方式不一样,redis集群需要节点之间通信,各节点需要知道其他节点的状况,而quartz集群的实现 
      方式在于11张表,集群节点相互之间不通信,而是通过定时任务持久化加锁的方式来实现集群。
    4. 破坏集群后果一般是死锁或者状态紊乱每个节点都不可用或其中某些节点能用部分或全部的定时任务

1. 创建集群需要的11张表

t_b_qrtz_blob_triggers
t_b_qrtz_calendars
t_b_qrtz_cron_triggers
t_b_qrtz_fired_triggers
t_b_qrtz_job_details
t_b_qrtz_locks
t_b_qrtz_paused_trigger_grps
t_b_qrtz_scheduler_state
t_b_qrtz_simple_triggers
t_b_qrtz_simprop_triggers
t_b_qrtz_triggers
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2. 集群建表sql

drop table if exists t_b_qrtz_fired_triggers;
drop table if exists t_b_qrtz_paused_trigger_grps;
drop table if exists t_b_qrtz_scheduler_state;
drop table if exists t_b_qrtz_locks;
drop table if exists t_b_qrtz_simple_triggers;
drop table if exists t_b_qrtz_simprop_triggers;
drop table if exists t_b_qrtz_cron_triggers;
drop table if exists t_b_qrtz_blob_triggers;
drop table if exists t_b_qrtz_triggers;
drop table if exists t_b_qrtz_job_details;
drop table if exists t_b_qrtz_calendars;

create table t_b_qrtz_job_details(
  sched_name varchar(120) not null,
  job_name varchar(200) not null,
  job_group varchar(200) not null,
  description varchar(250) null,
  job_class_name varchar(250) not null,
  is_durable varchar(1) not null,
  is_nonconcurrent varchar(1) not null,
  is_update_data varchar(1) not null,
  requests_recovery varchar(1) not null,
  job_data blob null,
  primary key (sched_name,job_name,job_group))
  engine=innodb;

create table t_b_qrtz_triggers (
  sched_name varchar(120) not null,
  trigger_name varchar(200) not null,
  trigger_group varchar(200) not null,
  job_name varchar(200) not null,
  job_group varchar(200) not null,
  description varchar(250) null,
  next_fire_time bigint(13) null,
  prev_fire_time bigint(13) null,
  priority integer null,
  trigger_state varchar(16) not null,
  trigger_type varchar(8) not null,
  start_time bigint(13) not null,
  end_time bigint(13) null,
  calendar_name varchar(200) null,
  misfire_instr smallint(2) null,
  job_data blob null,
  primary key (sched_name,trigger_name,trigger_group),
  foreign key (sched_name,job_name,job_group)
  references t_b_qrtz_job_details(sched_name,job_name,job_group))
  engine=innodb;

create table t_b_qrtz_simple_triggers (
  sched_name varchar(120) not null,
  trigger_name varchar(200) not null,
  trigger_group varchar(200) not null,
  repeat_count bigint(7) not null,
  repeat_interval bigint(12) not null,
  times_triggered bigint(10) not null,
  primary key (sched_name,trigger_name,trigger_group),
  foreign key (sched_name,trigger_name,trigger_group)
  references t_b_qrtz_triggers(sched_name,trigger_name,trigger_group))
  engine=innodb;

create table t_b_qrtz_cron_triggers (
  sched_name varchar(120) not null,
  trigger_name varchar(200) not null,
  trigger_group varchar(200) not null,
  cron_expression varchar(120) not null,
  time_zone_id varchar(80),
  primary key (sched_name,trigger_name,trigger_group),
  foreign key (sched_name,trigger_name,trigger_group)
  references t_b_qrtz_triggers(sched_name,trigger_name,trigger_group))
  engine=innodb;

create table t_b_qrtz_simprop_triggers
(
  sched_name varchar(120) not null,
  trigger_name varchar(200) not null,
  trigger_group varchar(200) not null,
  str_prop_1 varchar(512) null,
  str_prop_2 varchar(512) null,
  str_prop_3 varchar(512) null,
  int_prop_1 int null,
  int_prop_2 int null,
  long_prop_1 bigint null,
  long_prop_2 bigint null,
  dec_prop_1 numeric(13,4) null,
  dec_prop_2 

相关文章:

  • 2021-07-01
  • 2021-08-27
  • 2021-11-29
  • 2021-08-04
  • 2021-08-09
猜你喜欢
  • 2021-05-29
  • 2021-04-23
  • 2021-09-01
  • 2021-11-30
  • 2021-05-09
  • 2021-09-24
  • 2021-12-18
相关资源
相似解决方案