1.jdk.8
2.apache-maven-3.5.4
3.zookeeper-3.4.12(下面会详细介绍windows安装教程)
在我们项目开发中会写很多定时任务,但是我们集群部署时候就尴尬了,如果用springboot自带的定时任务,多节点部署我们只能打包一个项目部署后,在打包另一个时候将另一个节点定时任务注释掉,但是单节点的定时任务,一旦出现问题整个定时任务全部宕掉,所以这时候就为了上面的问题出现了elastic-job分布式定时任务。下面我们简单了解整体elastic-job。
zookeepr如果不了解,请自行学习下zookeeper,后期我会单独讲解zookeeper,简单说几句:zookeeper就是注册中心,以key-value样的key为长久节点,value为临时节点存在的,类似于文件夹这样的树状结构!在市面上dubbo和zookeeper比较常见!
1.现在D盘建立一个zookeeper文件
2.在zookeeper文件下建立两个文件data和log两个文件
3.在zookeeper文件在建立个文件elasticjob-console(来放入elastic-job-lite-console控制
zookeeper我的网盘下载地址
链接:https://pan.baidu.com/s/1CuOIWlyy1hJWzEDsS8BZmA
提取码:i6fh
下载完zookeeper放入到上面建立的zookeeper文件下解压
在下载elasticjob-console控制台,这是管理elastic-job
数字化管理平台
链接:https://pan.baidu.com/s/1uZclYiF8-odQneVj1j9sKQ
提取码:8i5t
下载完elasticjob-console放入到上面建立的elasticjob-console文件下解压
1.pom.xml需要的jar
com.dangdang
elastic-job-lite-core
2.1.5
com.dangdang
elastic-job-lite-spring
2.1.5
org.apache.zookeeper
zookeeper
3.4.9
2.在application.properties
#加载文件
spring.profiles.include=job
#配置Elastic-Job需要的zookeeper
regCenter.serverList = localhost:2181
regCenter.namespace = elastic-job-lite-springboot
新建一个配置文件叫做application-job.properties(主要来加载定时任务信息配置)
#处理定时业务1 stockJob.cron为定时任务时间,stockJob.shardingTotalCount为总共设置片数,stockJob.shardingItemParameters为每个片数定义的名
1.stockJob.cron = 0 */1 * * * ?
1.stockJob.shardingTotalCount = 9
1.stockJob.shardingItemParameters = 0=A,1=B,2=C,3=D,4=E,5=F,6=G,7=H,8=I
#处理定时业务2,这是第二个定时任务,可以不用管
2.stockJob.cron = 0 */1 * * * ?
2.stockJob.shardingTotalCount = 9
2.stockJob.shardingItemParameters = 0=A,1=B,2=C,3=D,4=E,5=F,6=G,7=H,8=I
3.建立config文件夹在springboot项目中
package com.example.test.config;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.example.test.elasticjobutil.MyJobBusinessOne;
import com.example.test.elasticjobutil.MyJobBusinessTwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* job配置管理类
**/
@Configuration
public class ElasticJobConfig {
@Autowired
private ZookeeperRegistryCenter regCenter;
@Resource
MyJobBusinessOne MyJobBusinessOne;
@Resource
MyJobBusinessTwo MyJobBusinessTwo;
/**
* 处理定时业务1
* @param cron
* @param shardingTotalCount
* @param shardingItemParameters
* @return
*/
@Bean(initMethod = "init")
public JobScheduler myJobBusinessOneSimpleJobScheduler( @Value("${1.stockJob.cron}") final String cron, @Value("${1.stockJob.shardingTotalCount}") final int shardingTotalCount, @Value("${1.stockJob.shardingItemParameters}") final String shardingItemParameters) {
return new SpringJobScheduler(MyJobBusinessOne, regCenter, getLiteJobConfiguration(MyJobBusinessOne.getClass(), cron, shardingTotalCount, shardingItemParameters));
}
// /**
// * 处理定时业务2
// * @param cron
// * @param shardingTotalCount
// * @param shardingItemParameters
// * @return
// */
// @Bean(initMethod = "init")
// public JobScheduler myJobBusinessTwoSimpleJobScheduler( @Value("${2.stockJob.cron}") final String cron, @Value("${2.stockJob.shardingTotalCount}") final int shardingTotalCount, @Value("${2.stockJob.shardingItemParameters}") final String shardingItemParameters) {
// return new SpringJobScheduler(MyJobBusinessTwo, regCenter, getLiteJobConfiguration(MyJobBusinessTwo.getClass(), cron, shardingTotalCount, shardingItemParameters));
// }
/**
*@Description 任务配置类
* 如有详细配置,可使用JobCoreConfiguration另外的构造器,这里选取了必要的配置
*/
private LiteJobConfiguration getLiteJobConfiguration(final Class extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters){
return LiteJobConfiguration
.newBuilder(
new SimpleJobConfiguration(
JobCoreConfiguration.newBuilder(
jobClass.getName(),cron,shardingTotalCount)
.shardingItemParameters(shardingItemParameters)
.build()
,jobClass.getCanonicalName()
)
)
.overwrite(true)
.build();
}
}
package com.example.test.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
@Configuration
@ConditionalOnExpression("'${regCenter.serverList}'.length() > 0")
public class JobRegistryCenterConfig {
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList, @Value("${regCenter.namespace}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
}
上面两个文件都是elastic-job启动加载的配置信息,需要加载bean中
建立定时任务执行方法
package com.example.test.elasticjobutil;
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.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @description: TODO
* @author: Lucf
* @create: 2020-07-17 10:02
*/
@Service
public class MyJobBusinessOne implements SimpleJob {
private static final Logger logger = LoggerFactory.getLogger(MyJobBusinessOne.class);
@Autowired
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Override
public void execute(ShardingContext shardingContext) {
logger.info("MyJobBusinessOne:分布式定时任务开始");
logger.info(String.format("Thread ID: %s, 作业分片总数: %s, " +
"当前分片项: %s.当前参数: %s," +
"作业名称: %s.作业自定义参数: %s"
,
Thread.currentThread().getId(),
shardingContext.getShardingTotalCount(),
shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),
shardingContext.getJobName(),
shardingContext.getJobParameter()
));
logger.info("MyJobBusinessOne:分布式定时任务结束");
}
}
package com.example.test.elasticjobutil;
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.Service;
/**
* @description: TODO
* @author: Lucf
* @create: 2020-07-17 10:02
*/
@Service
public class MyJobBusinessTwo implements SimpleJob {
private static final Logger logger = LoggerFactory.getLogger(MyJobBusinessTwo.class);
@Override
public void execute(ShardingContext shardingContext) {
logger.info("MyJobBusinessTwo:分布式定时任务开始");
logger.info(String.format("Thread ID: %s, 作业分片总数: %s, " +
"当前分片项: %s.当前参数: %s," +
"作业名称: %s.作业自定义参数: %s"
,
Thread.currentThread().getId(),
shardingContext.getShardingTotalCount(),
shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),
shardingContext.getJobName(),
shardingContext.getJobParameter()
));
}
}
4.上面就OK了
5.我们需要进入D:\zookeeper\zookeeper-3.4.12\bin
双击zkServer.cmd,看见zookeeper启动成功后,这时候启动项目会看到下面信息
6.我们复制上面项目,在启动一个项目,这样我们分片数是9,elastic-job会自动分别分给两个项目,第一个为,0123,第二个为45678,,在家一个项目elastic-job还会进行自动分片,这样就满足了集群定时任务。
你可以根据两个项目打印日志就能看出来,他自动分片信息了,一目了然,我这里就不给大家截图了。
7.我们需要进入D:\zookeeper\elasticjob-console\elastic-job-lite-console-2.1.5\bin
双击start.bat,会看见elasticjob-console控制台启动成功,网页输入http://127.0.0.1:8899/。就可以进入控制台了,默认密码账号都是root
点击链接