Uncode-Schedule分布式任務(wù)調(diào)度組件
基于zookeeper+spring task/quartz的分布式任務(wù)調(diào)度組件,確保所有任務(wù)在集群中不重復(fù),不遺漏的執(zhí)行。支持動態(tài)添加和刪除任務(wù)。
功能概述
- 基于zookeeper+spring task/quartz的分布任務(wù)調(diào)度系統(tǒng)。
- 確保每個任務(wù)在集群中不同節(jié)點上不重復(fù)的執(zhí)行。
- 單個任務(wù)節(jié)點故障時自動轉(zhuǎn)移到其他任務(wù)節(jié)點繼續(xù)執(zhí)行。
- 任務(wù)節(jié)點啟動時必須保證zookeeper可用,任務(wù)節(jié)點運(yùn)行期zookeeper集群不可用時任務(wù)節(jié)點保持可用前狀態(tài)運(yùn)行,zookeeper集群恢復(fù)正常運(yùn)期。
- 支持動態(tài)添加和刪除任務(wù)。
- 添加ip黑名單,過濾不需要執(zhí)行任務(wù)的節(jié)點。
- 簡單管理后臺
說明:
- 單節(jié)點故障時需要業(yè)務(wù)保障數(shù)據(jù)完整性或冪等性
- 具體使用方式和spring task相同k
模塊架構(gòu)
Uncode-Schedule
Spring bean
public class SimpleTask {
private static int i = 0;
public void print() {
System.out.println("===========start!=========");
System.out.println("I:"+i);i++;
System.out.println("=========== end !=========");
}
}
xml配置
<!-- 分布式任務(wù)管理器 -->
<bean id="zkScheduleManager" class="cn.uncode.schedule.ZKScheduleManager"
init-method="init">
<property name="zkConfig">
<map>
<entry key="zkConnectString" value="127.0.0.1:2181" />
<entry key="rootPath" value="/uncode/schedule" />
<entry key="zkSessionTimeout" value="60000" />
<entry key="userName" value="ScheduleAdmin" />
<entry key="password" value="password" />
<entry key="isCheckParentPath" value="true" />
<entry key="ipBlacklist" value="127.0.0.2,127.0.0.3" />
</map>
</property>
</bean>
API
1 動態(tài)添加任務(wù)
ConsoleManager.addScheduleTask(TaskDefine taskDefine);
2 動態(tài)刪除任務(wù)
ConsoleManager.delScheduleTask(String targetBean, String targetMethod);
3 查詢?nèi)蝿?wù)列表
ConsoleManager.queryScheduleTask();
基于Spring Task的XML配置
XML方式
1 Spring bean
public class SimpleTask {
private static int i = 0;
public void print() {
System.out.println("===========start!=========");
System.out.println("I:"+i);i++;
System.out.println("=========== end !=========");
}
}
2 xml配置
<!-- 分布式任務(wù)管理器 -->
<bean id="zkScheduleManager" class="cn.uncode.schedule.ZKScheduleManager"
init-method="init">
<property name="zkConfig">
<map>
<entry key="zkConnectString" value="127.0.0.1:2181" />
<entry key="rootPath" value="/uncode/schedule" />
<entry key="zkSessionTimeout" value="60000" />
<entry key="userName" value="ScheduleAdmin" />
<entry key="password" value="password" />
<entry key="isCheckParentPath" value="true" />
<entry key="ipBlacklist" value="127.0.0.2,127.0.0.3" />
</map>
</property>
</bean>
<!-- Spring bean配置 -->
<bean id="taskObj" class="cn.uncode.schedule.SimpleTask"/>
<!-- Spring task配置 -->
<task:scheduled-tasks scheduler="zkScheduleManager">
<task:scheduled ref="taskObj" method="print" fixed-rate="5000"/>
</task:scheduled-tasks>
Annotation方式
1 Spring bean
@Component
public class SimpleTask {
private static int i = 0;
@Scheduled(fixedDelay = 1000)
public void print() {
System.out.println("===========start!=========");
System.out.println("I:"+i);i++;
System.out.println("=========== end !=========");
}
}
2 xml配置
<!-- 配置注解掃描 -->
<context:annotation-config />
<!-- 自動掃描的包名 -->
<context:component-scan base-package="cn.uncode.schedule" />
<!-- 分布式任務(wù)管理器 -->
<bean id="zkScheduleManager" class="cn.uncode.schedule.ZKScheduleManager"
init-method="init">
<property name="zkConfig">
<map>
<entry key="zkConnectString" value="127.0.0.1:2181" />
<entry key="rootPath" value="/uncode/schedule" />
<entry key="zkSessionTimeout" value="60000" />
<entry key="userName" value="ScheduleAdmin" />
<entry key="password" value="password" />
<entry key="isCheckParentPath" value="true" />
<entry key="ipBlacklist" value="127.0.0.2,127.0.0.3" />
</map>
</property>
</bean>
<!-- Spring定時器注解開關(guān)-->
<task:annotation-driven scheduler="zkScheduleManager" />
基于Quartz的XML配置
注意:spring的MethodInvokingJobDetailFactoryBean改成cn.uncode.schedule.quartz.MethodInvokingJobDetailFactoryBean
<bean id="zkScheduleManager" class="cn.uncode.schedule.ZKScheduleManager"
init-method="init">
<property name="zkConfig">
<map>
<entry key="zkConnectString" value="183.131.76.147:2181" />
<entry key="rootPath" value="/uncode/schedule" />
<entry key="zkSessionTimeout" value="60000" />
<entry key="userName" value="ScheduleAdmin" />
<entry key="password" value="password" />
<entry key="autoRegisterTask" value="true" />
<entry key="ipBlacklist" value="127.0.0.2,127.0.0.3" />
</map>
</property>
</bean>
<bean id="taskObj" class="cn.uncode.schedule.SimpleTask"/>
<!-- 定義調(diào)用對象和調(diào)用對象的方法 -->
<bean id="jobtask" class="cn.uncode.schedule.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 調(diào)用的類 -->
<property name="targetObject" ref="taskObj" />
<!-- 調(diào)用類中的方法 -->
<property name="targetMethod" value="print" />
</bean>
<!-- 定義觸發(fā)時間 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表達(dá)式 -->
<property name="cronExpression">
<value>0/3 * * * * ?</value>
</property>
</bean>
<!-- 總管理類 如果將lazy-init='false'那么容器啟動就會執(zhí)行調(diào)度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
uncode-schedule管理后臺
訪問URL:項目名稱/uncode/schedule
大家都在使用uncode-schedule
關(guān)于
作者:冶衛(wèi)軍
評論
圖片
表情
