Elastic-Job分片
AbstractElasticJob在執行job的時候會獲取分片內容JobExecutionMultipleShardingContext,通過shardingService.shardingIfNecessary()進行分片。分片默認得分片策略是均分算法AverageAllocationJobShardingStrategy.sharding(),獲取本機的分片序列號shardingService.getLocalHostShardingItems(),並把分片序列號進行set。我們在寫具體的業務job,實現process方法時候,可以通過shardingContext獲取分片序列號shardingItems。
例如:
投標分10片,那麼每臺服務器都會獲取到對應的分片序列號。當一臺服務器那麼分片序列號是0,1,2,3,4,5,6,7,8,9,當兩臺服務器的時候分片序列號是服務器A-0,1,2,3,4服務器B-5,6,7,8,9,實現可以根據查詢業務數據進行進行%10取餘,如果取到的餘在獲取的分片序列號內進行投標處理,如果不在則不處理。投標考慮標id和賬戶id都進行%10是否在滿足分片進行具體的投標處理。
序列號對應參數配置shardingItemParameters
部分代碼片度如下:
均分策略AverageAllocationJobShardingStrategy
附測試代碼:
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Lists;
import com.dangdang.ddframe.job.internal.sharding.strategy.JobShardingStrategyOption;
import com.google.common.collect.Maps;
public class Testss {
public static void main(String[] args) {
List<String> serversList = Lists.newArrayList(2);
serversList.add("ip1");
serversList.add("ip2");
int shardingTotalCount = 10;
Map<Integer, String> shardingItemParameters = Maps.newHashMapWithExpectedSize(0);
JobShardingStrategyOption option = new JobShardingStrategyOption("autoLoanBid", shardingTotalCount, shardingItemParameters);
Map<String, List<Integer>> map = sharding(serversList, option);
System.out.println(JSONObject.toJSONString(map));
}
public static Map<String, List<Integer>> sharding(final List<String> serversList, final JobShardingStrategyOption option) {
if (serversList.isEmpty()) {
return Collections.emptyMap();
}
Map<String, List<Integer>> result = shardingAliquot(serversList, option.getShardingTotalCount());
addAliquant(serversList, option.getShardingTotalCount(), result);
return result;
}
private static Map<String, List<Integer>> shardingAliquot(final List<String> serversList, final int shardingTotalCount) {
Map<String, List<Integer>> result = new LinkedHashMap<>(serversList.size());
int itemCountPerSharding = shardingTotalCount / serversList.size();
int count = 0;
for (String each : serversList) {
List<Integer> shardingItems = new ArrayList<>(itemCountPerSharding + 1);
for (int i = count * itemCountPerSharding; i < (count + 1) * itemCountPerSharding; i++) {
shardingItems.add(i);
}
result.put(each, shardingItems);
count++;
}
return result;
}
private static void addAliquant(final List<String> serversList, final int shardingTotalCount, final Map<String, List<Integer>> shardingResult) {
int aliquant = shardingTotalCount % serversList.size();
int count = 0;
for (Entry<String, List<Integer>> entry : shardingResult.entrySet()) {
if (count < aliquant) {
entry.getValue().add(shardingTotalCount / serversList.size() * serversList.size() + count);
}
count++;
}
}
}