爲什麼使用quartz分佈式集羣
像之前的quartz和spring scheduler如果我們部署在單臺機器上,雖然表面上我們解決了多臺機器重複執行的問題,但是卻有隱含的風險。單臺機器如果宕機,那麼定時任務功能將會導致停止,所以要部署到多臺,保證系統的高可用性。並且隨着我們要執行的定時任務越來越多,那麼單臺的壓力會越大。
如果我們部署到多臺機器上,那麼隨之而來的問題就是重複記錄如何避免。那麼就需要我們去了解分佈式鎖的實現方式。而quartz的集羣就採用了mysql的行級鎖互斥來防止重複記錄的。
環境搭建
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- MySQL相關包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- 數據庫連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.20</version>
</dependency>
<!-- quartz組件 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
搭建工程
- properties配置文件
① sys.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://IP:3306/sunpy-quartz?characterEncoding=utf-8
jdbc.username=用戶名
jdbc.password=密碼
jdbc.initialSize=1
jdbc.maxActive=30
jdbc.minIdle=1
② quartz.properties
org.quartz.scheduler.instanceName=myQuartzScheduler
org.quartz.scheduler.instanceId=AUTO
#quartz-scheduler出口本身通過RMI作爲服務器,然後設置“出口”標誌true(默認值爲false)
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=10
org.quartz.threadPool.threadPriority=5
#配置JobStore
org.quartz.jobSotre.useProperties=false
org.quartz.jobStore.tablePrefix=QRTZ_
#信息保存時間 默認值60秒
org.quartz.jobStore.misfireThreshold=60000
#開啓集羣
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=5000
org.quartz.jobStore.txIsolationLevelReadCommitted=true
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
- XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<context:component-scan base-package="cn.sunpy.*"></context:component-scan>
<context:annotation-config></context:annotation-config>
<context:property-placeholder location="classpath:sys.properties"/>
<beans:bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<beans:property name="driverClassName" value="${jdbc.driverClassName}"></beans:property>
<beans:property name="url" value="${jdbc.url}"></beans:property>
<beans:property name="username" value="${jdbc.username}"></beans:property>
<beans:property name="password" value="${jdbc.password}"></beans:property>
<beans:property name="initialSize" value="${jdbc.initialSize}"></beans:property>
<beans:property name="minIdle" value="${jdbc.minIdle}"></beans:property>
<beans:property name="maxActive" value="${jdbc.maxActive}"></beans:property>
</beans:bean>
<!-- 配置JobDetail -->
<beans:bean id="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<beans:property name="jobClass" value="cn.sunpy.quartz.MyJob"></beans:property>
</beans:bean>
<!-- 配置Cron觸發器 -->
<beans:bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<beans:property name="jobDetail" ref="myJobDetail"></beans:property>
<beans:property name="cronExpression" value="0 54 16 * * ?"></beans:property>
</beans:bean>
<!-- 配置調度者 -->
<beans:bean id="myScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<beans:property name="configLocation" value="classpath:quartz.properties"></beans:property>
<beans:property name="dataSource" ref="dataSource"></beans:property>
<beans:property name="triggers">
<beans:list>
<beans:ref bean="myCronTrigger"/>
</beans:list>
</beans:property>
<beans:property name="quartzProperties">
<beans:props>
<beans:prop key="org.quartz.scheduler.skipUpdateCheck">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
</beans>
- 任務
public class MyJob implements Job{
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("MyJob execute" + sdf.format(new Date()));
}
}
0人點贊
作者:sunpy
鏈接:https://www.jianshu.com/p/fd6dd2af89c5
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。