本期將提供quartz集羣能力
- 集羣案例分析:
上一期的郵件發送功能,若在服務須要部署多節點,但定時任務不支持集羣,所以,多節點定時任務勢必會同時運行,
若向用戶發送郵件通知,這種狀況下會向用戶發送兩次如出一轍的郵件,N個節點會發送N次郵件,嚴重不符合業務場景,
若提供集羣能力,則多節點間應分擔郵件發送的工做而不是各節點作重複的工做,所以在部署多節點的時候定時任務也須要提供集羣能力。
- 我的看法:
- quartz集羣分爲水平集羣和垂直集羣,水平集羣即將定時任務節點部署在不一樣的服務器,水平集羣最大的問題就是時鐘同步問題,
quartz集羣強烈要求時鐘同步,若時鐘不能同步,則會致使集羣中各個節點狀態紊亂,形成不可預知的後果,請自行搜索服務器時鐘同步
,
若能保證時鐘同步,水平集羣能保證服務的可靠性,其中一個節點掛掉或其中一個服務器宕機,其餘節點依然正常服務;垂直集羣則是集羣各節點部署在同一臺服務器,
時鐘同步天然不是問題,但存在單點故障問題,服務器宕機會嚴重影響服務的可用性。所以,要結合實際狀況來考慮集羣方案
- 因爲集羣中強烈要求時鐘同步,所以無論是垂直集羣仍是水平集羣,本地開發決不能鏈接線上環境(本地也是集羣模式),這樣的話勢必會破壞集羣,但本地如果非集羣模式,
則能夠依狀況來鏈接線上環境。
- quartz集羣和redis這樣的集羣實現方式不同,redis集羣須要節點之間通訊,各節點須要知道其餘節點的情況,而quartz集羣的實現
方式在於11張表,集羣節點相互之間不通訊,而是經過定時任務持久化加鎖的方式來實現集羣。
- 破壞集羣后果通常是死鎖或者狀態紊亂每一個節點都不可用或其中某些節點能用部分或所有的定時任務
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
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 numeric(13,4) null, bool_prop_1 varchar(