前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >每天20分钟之elastic-job-lite的应用

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

原创
作者头像
李子健
修改2023-01-14 22:31:36
5070
修改2023-01-14 22:31:36
举报
文章被收录于专栏:每日一善

简单使用

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

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

image.png
image.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.png
image.png

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简单使用
    • 项目配置
    相关产品与服务
    容器镜像服务
    容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档