经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
Redis生成分布式系统全局唯一ID的实现
来源:jb51  时间:2021/10/25 11:25:59  对本文有异议

分布式系统全局唯一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

Maven Dependency

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.8.RELEASE</version>
  9. <relativePath/>
  10. </parent>
  11.  
  12. <modelVersion>4.0.0</modelVersion>
  13.  
  14. <groupId>org.fool.redis</groupId>
  15. <artifactId>redis-string-id</artifactId>
  16. <version>1.0-SNAPSHOT</version>
  17.  
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23.  
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29.  
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-data-redis</artifactId>
  33. </dependency>
  34.  
  35. <dependency>
  36. <groupId>org.projectlombok</groupId>
  37. <artifactId>lombok</artifactId>
  38. <version>1.18.12</version>
  39. </dependency>
  40. </dependencies>
  41.  
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

application.properties

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

SRC

Application.java

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

Product.java

  1. package org.fool.redis.model;
  2.  
  3. import lombok.Data;
  4.  
  5. import java.math.BigDecimal;
  6.  
  7. @Data
  8. public class Product {
  9. private Long id;
  10. private String name;
  11. private BigDecimal price;
  12. private String detail;
  13. }

IdGeneratorService.java

  1. package org.fool.redis.service;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.redis.core.StringRedisTemplate;
  5. import org.springframework.stereotype.Service;
  6.  
  7. @Service
  8. public class IdGeneratorService {
  9. @Autowired
  10. private StringRedisTemplate stringRedisTemplate;
  11.  
  12. private static final String ID_KEY = "id:generator:product";
  13.  
  14. public Long incrementId() {
  15. return stringRedisTemplate.opsForValue().increment(ID_KEY);
  16. }
  17. }

ProductController.java

  1. package org.fool.redis.controller;
  2.  
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.fool.redis.model.Product;
  5. import org.fool.redis.service.IdGeneratorService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11.  
  12. @RestController
  13. @Slf4j
  14. @RequestMapping(value = "/product")
  15. public class ProductController {
  16. @Autowired
  17. private IdGeneratorService idGeneratorService;
  18.  
  19. @PostMapping(value = "/create")
  20. public String create(@RequestBody Product obj) {
  21. //生成分布式id
  22. long id = idGeneratorService.incrementId();
  23.  
  24. //使用全局id 代替数据库的自增id
  25. obj.setId(id);
  26.  
  27. //取模(e.g: 这里分为8张表,海量数据可以分为1024张表),计算表名
  28. int table = (int) id % 8;
  29. String tableName = "product_" + table;
  30.  
  31. log.info("insert to table: {}, with content: {}", tableName, obj);
  32.  
  33. return "insert to table: " + tableName + " with content: " + obj;
  34. }
  35. }

Test

  1. curl --location --request POST 'http://localhost:8888/product/create' --header 'Content-Type: application/json' --data-raw '{
  2. "name": "Car",
  3. "price": "300000.00",
  4. "detail": "Lexus Style"
  5. }'

Console Output

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号