【基本原理】
● 概念 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