分布式定时任务

目录

1.xxl-job

2.elastic-job


1.xxl-job


xxl-job 是大众点评(许雪里)开发的一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。

xxl-job 框架对 quartz 进行了扩展,使用 mysql 数据库存储数据,并且内置jetty作为 RPC服务调用。

主要特点如下:

有界面维护定时任务和触发规则,非常容易管理。
能动态启动或停止任务
支持弹性扩容缩容
支持任务失败报警
支持动态分片
支持故障转移
Rolling实时日志
支持用户和权限管理

使用quartz架构图如下:

项目实战

xxl-admin 管理后台部署和mysql脚本执行等这些前期准备工作,我就不过多介绍了,有需求的朋友可以找我私聊,这些更偏向于运维的事情。

假设前期工作已经OK了,接下来我们需要:

第一步,在pom.xml文件中引入 xxl-job 相关依赖。

<dependency>
   <groupId>com.xuxueli</groupId>
   <artifactId>xxl-job-core</artifactId>
</dependency>

第二步,在 applicationContext.properties 文件中配置参数:

xxl.job.admin.address: http://localhost:8088/xxl-job-admin/
xxl.job.executor.appname: xxl-job-executor-sample
xxl.job.executor.port: 8888
xxl.job.executor.logpath: /data/applogs/xxl-job/

第三步,创建HelloJobHandler类继承 IJobHandler 类:

@JobHandler(value = "helloJobHandler")
@Component
public class HelloJobHandler extends IJobHandler {
 
    @Override
    public ReturnT<String> execute(String param) {
        System.out.println("XXL-JOB, Hello World.");
        return SUCCESS;
    }
}

这样定时任务就配置好了。

建议把定时任务单独部署到另外一个服务中,跟api服务分开。根据我以往的经验,job大部分情况下,会对数据做批量操作,如果操作的数据量太大,可能会对服务的内存和cpu资源造成一定的影响。

使用 xxl-job 的优缺点:
优点:有界面管理定时任务,支持弹性扩容缩容、动态分片、故障转移、失败报警等功能。它的功能非常强大,很多大厂在用,可以满足绝大多数业务场景。
缺点:和 quartz 一样,通过数据库分布式锁,来控制任务不能重复执行。在任务非常多的情况下,有一些性能问题。

 

2.elastic-job


elastic-job 是当当网开发的弹性分布式任务调度系统,功能丰富强大,采用zookeeper实现分布式协调,实现任务高可用以及分片。它是专门为高并发和复杂业务场景开发。

elastic-job 目前是 apache 的 shardingsphere 项目下的一个子项目,官网地址:http://shardingsphere.apache.org/elasticjob/。

elastic-job 在2.x之后,出了两个产品线: Elastic-Job-Lite 和 Elastic-Job-Cloud ,而我们一般使用Elastic-Job-Lite就能够满足需求。Elastic-Job-Lite定位为轻量级无中心化解决方案,使用jar包的形式提供分布式任务的协调服务,外部仅依赖于Zookeeper。。

主要特点如下:

分布式调度协调
弹性扩容缩容
失效转移
错过执行作业重触发
作业分片一致性,保证同一分片在分布式环境中仅一个执行实例
自诊断并修复分布式不稳定造成的问题
支持并行调度

项目实战

第一步,在pom.xml文件中引入 elastic-job 相关依赖。

<dependency>
    <groupId>com.dangdang</groupId>
    <artifactId>elastic-job-lite-core</artifactId>
</dependency>
<dependency>
    <groupId>com.dangdang</groupId>
    <artifactId>elastic-job-lite-spring</artifactId>
</dependency>

第二步,增加ZKConfig类,配置 zookeeper :

@Configuration
@ConditionalOnExpression("'${zk.serverList}'.length() > 0")
public class ZKConfig {
 
    @Bean
    public ZookeeperRegistryCenter registry(@Value("${zk.serverList}") String serverList,
                                             @Value("${zk.namespace}") String namespace) {
        return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
    }
 
}

第三步,定义一个类实现 SimpleJob 接口:

public class TestJob implements SimpleJob {
 
    @Override
    public void execute(ShardingContext shardingContext){
        System.out.println("ShardingTotalCount:"+shardingContext.getShardingTotalCount());
        System.out.println("ShardingItem:"+shardingContext.getShardingItem());
    }
}

第四步,增加JobConfig配置任务:

@Configuration
public class JobConfig {
    @Value("${sue.spring.elatisc.cron}")
    private String testCron;
    @Value("${sue.spring.elatisc.itemParameters}")
    private  String shardingItemParameters;
    @Value("${sue.spring.elatisc.jobParameters}")
    private String jobParameters =;
    @Value("${sue.spring.elatisc.shardingTotalCount}")
    private int shardingTotalCount;
    
    @Autowired
    private ZookeeperRegistryCenter registryCenter;
 
    @Bean
    public SimpleJob testJob() {
        return new TestJob();
    }
 
    @Bean
    public JobScheduler simpleJobScheduler(final SimpleJob simpleJob) {
        return new SpringJobScheduler(simpleJob, registryCenter, getConfiguration(simpleJob.getClass(),
                cron, shardingTotalCount, shardingItemParameters, jobParameters));
    }
 
    private geConfiguration getConfiguration(Class<? extends SimpleJob> jobClass,String cron,int shardingTotalCount,String shardingItemParameters,String jobParameters) {
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), testCron, shardingTotalCount).
                shardingItemParameters(shardingItemParameters).jobParameter(jobParameters).build();
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName());
        LiteJobConfiguration jobConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();
        return jobConfig;
    }
}

其中:

cron:cron表达式,定义触发规则。
shardingTotalCount:定义作业分片总数
shardingItemParameters:定义分配项参数,一般用分片序列号和参数用等号分隔,多个键值对用逗号分隔,分片序列号从0开始,不可大于或等于作业分片总数。
jobParameters:作业自定义参数
第五步,在 applicationContext.properties 文件中配置参数:

spring.application.name=elasticjobDemo
zk.serverList=localhost:2181
zk.namespace=elasticjobDemo
sue.spring.elatisc.cron=0/5 * * * * ?
sue.spring.elatisc.itemParameters=0=A,1=B,2=C,3=D
sue.spring.elatisc.jobParameters=test
sue.spring.elatisc.shardingTotalCount=4

这样定时任务就配置好了,创建定时任务的步骤,相对于 xxl-job 来说要繁琐一些。

使用 elastic-job 的优缺点:
优点:支持分布式调度协调,支持分片,适合高并发,和一些业务相对来说较复杂的场景。
缺点:需要依赖于zookeeper,实现定时任务相对于 xxl-job 要复杂一些,要对分片规则非常熟悉。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值