简单使用
官网: https://shardingsphere.apache.org/elasticjob/current/cn/user-manual/elasticjob-lite/
简单的说:lite就是和本地运行定时任务没啥区别,只是帮你封装了zookeeper。仅此而已,理想的情况还是需要使用elasticjob-cloud的
运行docker
docker run -d -e TZ="Asia/Shanghai" -p 2181:2181 -v /root/docker/zookeeper:/data --name zookeeper --restart always zookeeper
项目配置
- 增加依赖 <properties> <springboot.version>2.3.1.RELEASE</springboot.version> <spring.version>5.2.7.RELEASE</spring.version> <rversion>3.0.0</rversion> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.shardingsphere.elasticjob/elasticjob-lite-spring-boot-starter --> <dependency> <groupId>org.apache.shardingsphere.elasticjob</groupId> <artifactId>elasticjob-lite-spring-boot-starter</artifactId> <version>${rversion}</version> </dependency> </dependencies>
- 增加配置
elasticjob:
tracing:
type: RDB
regCenter:
serverLists: localhost:2181
namespace: elasticjob-lite-springboot
jobs:
simpleJob:
elasticJobClass: cn.beckbi.cron.SpringBootSimpleJob
cron: 0/5 * * * * ?
shardingTotalCount: 3
shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
dump:
port: 9888
server:
port: 7191
- 增加代码
@Slf4j
@Component
public class SpringBootSimpleJob implements SimpleJob {
@Override
public void execute(final ShardingContext shardingContext) {
log.info("Item: {} | Time: {} | Thread: {} | {}",
shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "SIMPLE");
}
}
运行结果
https://github.com/beckbikang/spring-cloud/tree/main/kcron/kelasticjob