软件环境
-
JDK 1.8
-
SpringBoot 2.2.1
-
Maven 3.2+
-
Mysql 8.0.26
-
redis 6.2.14
-
Mybatis Plus 3.4.3.4
-
开发工具
-
IntelliJ IDEA
-
smartGit
-
一、实现原理
使用Redis来实现分布式的主键自增主要是依赖于Redis
的INCR
命令,调用INCR
命令的对应key,其数值是实现递增加一,所以利用这个性质,将redis独立部署起来就可以实现分布式环境的自增ID,如图,使用INCR
命令的例子
二、实践
本博客写一个基于redis、mybatis-plus的例子,主要介绍一下redis分布式ID的基本使用
首先,快速新建一个Spring Initializr项目,service url
就选择这个https://start.aliyun.com
,spring官网的url
选择jdk的版本,maven类型的项目
选择需要的依赖,选择之后,新生成的项目就会自动加上需要的maven配置,主要选择一下spring data redis
的依赖
mybatis plus是没有搜到的,所以手动加一下
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter-test</artifactId>
<version>3.4.3.4</version>
</dependency>
项目pom.xml文件参考
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-redis</name>
<description>Demo project for Spring Boot</description>
<properties>
<mybatis.plus.version>3.4.3.4</mybatis.plus.version>
<dynamic.datasource.version>3.4.1</dynamic.datasource.version>
<mysql.connector.version>8.0.21</mysql.connector.version>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${dynamic.datasource.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter-test</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件参考
spring:
redis:
host: 127.0.0.1
port: 6379
password:
database: 0
datasource:
dynamic:
primary: shop #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
shop:
url: jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=utf-8
username: root
password:
driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
schema: classpath:db/schema-mysql.sql
data: classpath:db/data-mysql.sql
mybatis-plus:
type-aliases-package: com.example.redis.*.*.model
mapper-locations: classpath*:mapper/*/*.xml
global-config:
db-config:
logic-not-delete-value: 1
logic-delete-value: 0
configuration:
map-underscore-to-camel-case: true
default-statement-timeout: 60
cache-enabled: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
自定义redis主键生成器RedisIdentifierGenerator
package com.example.redis.common.handlers;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* <pre>
* Redis分布式ID生成器
* </pre>
*
* <pre>
* @author nicky
* 修改记录
* 修改后版本: 修改人: 修改日期: 2023/11/07 14:18 修改内容:
* </pre>
*/
@Component
public class RedisIdentifierGenerator implements IdentifierGenerator {
@Resource
private RedisTemplate redisTemplate;
@Override
public Number nextId(Object entity) {
String key = entity.getClass().getName();
return redisTemplate.opsForValue().increment(key);
}
}
加一个baseDO类,type设置为type = IdType.ASSIGN_ID
package com.example.redis.model;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class BaseDO {
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long id;
@TableField(value = "create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(value = "modify_time", fill = FieldFill.INSERT_UPDATE)
private LocalDateTime modifyTime;
@TableLogic(value = "0", delval = "1")
@TableField(value = "is_deleted", select = false)
private Boolean deleted;
}
写一个测试类,运行一下看看:
package com.example.redis.controller;
import cn.hutool.core.bean.BeanUtil;
import com.example.redis.common.rest.ResultBean;
import com.example.redis.model.UserDO;
import com.example.redis.model.dto.UserDto;
import com.example.redis.service.IUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping(value = "/api")
public class UserController {
@Resource
private IUserService userService;
@PostMapping(value = "/user")
public ResultBean<UserDO> save(@RequestBody UserDto userDto) {
UserDO user = BeanUtil.copyProperties(userDto , UserDO.class);
boolean flag = userService.save(user);
if (flag) return ResultBean.ok(user);
return ResultBean.badRequest("新增失败");
}
}
查看控制台日志,可以看到自增的主键生成
存在redis里的主键