服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Redis - Redis生成分布式系统全局唯一ID的实现

Redis生成分布式系统全局唯一ID的实现

2021-11-21 22:32李白与酒 Redis

在互联网系统中,并发越大的系统,数据就越大,数据越大就越需要分布式,本文主要介绍了Redis生成分布式系统全局唯一ID的实现,感兴趣的可以了解一下

分布式系统全局唯一id

在互联网系统中,并发越大的系统,数据就越大,数据越大就越需要分布式,而大量的分布式数据就越需要唯一标识来识别它们。

例如淘宝的商品系统有千亿级别商品,订单系统有万亿级别的订单数据,这些数据都是日渐增长,传统的单库单表是无法支撑这种级别的数据,必须对其进行分库分表;一旦分库分表,表的自增id就失去了意义;故需要一个全局唯一的id来标识每一条数据(商品、订单)。

e.g: 一张表1亿条数据,被分库分表10张表,原先的id就失去意义,所以需要全局唯一id来标识10张表的数据。

全局唯一的id生成的技术方案有很多,业界比较有名的有 uuid、redis、twitter的snowflake算法、美团leaf算法。

基于redis incr 命令生成分布式全局唯一id

incr 命令主要有以下2个特征:

  • redis的incr命令具备了“incr and get”的原子操作
  • redis是单进程单线程架构,incr命令不会出现id重复

基于以上2个特性,可以采用incr命令来实现分布式全局id生成。

采用redis生成商品全局唯一id

project directory

Redis生成分布式系统全局唯一ID的实现

maven dependency

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="utf-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.2.8.release</version>
        <relativepath/>
    </parent>
 
    <modelversion>4.0.0</modelversion>
 
    <groupid>org.fool.redis</groupid>
    <artifactid>redis-string-id</artifactid>
    <version>1.0-snapshot</version>
 
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
 
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
 
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-data-redis</artifactid>
        </dependency>
 
        <dependency>
            <groupid>org.projectlombok</groupid>
            <artifactid>lombok</artifactid>
            <version>1.18.12</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring.application.name=redis-spring-id
server.port=8888
 
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=
spring.redis.timeout=2000
spring.redis.pool.max-active=10
spring.redis.pool.max-wait=1000
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.num-tests-per-eviction-run=1024
spring.redis.pool.time-between-eviction-runs-millis=30000
spring.redis.pool.min-evictable-idle-time-millis=60000
spring.redis.pool.soft-min-evictable-idle-time-millis=10000
spring.redis.pool.test-on-borrow=true
spring.redis.pool.test-while-idle=true
spring.redis.pool.block-when-exhausted=false

src

application.java

?
1
2
3
4
5
6
7
8
9
10
11
package org.fool.redis;
 
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
 
@springbootapplication
public class application {
    public static void main(string[] args) {
        springapplication.run(application.class, args);
    }
}

product.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package org.fool.redis.model;
 
import lombok.data;
 
import java.math.bigdecimal;
 
@data
public class product {
    private long id;
    private string name;
    private bigdecimal price;
    private string detail;
}

idgeneratorservice.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.fool.redis.service;
 
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.stringredistemplate;
import org.springframework.stereotype.service;
 
@service
public class idgeneratorservice {
    @autowired
    private stringredistemplate stringredistemplate;
 
    private static final string id_key = "id:generator:product";
 
    public long incrementid() {
        return stringredistemplate.opsforvalue().increment(id_key);
    }
}

productcontroller.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package org.fool.redis.controller;
 
import lombok.extern.slf4j.slf4j;
import org.fool.redis.model.product;
import org.fool.redis.service.idgeneratorservice;
import org.springframework.beans.factory.annotation.autowired;
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;
 
@restcontroller
@slf4j
@requestmapping(value = "/product")
public class productcontroller {
    @autowired
    private idgeneratorservice idgeneratorservice;
 
    @postmapping(value = "/create")
    public string create(@requestbody product obj) {
        //生成分布式id
        long id = idgeneratorservice.incrementid();
 
        //使用全局id 代替数据库的自增id
        obj.setid(id);
 
        //取模(e.g: 这里分为8张表,海量数据可以分为1024张表),计算表名
        int table = (int) id % 8;
        string tablename = "product_" + table;
 
        log.info("insert to table: {}, with content: {}", tablename, obj);
 
        return "insert to table: " + tablename + " with content: " + obj;
    }
}

test

?
1
2
3
4
5
6
7
curl --location --request post 'http://localhost:8888/product/create' \
--header 'content-type: application/json' \
--data-raw '{
    "name": "car",
    "price": "300000.00",
    "detail": "lexus style"
}'

console output

Redis生成分布式系统全局唯一ID的实现

到此这篇关于redis生成分布式系统全局唯一id的实现的文章就介绍到这了,更多相关redis生成分布式系统全局唯一id内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/agilestyle/p/13194027.html

延伸 · 阅读

精彩推荐
  • Redis详解Redis复制原理

    详解Redis复制原理

    与大多数db一样,Redis也提供了复制机制,以满足故障恢复和负载均衡等需求。复制也是Redis高可用的基础,哨兵和集群都是建立在复制基础上实现高可用的...

    李留广10222021-08-09
  • Redisredis实现排行榜功能

    redis实现排行榜功能

    排行榜在很多地方都能使用到,redis的zset可以很方便地用来实现排行榜功能,本文就来简单的介绍一下如何使用,具有一定的参考价值,感兴趣的小伙伴们...

    乘月归5022021-08-05
  • Redisredis 交集、并集、差集的具体使用

    redis 交集、并集、差集的具体使用

    这篇文章主要介绍了redis 交集、并集、差集的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    xiaojin21cen10152021-07-27
  • RedisRedis的配置、启动、操作和关闭方法

    Redis的配置、启动、操作和关闭方法

    今天小编就为大家分享一篇Redis的配置、启动、操作和关闭方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    大道化简5312019-11-14
  • RedisRedis全量复制与部分复制示例详解

    Redis全量复制与部分复制示例详解

    这篇文章主要给大家介绍了关于Redis全量复制与部分复制的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Redis爬虫具有一定的参考学习...

    豆子先生5052019-11-27
  • RedisRedis如何实现数据库读写分离详解

    Redis如何实现数据库读写分离详解

    Redis的主从架构,能帮助我们实现读多,写少的情况,下面这篇文章主要给大家介绍了关于Redis如何实现数据库读写分离的相关资料,文中通过示例代码介绍...

    罗兵漂流记6092019-11-11
  • RedisRedis 事务知识点相关总结

    Redis 事务知识点相关总结

    这篇文章主要介绍了Redis 事务相关总结,帮助大家更好的理解和学习使用Redis,感兴趣的朋友可以了解下...

    AsiaYe8232021-07-28
  • Redisredis中如何使用lua脚本让你的灵活性提高5个逼格详解

    redis中如何使用lua脚本让你的灵活性提高5个逼格详解

    这篇文章主要给大家介绍了关于redis中如何使用lua脚本让你的灵活性提高5个逼格的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具...

    一线码农5812019-11-18