Elastic-job,是当当开源的一款定时任务框架,有很多的公司在用,在稳定性上面还是有很大的保证的.简单介绍一下它,当当定时任务是一个去中心化的分布式的服务,仅仅依赖的只有zookeeper,它可以灵活的对我们的任务进行分片,在我之前的工作当中,集群的环境下,有的任务需要几台服务器同时执行,也有的任务只需要一台服务器执行,Elastic-job都可以完美的支持,更重要的一点是,基本上定时任务的所有配置都有图形化的配置界面. 给一个它的官方网址 http://elasticjob.io/index_zh.html ,有兴趣的朋友可以去详细了解一下.话不多说我们下面进入正题. 我这里使用的编辑器是idea,首先我们去新建一个工程,如下图 一直next到最后就好了,我这里为了方便,还在配置页面选择了一个web的依赖 打开我们新建的工程,编辑我们的pom文件,添加我们的Elastic-job的依赖,如下图 图中的依赖代码在这
<dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-spring</artifactId> <version>2.1.5</version> </dependency>接下来我们新建一个测试的定时任务类,TestJob,实现SimpleJob,并交给spring容器管理就ok了,完整代码如下:
package com.example.demo; import com.dangdang.ddframe.job.api.ShardingContext; import com.dangdang.ddframe.job.api.simple.SimpleJob; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; /** * 这是我们的测试job类 * @author * @date 2018/10/25. */ @Component public class TestJob implements SimpleJob { Logger logger = LoggerFactory.getLogger(TestJob.class); @Override public void execute(ShardingContext shardingContext) { logger.info("我是一个定时任务"); } }然后在我们的resources下面新建一个jobs.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:reg="http://www.dangdang.com/schema/ddframe/reg" xmlns:job="http://www.dangdang.com/schema/ddframe/job" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.dangdang.com/schema/ddframe/reg http://www.dangdang.com/schema/ddframe/reg/reg.xsd http://www.dangdang.com/schema/ddframe/job http://www.dangdang.com/schema/ddframe/job/job.xsd "> <!--配置作业注册中心 --> <reg:zookeeper id="regCenter" server-lists="127.0.0.1:2181" namespace="test" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" /> <!-- 配置作业 下面的TestJob就是我们刚才新建的测试类--> <job:simple id="testJob" class="com.example.demo.TestJob" registry-center-ref="regCenter" cron="0 * * * * ?" sharding-item-parameters="0=A,1=B,2=C" description="这个一个定时任务" sharding-total-count="3"/> </beans>写到这里我们发现我们少了一个zookeeper,这里就不详细描述如何安装zookeeper了,提供2个博客给大家: Windows版:https://www.cnblogs.com/shanyou/p/3221990.html Linux版:https://www.cnblogs.com/zengxiaoliang/p/8442815.html 把上面jobs.xml文件中的server-lists中的ip替换成自己的就好了,这里在简单介绍一下我们配置文件中的cron表达式的意思是每分钟执行一次,这里也不详细介绍了.此时我们的目录结构是这样的 下面就是我们在启动项目前的最后一步了,打开我们的启动类,也就是我们上面截图的DemoApplication,把我们的配置文件注入进去,代码如下:
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(locations={"classpath:jobs.xml"}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }下面我们点击运行,看一下效果吧!等待一分钟后我们会看到我们的控制台输出了我们上面再TestJob中打印的语句,截图如下: 到这里就说明我们的定时任务正常启动了. 最后我们在说一下我们上文提到的图形化的配置界面.我们需要下载Elastic-job的源码,然后使用maven命令进行编译,就可以得到了,这里我提供一个已经下载好的,只需要解压,windows用户点击bin目录下的start.bat,mac或者linux用户执行start.sh脚本就可以了 链接:https://pan.baidu.com/s/1IhuL9JwDUvRTCtIbyW6uTg 密码:pany 浏览器上输入127.0.0.1:8899,输入用户名root,密码root,进入界面,添加我们的注册中心也就是zookeeper地址,就ok了! 剩下的界面上的功能就由大家自己去解锁了!!!
