xxl-job - 分片任务实战

【基本原理】

● 概念

index - 当前分片序号(从0开始),执行器集群列表中当前执行器的序号;
total - 总分片数,执行器集群的总机器数量。

● 业务代码中获取

  1.ShardIndex - 分片索引号
  
        int shardIndex = XxlJobHelper.getShardIndex();
        
  2.ShardTotal - 分片总数,即注册的业务服务实例总数
  
        int shardTotal = XxlJobHelper.getShardTotal();

● 原理描述:

1.各个业务实例注册到admin调度中心后,在业务代码中获取 分片总数(ShardTotal) 和 调度中心计算出的 分片索引(ShardIndex)

2.查询任务数据时,使用 ShardTotal 进行 mod(id, ShardTotal) 取模操作,使用 ShardIndex 作为各个分片的数据筛选过滤条件

@Select("select * from t_user_mobile_plan where mod(id, #{ShardTotal}) = #{ShardIndex}")

3.各个分片实例获取到自己任务的数据,并行执行任务,提升性能和效率

【分片 - 实战】

1.创建任务,引入依赖

        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>2.3.1</version>
        </dependency>


2.配置 application.properties


# web port
server.port=9001

#
spring.application.name=xxl-job-shards-sample
# no web
#spring.main.web-environment=false

# log config
#  logback.xml 先于 application.properties 加载,自然会因得不到 log.path | springProperty 而报错
# 改用 logback-spring.xml 作为日志配置文件名称
logging.config=classpath:logback-spring.xml

# DataSource Config
spring.datasourc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值