【分布式调度Elastic-job】

1.1什么是任务调度

我们可以思考一下下面业务场景的解决方案:

  • 某电商平台需要每天上午10点,下午3点,晚上8点发放一批优惠券
  • 某银行系统需要在信用卡到期还款日的前三天进行短信提醒
  • 某财务系统需要在每天凌晨0:10分结算前一天的财务数据,统计汇总

以上场景就是任务调度所需要解决的问题

1.2 为什么需要分布式调度

感觉Spring给我们提供的这个注解可以完成任务调度的功能,好像已经完美解决问题了,为什么还需要分布式呢?

主要有如下这几点原因:

1.单机处理极限:原本1分钟内需要处理1万个订单,但是现在需要1分钟内处理10万个订单;原来一个统计需要1小时,现在业务方需要10分钟就统计出来。你也许会说,你也可以多线程、单机多进程处理。的确,多线程并行处理可以提高单位时间的处理效率,但是单机能力毕竟有限(主要是CPU、内存和磁盘),始终会有单机处理不过来的情况。

2.高可用:单机版的定式任务调度只能在一台机器上运行,如果程序或者系统出现异常就会导致功能不可用。虽然可以在单机程序实现的足够稳定,但始终有机会遇到非程序引起的故障,而这个对于一个系统的核心功能来说是不可接受的。

3.防止重复执行: 在单机模式下,定时任务是没什么问题的。但当我们部署了多台服务,同时又每台服务又有定时任务时,若不进行合理的控制在同一时间,只有一个定时任务启动执行,这时,定时执行的结果就可能存在混乱和错误了

这个时候就需要分布式的任务调度来实现了。

启动zookeeper

1 把包解压一下 2 到conf目录中 把zoo_sample.cfg 拷贝一份 , 修改名字为zoo.cfg 3 到bin目录中启动startup.cmd文件

启动图形化界面

1 解压 2 到build目录中 ,cmd java -jar jar包名字 , 在图形化界面中成功进行连接

2.1 环境搭建

2.1.1 版本要求
  • JDK 要求1.7以上保本
  • Maven 要求3.0.4及以上版本
  • Zookeeper 要求采取3.4.6以上版本
2.1.2 Zookeeper安装&运行
2.1.3 创建Maven项目

添加如下依赖

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

2.2 代码实现

@Configuration
public class RegistryCenterConfig {

@Bean(initMethod = "init")
public CoordinatorRegistryCenter createRegistryCenter(@Value("${elasticjob.zookeeper-url}") String zookeeperUrl, @Value("${elasticjob.group-name}") String groupName) {
    //zk的配置
    ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(zookeeperUrl,groupName);
    //设置zk超时时间
    zookeeperConfiguration.setSessionTimeoutMilliseconds(5000);
    //创建注册中心
    CoordinatorRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
    return zookeeperRegistryCenter;
}

}

@Configuration
public class ElasticJobConfig {
    @Autowired
    private CoordinatorRegistryCenter registryCenter;
    @Autowired
    private MyElasticJob myElasticJob;


    //作业配置
    private static LiteJobConfiguration createJobConfiguration(Class clz,
                                                                String cron,
                                                                int shardingTotalCount,
                                                                String shardingItemParameters) {
        // 定义作业核心配置
        JobCoreConfiguration.Builder jobCoreConfigurationBuilder = JobCoreConfiguration.newBuilder(clz.getSimpleName(), cron, shardingTotalCount);
        if(!StringUtils.isEmpty(shardingItemParameters)){
            jobCoreConfigurationBuilder.shardingItemParameters(shardingItemParameters);
        }
        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(jobCoreConfigurationBuilder.build(), MyElasticJob.class.getCanonicalName());
        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();
        return simpleJobRootConfig;
    }

    @Autowired
    private FileCustomJob fileCustomJob;

/*    //入门案例
    @Bean(initMethod = "init")
    public SpringJobScheduler initSimpleElasticJob(){
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(myElasticJob,registryCenter,createJobConfiguration(myElasticJob.getClass(),"0/3 * * * * ?",1,null));
        return springJobScheduler;
    }
    //备份案例
    @Bean(initMethod = "init")
    public SpringJobScheduler initSimpleElasticJob1(){
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(fileCustomJob,registryCenter,createJobConfiguration(fileCustomJob.getClass(),"0/3 * * * * ?",1,null));
        return springJobScheduler;
    }*/
    //分片案例
    @Bean(initMethod = "init")
    public SpringJobScheduler initSimpleElasticJob(FileCustomJob fileCustomJob){

        return new SpringJobScheduler(fileCustomJob,registryCenter,createJobConfiguration(FileCustomJob.class,"0/3 * * * * ?",1,null));

    }
}
@Component
public class FileCustomJob implements SimpleJob {

    @Autowired
    private FileCustomMapper fileCustomMapper;

    @Override
    public void execute(ShardingContext shardingContext) {
        dowork();
    }

    private void dowork() {
        //查出数据
        List<FileCustom> fileCustoms = fileCustomMapper.selectFileCustomList();
        System.out.println("要备份的数据个数为 :" +fileCustoms.size());
        //2 循环遍历一个个进行模拟备份,, 修改为 状态为 1
        for (FileCustom fileCustom :fileCustoms){
            changeStatus(fileCustom);
        }
    }

    private void changeStatus(FileCustom fileCustom) {
        System.out.println("要备份的数据ID为 " +fileCustom.getId() +"要备份的数据类型为 " +fileCustom.getType());
        try {
            Thread.sleep(1000);
        }catch (Exception e){
            e.printStackTrace();
        }


        //修改 状态为
        fileCustomMapper.updateBackedUp(fileCustom.getId());
    }
}

运维管理

8.1 事件追踪

Elastic-Job-Lite在配置中提供了JobEventConfiguration,支持数据库方式配置,会在数据库中自动创建JOB_EXECUTION_LOG和JOB_STATUS_TRACE_LOG两张表以及若干索引来近路作业的相关信息。

1 .修改Elastic-Job配置类

在ElasticJobConfig配置类中注入DataSource

@Configuration
public class ElasticJobConfig {
    @Autowired
    private DataSource dataSource; 
	......
}

在任务配置中增加事件追踪配置

@Bean(initMethod = "init")
    public SpringJobScheduler initFileCustomElasticJob(FileCustomElasticJob fileCustomElasticJob){
        //增加任务事件追踪配置
        JobEventConfiguration jobEventConfiguration = new JobEventRdbConfiguration(dataSource);
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(
                fileCustomElasticJob,
                registryCenter,
                createJobConfiguration(FileCustomElasticJob.class,"0 0/1 * * * ?",4,"0=text,1=image,2=radio,3=vedio",false),
                jobEventConfiguration);
        return springJobScheduler;
    }

运维控制台

elastic-job中提供了一个elastic-job-lite-console控制台
1 .解压缩elastic-job-lite-console-2.1.5.tar
2 .进入bin目录,并执行:bin\start.bat
打开浏览器访问http://localhost:8899
用户名: root 密码: root,进入之后界面如下:

可以吧这么一个大的任务进行拆分,根据type进行拆分: image, text, vedio,radio ,拆分成4个小的任务
针对分片机制在elasicjob当中
如果你有一个台机器:开启4个线程去处理4个任务
如果你有两台机器:每个机器会开启两个线程去执行我们任务
如果你有三台机器: -台执行两个,两台分别执行1个任务
如果你有四台机器:每一个都执行1个任务..
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值