SpringBoot原生实现分布式MapReduce计算

一、架构设计调整

核心组件替换方案:

1、注册中心

→ 数据库注册表

2、任务队列

→ 数据库任务表

3、分布式锁

→ 数据库行级锁

4、节点通信

→ HTTP REST接口

二、数据库表结构设计

java 复制代码
 节点注册表
CREATETABLE compute_nodes (
    node_id VARCHAR(36)PRIMARYKEY,
    last_heartbeat TIMESTAMP,
    statusENUM('ACTIVE','DOWN')
);
java 复制代码
-- 任务分片表
CREATETABLE task_shards (
    shard_id INTAUTO_INCREMENTPRIMARYKEY,
    data_range VARCHAR(100),-- 例如:1-10000
    statusENUM('PENDING','PROCESSING','COMPLETED'),
    locked_by VARCHAR(36),
    locked_at TIMESTAMP
);

三、核心实现代码

1. 节点自注册实现

java 复制代码
@Scheduled(fixedRate =3000)
public void nodeRegistration(){
    jdbcTemplate.update(
        "INSERT INTO compute_nodes VALUES (?, NOW(), 'ACTIVE') "+
        "ON DUPLICATE KEY UPDATE last_heartbeat = NOW()",
        nodeId
    );
    
    // 清理过期节点
    jdbcTemplate.update(
        "DELETE FROM compute_nodes WHERE last_heartbeat < ?",
        LocalDateTime.now().minusSeconds(10)
    );
}

2. 任务分片抢占式调度

java 复制代码
@Scheduled(fixedDelay =1000)
public void acquireTasks(){
    List<Long> shardIds = jdbcTemplate.queryForList(
        "SELECT shard_id FROM task_shards "+
        "WHERE status = 'PENDING' "+
        "ORDER BY shard_id LIMIT 5 FOR UPDATE SKIP LOCKED",
        Long.class
    );
    
    shardIds.forEach(shardId ->{
        int updated = jdbcTemplate.update(
            "UPDATE task_shards SET status = 'PROCESSING', "+
            "locked_by = ?, locked_at = NOW() "+
            "WHERE shard_id = ? AND status = 'PENDING'",
            nodeId, shardId
        );
        if(updated >0) processShard(shardId);
    });
}

3. Map阶段分布式处理

java 复制代码
public void processShard(Long shardId){
    try{
        DataRange range =getDataRange(shardId);
        List<Record> records =fetchData(range);
        
        Map<String, Double> partialResult = records.parallelStream()
            .collect(Collectors.groupingBy(
                Record::getCategory,
                Collectors.summingDouble(Record::getAmount)
            ));
        
        saveResult(shardId, partialResult);
        markShardCompleted(shardId);
    }catch(Exception e){
        releaseShard(shardId);
    }
}

4. Reduce阶段聚合实现

java 复制代码
public Map<String, Double> reduceAllResults(){
    return jdbcTemplate.query(
        "SELECT category, SUM(amount) AS total "+
        "FROM map_results GROUP BY category",
        (rs, rowNum)->newAbstractMap.SimpleEntry<>(
            rs.getString("category"),
            rs.getDouble("total")
        )).stream().collect(Collectors.toMap(
            Entry::getKey,Entry::getValue
        ));
}

四、关键优化点

1. 分片锁优化策略

java 复制代码
// 使用乐观锁避免长时间占用连接
public boolean tryLockShard(Long shardId) {
    return jdbcTemplate.update(
        "UPDATE task_shards SET version = version + 1 " +
        "WHERE shard_id = ? AND version = ?",
        shardId, currentVersion) > 0;
}

2. 结果缓存优化

java 复制代码
@Cacheable(value ="partialResults", key ="#shardId")
public Map<String, Double> getPartialResult(Long shardId){
    return jdbcTemplate.query(...);
}

// 配置类启用缓存
@Configuration
@EnableCaching
publicclassCacheConfig{
    @Bean
    public CacheManagercacheManager(){
        return new ConcurrentMapCacheManager();
    }
}

3. 分布式事务处理

java 复制代码
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void markShardCompleted(Long shardId) {
    jdbcTemplate.update(
        "UPDATE task_shards SET status = 'COMPLETED' " +
        "WHERE shard_id = ?", shardId);
    
    eventPublisher.publishEvent(
        new ShardCompleteEvent(shardId));
}

五、部署架构对比

六、性能压测数据

测试环境:

100w数据

七、生产级改进建议

分片策略优化

java 复制代码
// 采用跳跃哈希算法避免热点
public List<Long> assignShards(int totalShards) {
    return IntStream.range(0, totalShards)
        .mapToObj(i -> (nodeHash + i*2654435761L) % totalShards)
        .collect(Collectors.toList());
}

动态分片扩容

java 复制代码
@Scheduled(fixedRate =60000)
public void autoReshard(){
    int currentShards = getCurrentShardCount();
    int required = calculateRequiredShards();
    
    if(required > currentShards){
        jdbcTemplate.execute("ALTER TABLE task_shards AUTO_INCREMENT = "+ required);
    }
}

结果校验机制

java 复制代码
public void validateResults() {
    jdbcTemplate.query("SELECT shard_id FROM task_shards WHERE status = 'COMPLETED'", 
        rs -> {
            Long shardId = rs.getLong(1);
            if(!resultCache.contains(shardId)) {
                repairShard(shardId);
            }
        });
}

该方案完全基于SpringBoot原生能力实现,通过关系型数据库+定时任务调度机制,在保持系统简洁性的同时满足基本分布式计算需求。适合中小规模(日处理千万级以下)的离线计算场景,如需更高性能建议仍考虑引入专业分布式计算框架。

相关推荐
java小白小1 天前
SpringBoot(01): 初识SpringBoot,从Spring的痛点说起
spring boot
用户3169353811832 天前
如何从零编写一个 Spring Boot Starter
spring boot
程序员晓琪2 天前
约定大于配置:基于 Java 包名自动生成 API 版本路由的最佳实践
java·spring boot·后端
Flittly2 天前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
用户3521802454753 天前
🎆从 Prompt 到 Skill:让 Spring AI Agent 学会"装新技能"
人工智能·spring boot·ai编程
用户3521802454756 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
昵称为空C7 天前
手撸一个动态 SQL 执行引擎:不重启服务,在线增删改查任意数据库
spring boot·后端
霸道流氓气质7 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
于先生吖7 天前
SpringBoot对接大模型开发AI命理测算系统:八字排盘与AI解析接口源码全解
人工智能·spring boot·后端