/images/logo.jpg

[Cluster] 1. RAFT Algorithm: A Complete Evolution from Single Node to Distributed Consensus

[Cluster] 1. RAFT Algorithm: A Complete Evolution from Single Node to Distributed Consensus

Introduction

  • RAFT (Raft Consensus Algorithm) is a distributed consensus algorithm designed to solve the problem of achieving data state agreement among multiple nodes in distributed systems.
  • Compared to the renowned Paxos algorithm, RAFT’s design philosophy emphasizes “understandability” through clear role separation and straightforward state transitions, making it easier for developers to comprehend and implement.
  • This article demonstrates the complete evolution of the RAFT algorithm from single-node to multi-node clusters through 11 detailed diagrams, covering key scenarios including normal operation, failure handling, network partitions, and conflict resolution.

Core RAFT Concepts

  • Before diving into the analysis, let’s familiarize ourselves with several core concepts of RAFT:

Node States:

[Cluster] 1. 浅谈RAFT算法:从单节点到分布式共识的完整演进

[Cluster] 1. 浅谈RAFT算法:从单节点到分布式共识的完整演进

引言

  • RAFT(Raft Consensus Algorithm)是一种分布式共识算法,旨在解决分布式系统中多个节点对数据状态达成一致的问题。
  • 相比于著名的Paxos算法,RAFT的设计理念是"可理解性"(understandability),通过清晰的角色划分和简洁的状态转换,让开发者更容易理解和实现。
  • 本文将通过11个详细的图例,展示RAFT算法从单节点到多节点集群的完整演进过程,包括正常运行、故障处理、网络分区和冲突解决等关键场景。

RAFT核心概念

  • 在深入分析之前,让我们先了解RAFT的几个核心概念:

节点状态(Node States)

  • Leader(领导者):负责处理客户端请求,向其他节点复制日志条目
  • Follower(跟随者):被动接收Leader的日志复制请求
  • Candidate(候选者):Leader选举过程中的临时状态

关键数据结构

  • Log(日志):存储操作命令的有序序列
  • Term(任期):单调递增的逻辑时钟,用于检测过期信息
  • CommitIndex(提交索引):已知被提交的最高日志条目索引
  • ApplyIndex/LastApplied(应用索引):已应用到状态机的最高日志条目索引
  • State(状态机):实际的业务数据状态

阶段一:单节点启动(图1)

1
2
3
4
5
Node1启动为Leader,初始状态:
- Log=[](空日志)
- Term=0(初始任期)
- CommitIndex=0, ApplyIndex=0(无已提交/应用的条目)
- State={}(空状态机)

/images/9.%20raft/1.%20single%20node.svg

单节点集群中,节点自动成为Leader,因为它构成了"多数派"(1 > 1/2)。这是RAFT算法的一个重要特性:任何时刻最多只能有一个Leader。

阶段二:处理客户端请求(图2)

1
2
3
4
5
6
7
客户端请求:x=1

处理流程:
1. Leader将"x=1"追加到日志(Log=[x=1])
2. 更新Term=1(在某些实现中,term在接收客户端请求时更新)
3. 单节点立即提交(CommitIndex=1)
4. 应用到状态机(ApplyIndex=1, State={x:1})

/images/9.%20raft/2.%20add%20value.svg

[Github] 2. MyBatis Generator Custom Plugins

[Github] 2. MyBatis Generator Custom Plugins

🔗 Project Repository: mybatis-generator-custome-plugins

A powerful collection of MyBatis Generator custom plugins designed to enhance code generation capabilities with MySQL-specific features, DTO layer generation, and automatic Service layer generation.

🚀 Features Overview

This plugin collection includes 6 custom plugins:

