每天20分钟之elastic-job-lite的应用

2023-01-14 22:31:36 浏览数 (2)

简单使用

官网: https://shardingsphere.apache.org/elasticjob/current/cn/user-manual/elasticjob-lite/

简单的说:lite就是和本地运行定时任务没啥区别,只是帮你封装了zookeeper。仅此而已,理想的情况还是需要使用elasticjob-cloud的

image.pngimage.png

运行docker

docker run -d -e TZ="Asia/Shanghai" -p 2181:2181 -v /root/docker/zookeeper:/data --name zookeeper --restart always zookeeper

项目配置

  1. 增加依赖 <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>
  2. 增加配置
代码语言:txt复制
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
  1. 增加代码
代码语言:txt复制
@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");
    }
}

运行结果

image.pngimage.png

https://github.com/beckbikang/spring-cloud/tree/main/kcron/kelasticjob

0 人点赞