第一篇需要实现一个最简单的需求:某个任务定时执行,多台机子只让其中一台机子执行任务
一、安装 分布式应用程序协调服务 zookeeper,安装步骤在链接里面
二、在springboot项目中引入 elastic-job 依赖,我这里用的 springboot 2.0.5 版本
整合代码参考官方的springboot demo
对应的 elastic-job 版本
2.1.5
com.dangdang
elastic-job-lite-core
${elastic-job.version}
com.dangdang
elastic-job-lite-spring
${elastic-job.version}
依赖XML代码
application.yaml 加入配置
regCenter:
serverList: localhost:2181
namespace: my-project
simpleJob:
cron: 0/15 * * * * ?
shardingTotalCount: 1
shardingItemParameters: 0=a
dataflowJob:
cron: 0/5 * * * * ?
shardingTotalCount: 3
shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
application.yaml 配置代码
三、作业代码(simpleJob类型的任务),把官方的demo拿到自己的项目中来就行,注意添加一个@Component注解在类上
/** Copyright 1999-2015 dangdang.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*http://www.apache.org/licenses/LICENSE-2.0*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/packagecom.dianji.task_server.job.exec;importcom.dangdang.ddframe.job.api.ShardingContext;importcom.dangdang.ddframe.job.api.simple.SimpleJob;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;importjava.text.MessageFormat;importjava.text.SimpleDateFormat;importjava.util.Date;
@Slf4j
@Componentpublic class SpringSimpleJob implementsSimpleJob {
@Value("${flag}")privateString serverFlag;
@Overridepublic void execute(finalShardingContext shardingContext) {int shardingItem =shardingContext.getShardingItem();if (shardingItem == 0) {
log.info("这是来自『{}』的任务,SIMPLE「SpringSimpleJob_execute」", serverFlag + ":" +shardingContext.getShardingParameter());
String logStr= "Item: {0} | Parameter: {1} | : {2} | Time: {3} | Thread: {4} | {5}";
logStr=MessageFormat.format(logStr, shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),new SimpleDateFormat("HH:mm:ss").format(newDate()),
Thread.currentThread().getId(),"SIMPLE「SpringSimpleJob_execute」");
log.info(logStr);
}else if (shardingItem == 1) {
log.info("这是来自『{}』的任务", serverFlag + ":" +shardingContext.getShardingParameter());
}else if (shardingItem == 2) {
log.info("这是来自『{}』的任务", shardingContext.getShardingParameter());
}
}
}
作业任务代码
四、配置作业(直接将官方的配置代码拿进自己的项目中)
/** Copyright 1999-2015 dangdang.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*http://www.apache.org/licenses/LICENSE-2.0*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/packagecom.dianji.task_server.job.config;importcom.dangdang.ddframe.job.api.simple.SimpleJob;importcom.dangdang.ddframe.job.config.JobCoreConfiguration;importcom.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;importcom.dangdang.ddframe.job.event.JobEventConfiguration;importcom.dangdang.ddframe.job.lite.api.JobScheduler;importcom.dangdang.ddframe.job.lite.config.LiteJobConfiguration;importcom.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;importcom.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;importcom.dianji.task_server.job.exec.SpringSimpleJob;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.annotation.Resource;
@Configurationpublic classSimpleJobConfig {
@ResourceprivateZookeeperRegistryCenter regCenter;
@ResourceprivateJobEventConfiguration jobEventConfiguration;
@BeanpublicSimpleJob simpleJob() {return newSpringSimpleJob();
}
@Bean(initMethod= "init")public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @Value("${simpleJob.cron}") final String cron, @Value("${simpleJob.shardingTotalCount}") final intshardingTotalCount,
@Value("${simpleJob.shardingItemParameters}") finalString shardingItemParameters) {return newSpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);
}private LiteJobConfiguration getLiteJobConfiguration(final Class extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, finalString shardingItemParameters) {return LiteJobConfiguration.newBuilder(newSimpleJobConfiguration(JobCoreConfiguration.newBuilder(
jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
}
}
作业配置代码
五、最简单的整合步骤就完成了,启动应用(分别启动三个进程),看见任务开始执行了
最简单的任务需求得到了满足,定时执行,多台机子只让其中一台机子执行任务
关于分片的数量设置,实际操作了一把
假如:有3个进程实例
并且elastic-job 设置
分片 1 时:3个进程只有1一个进程执行1次任务,共执行1次任务
分片 2 时:3个进程中的2个进程一共执行2次任务,共执行2次任务
分片 3 时:3个进程每个执行1次任务,共执行3次任务
分片 4 时:3个进程同时一共执行4次任务,共执行4次任务
分片 5 时:3个进程同时一共执行5次任务,共执行5次任务
分片为X时,此任务同时在Y台个进程里面执行X次。
这样就可以根据不同的使用场景配置不同的分片数量了