Plugin NameDescriptionKey Features
InsertIgnoreIntoPluginMySQL INSERT IGNORE statement supportBatch insert ignoring duplicate records
InsertOnDuplicateKeyPluginMySQL ON DUPLICATE KEY UPDATE supportAuto update on insert conflicts
ReplaceIntoPluginMySQL REPLACE INTO statement supportReplace insert operations
DtoGeneratorPluginDTO layer code generationLombok annotations, Entity conversion methods
ServiceGeneratorPluginService layer code generationInterface + Implementation, Complete CRUD operations
CustomerMapperPluginCustom Mapper generationExtended native Mapper functionality

📦 Dependency Analysis

Core Dependencies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<!-- MyBatis Generator Core Dependency -->
<dependency>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-core</artifactId>
  <version>1.4.2</version>
</dependency>

        <!-- MyBatis Dynamic SQL Support -->
<dependency>
<groupId>org.mybatis.dynamic-sql</groupId>
<artifactId>mybatis-dynamic-sql</artifactId>
<version>1.5.2</version>
</dependency>

        <!-- MyBatis Spring Boot Integration -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>

Maven Plugin Configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17

<plugin>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-maven-plugin</artifactId>
  <version>1.4.2</version>
  <configuration>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>com.goody.utils</groupId>
      <artifactId>mybatis-generator-custome-plugins</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>
</plugin>

🔧 Plugin Details

1. MySQL Extension Plugins

InsertIgnoreIntoPlugin

  • Function: Adds INSERT IGNORE statement support to Mapper
  • Generated Methods: insertIgnoreCustom(), insertIgnoreBatchCustom()
  • Use Cases: Ignore primary key conflicts during batch inserts

InsertOnDuplicateKeyPlugin

  • Function: Adds ON DUPLICATE KEY UPDATE statement support to Mapper
  • Generated Methods: insertOnDuplicateKeyCustom(), insertOnDuplicateKeyBatchCustom()
  • Use Cases: Auto update records on duplicate key conflicts during insert

ReplaceIntoPlugin

  • Function: Adds REPLACE INTO statement support to Mapper
  • Generated Methods: replaceIntoCustom(), replaceIntoBatchCustom()
  • Use Cases: Replace existing records or insert new ones

2. DTO Layer Generation Plugin

DtoGeneratorPlugin

  • Function: Automatically generates DTO classes
  • Features:
    • Lombok annotation support (@Data, @Builder, @AllArgsConstructor, @NoArgsConstructor)
    • Auto-generated fromEntity() and toEntity() conversion methods
    • Package structure: *.model.dto

3. Service Layer Generation Plugin

ServiceGeneratorPlugin

  • Function: Automatically generates Service interfaces and implementation classes
  • Features:
    • Complete CRUD operation methods
    • Support for single and composite primary keys
    • Spring annotation support (@Service, @Autowired)
    • Package structure: *.service.interfaces and *.service.impl

4. Custom Mapper Plugin

CustomerMapperPlugin

  • Function: Generates extended Mapper interfaces
  • Package structure: *.dao.customer

💻 Usage Guide

Step 1: Add Dependencies

Add the plugin to your project:

[Github] 2. MyBatis Generator 自定义插件

[Github] 2. MyBatis Generator 自定义插件

🔗 项目地址: mybatis-generator-custome-plugins

为 MyBatis Generator 设计的强大自定义插件集合,专门针对 MySQL 数据库特性,提供 DTO 层生成、Service 层自动生成等功能。

🚀 功能概览

本插件集合包含以下6个自定义插件:

插件名称功能描述核心特性
InsertIgnoreIntoPluginMySQL INSERT IGNORE语句支持批量插入忽略重复记录
InsertOnDuplicateKeyPluginMySQL ON DUPLICATE KEY UPDATE支持插入冲突时自动更新
ReplaceIntoPluginMySQL REPLACE INTO语句支持替换插入操作
DtoGeneratorPluginDTO层代码生成Lombok注解,Entity转换方法
ServiceGeneratorPluginService层代码生成接口+实现,完整CRUD操作
CustomerMapperPlugin自定义Mapper生成扩展原生Mapper功能

📦 依赖分析

核心依赖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<!-- MyBatis Generator核心依赖 -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.2</version>
</dependency>

<!-- MyBatis Dynamic SQL支持 -->
<dependency>
    <groupId>org.mybatis.dynamic-sql</groupId>
    <artifactId>mybatis-dynamic-sql</artifactId>
    <version>1.5.2</version>
</dependency>

<!-- MyBatis Spring Boot集成 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>

Maven插件配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.4.2</version>
    <configuration>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>com.goody.utils</groupId>
            <artifactId>mybatis-generator-custome-plugins</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</plugin>

🔧 插件详述

1. MySQL扩展插件

InsertIgnoreIntoPlugin

  • 功能: 为Mapper添加INSERT IGNORE语句支持
  • 生成方法: insertIgnoreCustom(), insertIgnoreBatchCustom()
  • 应用场景: 批量插入时忽略主键冲突记录

InsertOnDuplicateKeyPlugin

  • 功能: 为Mapper添加ON DUPLICATE KEY UPDATE语句支持
  • 生成方法: insertOnDuplicateKeyCustom(), insertOnDuplicateKeyBatchCustom()
  • 应用场景: 插入时遇到重复键则更新记录

ReplaceIntoPlugin

  • 功能: 为Mapper添加REPLACE INTO语句支持
  • 生成方法: replaceIntoCustom(), replaceIntoBatchCustom()
  • 应用场景: 存在则替换,不存在则插入

2. DTO层生成插件

DtoGeneratorPlugin

  • 功能: 自动生成DTO类
  • 特性:
    • Lombok注解支持(@Data, @Builder, @AllArgsConstructor, @NoArgsConstructor)
    • 自动生成fromEntity()toEntity()转换方法
    • 包结构: *.model.dto

3. Service层生成插件

ServiceGeneratorPlugin

  • 功能: 自动生成Service接口和实现类
  • 特性:
    • 完整CRUD操作方法
    • 支持单主键和联合主键
    • Spring注解支持(@Service, @Autowired)
    • 包结构: *.service.interfaces*.service.impl

4. 自定义Mapper插件

CustomerMapperPlugin

  • 功能: 生成扩展Mapper接口
  • 包结构: *.dao.customer

💻 使用方法

步骤1: 添加依赖

将插件添加到你的项目中:

[Customs] 1. IntelliJ IDEA Configuration & Recommended Plugins

[Customs] 1. IntelliJ IDEA Configuration & Recommended Plugins

Introduction

As one of the most powerful Java IDEs available, IntelliJ IDEA can be significantly enhanced through proper configuration and carefully selected plugins. This guide presents a curated collection of essential plugins and configuration skills that will transform your development experience.

The goal is to provide ready-to-use configurations and plugins that immediately improve productivity, code quality, and development workflow.

Essential Plugins

🔧 Development Tools

CamelCase (3.0.12)

/images/7.%20customs%20-%20idea/plugin/CamelCase%20(3.0.12).png

[Java] 2. Unit Test 基本用法

[Java] 2. Unit Test 基本用法

Mocikto基本用法

在单元测试里,很多测试(除Util类)都需要mock掉一些服务来保证只测试当前想测的内容.

具体使用时,需要先mock一个对象,然后再mock此对象的方法,然后就可以使用mock的方法去测想测的逻辑了.

Mock对象

首先,需要在Test类里声明需要mock的接口/实现类. 如

1
2
@MockBean
private IOssService ossService;

有时候,也需要直接手动mock一个东西出来,比如,当需要mock掉redis的操作时,可以

1
RSet<Long> redisSet = Mockito.mock(RSet.class);

注意此操作不要去mock基本类型,如int,long等.

还有一种方式是使用@SpyBean. 此处先略过,后面的部分会介绍.

Mock方法

假定有一个接口

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public interface IUserService {

    Long add(UserDTO dto);

    void remove(Long userId);

    Optional<UserDTO> find(String username);

    Optional<UserDTO> find(Long userId);
}

在已经mock掉userService的情况下