当前位置: 首页 > news >正文

wordpress主题如何更换/网站如何优化流程

wordpress主题如何更换,网站如何优化流程,金堂企业网站建设,东莞横沥网站制作引言接着《一口气说出 9种 分布式ID生成方式,面试官有点懵了》来继续详细的介绍分布式ID生成器,大家比较感兴趣的美团(Leaf)、滴滴(Tinyid)、百度(uid-generator)三个开源项目&#…

0e2134d94a8938a69c726725439e721e.png

引言

接着《一口气说出 9种 分布式ID生成方式,面试官有点懵了》来继续详细的介绍分布式ID生成器,大家比较感兴趣的美团(Leaf)滴滴(Tinyid)百度(uid-generator)三个开源项目,美团(Leaf)已经讲完,详见《9种分布式ID生成之美团(Leaf)实战》,今天结合实战搞一下滴滴开源的(Tinyid)。


`Tinyid`介绍

Tinyid是滴滴开发的一款分布式ID系统,Tinyid是在美团(Leaf)leaf-segment算法基础上升级而来,不仅支持了数据库多主节点模式,还提供了tinyid-client客户端的接入方式,使用起来更加方便。但和美团(Leaf)不同的是,Tinyid只支持号段一种模式不支持雪花模式。

Tinyid的特性

  • 全局唯一的long型ID
  • 趋势递增的id
  • 提供 http 和 java-client 方式接入
  • 支持批量获取ID
  • 支持生成1,3,5,7,9…序列的ID
  • 支持多个db的配置

适用场景:只关心ID是数字,趋势递增的系统,可以容忍ID不连续,可以容忍ID的浪费

不适用场景:像类似于订单ID的业务,因生成的ID大部分是连续的,容易被扫库、或者推算出订单量等信息


`Tinyid`原理

Tinyid是基于号段模式实现,再简单啰嗦一下号段模式的原理:就是从数据库批量的获取自增ID,每次从数据库取出一个号段范围,例如 (1,1000] 代表1000个ID,业务服务将号段在本地生成1~1000的自增ID并加载到内存.。

Tinyid会将可用号段加载到内存中,并在内存中生成ID,可用号段在首次获取ID时加载,如当前号段使用达到一定比例时,系统会异步的去加载下一个可用号段,以此保证内存中始终有可用号段,以便在发号服务宕机后一段时间内还有可用ID。

原理图大致如下图:

694e016f5326962b3ecbfe5b420a25da.png

`Tinyid`实现

Tinyid的GitHub地址 : https://github.com/didi/tinyid.git

Tinyid提供了两种调用方式,一种基于Tinyid-server提供的http方式,另一种Tinyid-client客户端方式。
不管使用哪种方式调用,搭建Tinyid都必须提前建表tiny_id_infotiny_id_token

 1CREATE TABLE `tiny_id_info` (2  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',3  `biz_type` varchar(63) NOT NULL DEFAULT '' COMMENT '业务类型,唯一',4  `begin_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '开始id,仅记录初始值,无其他含义。初始化时begin_id和max_id应相同',5  `max_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '当前最大id',6  `step` int(11) DEFAULT '0' COMMENT '步长',7  `delta` int(11) NOT NULL DEFAULT '1' COMMENT '每次id增量',8  `remainder` int(11) NOT NULL DEFAULT '0' COMMENT '余数',9  `create_time` timestamp NOT NULL DEFAULT '2010-01-01 00:00:00' COMMENT '创建时间',
10  `update_time` timestamp NOT NULL DEFAULT '2010-01-01 00:00:00' COMMENT '更新时间',
11  `version` bigint(20) NOT NULL DEFAULT '0' COMMENT '版本号',
12  PRIMARY KEY (`id`),
13  UNIQUE KEY `uniq_biz_type` (`biz_type`)
14) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT 'id信息表';
15
16CREATE TABLE `tiny_id_token` (
17  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
18  `token` varchar(255) NOT NULL DEFAULT '' COMMENT 'token',
19  `biz_type` varchar(63) NOT NULL DEFAULT '' COMMENT '此token可访问的业务类型标识',
20  `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注',
21  `create_time` timestamp NOT NULL DEFAULT '2010-01-01 00:00:00' COMMENT '创建时间',
22  `update_time` timestamp NOT NULL DEFAULT '2010-01-01 00:00:00' COMMENT '更新时间',
23  PRIMARY KEY (`id`)
24) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT 'token信息表';
25
26INSERT INTO `tiny_id_info` (`id`, `biz_type`, `begin_id`, `max_id`, `step`, `delta`, `remainder`, `create_time`, `update_time`, `version`)
27VALUES
28    (1, 'test', 1, 1, 100000, 1, 0, '2018-07-21 23:52:58', '2018-07-22 23:19:27', 1);
29
30INSERT INTO `tiny_id_info` (`id`, `biz_type`, `begin_id`, `max_id`, `step`, `delta`, `remainder`, `create_time`, `update_time`, `version`)
31VALUES
32    (2, 'test_odd', 1, 1, 100000, 2, 1, '2018-07-21 23:52:58', '2018-07-23 00:39:24', 3);
33
34
35INSERT INTO `tiny_id_token` (`id`, `token`, `biz_type`, `remark`, `create_time`, `update_time`)
36VALUES
37    (1, '0f673adf80504e2eaa552f5d791b644c', 'test', '1', '2017-12-14 16:36:46', '2017-12-14 16:36:48');
38
39INSERT INTO `tiny_id_token` (`id`, `token`, `biz_type`, `remark`, `create_time`, `update_time`)
40VALUES
41    (2, '0f673adf80504e2eaa552f5d791b644c', 'test_odd', '1', '2017-12-14 16:36:46', '2017-12-14 16:36:48');

tiny_id_info表是具体业务方号段信息数据表

ededdc411e9438c7ab67573af262c183.png

max_id:号段的最大值

step:步长,即为号段的长度

biz_type:业务类型

号段获取对max_id字段做一次update操作,update max_id= max_id + step,更新成功则说明新号段获取成功,新的号段范围是(max_id ,max_id +step]

tiny_id_token是一个权限表,表示当前token可以操作哪些业务的号段信息。

097fac2728c040794b7ac98dd5f8ec09.png

修改tinyid-serverofflineapplication.properties 文件配置数据库,由于tinyid支持数据库多master模式,可以配置多个数据库信息。启动 TinyIdServerApplication 测试一下。

 1datasource.tinyid.primary.driver-class-name=com.mysql.jdbc.Driver2datasource.tinyid.primary.url=jdbc:mysql://127.0.0.1:3306/xin-master?autoReconnect=true&useUnicode=true&characterEncoding=UTF-83datasource.tinyid.primary.username=junkang4datasource.tinyid.primary.password=junkang5datasource.tinyid.primary.testOnBorrow=false6datasource.tinyid.primary.maxActive=1078datasource.tinyid.secondary.driver-class-name=com.mysql.jdbc.Driver9datasource.tinyid.secondary.url=jdbc:mysql://localhost:3306/db2?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
10datasource.tinyid.secondary.username=root
11datasource.tinyid.secondary.password=123456
12datasource.tinyid.secondary.testOnBorrow=false
13datasource.tinyid.secondary.maxActive=10

1、Http方式

tinyid内部一共提供了四个http接口来获取ID和号段。

 1package com.xiaoju.uemc.tinyid.server.controller;23/**4 * @author du_imba5 */6@RestController7@RequestMapping("/id/")8public class IdContronller {9
10    private static final Logger logger = LoggerFactory.getLogger(IdContronller.class);
11    @Autowired
12    private IdGeneratorFactoryServer idGeneratorFactoryServer;
13    @Autowired
14    private SegmentIdService segmentIdService;
15    @Autowired
16    private TinyIdTokenService tinyIdTokenService;
17    @Value("${batch.size.max}")
18    private Integer batchSizeMax;
19
20    @RequestMapping("nextId")
21    public Response<List<Long>> nextId(String bizType, Integer batchSize, String token) {
22        Response<List<Long>> response = new Response<>();
23        try {
24            IdGenerator idGenerator = idGeneratorFactoryServer.getIdGenerator(bizType);
25            List<Long> ids = idGenerator.nextId(newBatchSize);
26            response.setData(ids);
27        } catch (Exception e) {
28            response.setCode(ErrorCode.SYS_ERR.getCode());
29            response.setMessage(e.getMessage());
30            logger.error("nextId error", e);
31        }
32        return response;
33    }
34
35
36
37    @RequestMapping("nextIdSimple")
38    public String nextIdSimple(String bizType, Integer batchSize, String token) {
39        String response = "";
40        try {
41            IdGenerator idGenerator = idGeneratorFactoryServer.getIdGenerator(bizType);
42            if (newBatchSize == 1) {
43                Long id = idGenerator.nextId();
44                response = id + "";
45            } else {
46                List<Long> idList = idGenerator.nextId(newBatchSize);
47                StringBuilder sb = new StringBuilder();
48                for (Long id : idList) {
49                    sb.append(id).append(",");
50                }
51                response = sb.deleteCharAt(sb.length() - 1).toString();
52            }
53        } catch (Exception e) {
54            logger.error("nextIdSimple error", e);
55        }
56        return response;
57    }
58
59    @RequestMapping("nextSegmentId")
60    public Response<SegmentId> nextSegmentId(String bizType, String token) {
61        try {
62            SegmentId segmentId = segmentIdService.getNextSegmentId(bizType);
63            response.setData(segmentId);
64        } catch (Exception e) {
65            response.setCode(ErrorCode.SYS_ERR.getCode());
66            response.setMessage(e.getMessage());
67            logger.error("nextSegmentId error", e);
68        }
69        return response;
70    }
71
72    @RequestMapping("nextSegmentIdSimple")
73    public String nextSegmentIdSimple(String bizType, String token) {
74        String response = "";
75        try {
76            SegmentId segmentId = segmentIdService.getNextSegmentId(bizType);
77            response = segmentId.getCurrentId() + "," + segmentId.getLoadingId() + "," + segmentId.getMaxId()
78                    + "," + segmentId.getDelta() + "," + segmentId.getRemainder();
79        } catch (Exception e) {
80            logger.error("nextSegmentIdSimple error", e);
81        }
82        return response;
83    }
84
85}

nextIdnextIdSimple都是获取下一个ID,nextSegmentIdSimplegetNextSegmentId是获取下一个可用号段。区别在于接口是否有返回状态。

 1nextId:2'http://localhost:9999/tinyid/id/nextId?bizType=test&token=0f673adf80504e2eaa552f5d791b644c'3response :4{5    "data": [2],6    "code": 200,7    "message": ""8}9
10nextId Simple:
11'http://localhost:9999/tinyid/id/nextIdSimple?bizType=test&token=0f673adf80504e2eaa552f5d791b644c'
12response: 3

549670891b7955c77085202051b242de.png

056c8d31acc6a4194a1cd71fa42cfaa0.png

2、`Tinyid-client`客户端

如果不想通过http方式,Tinyid-client客户端也是一种不错的选择。

引用 tinyid-server

1<dependency>
2    <groupId>com.xiaoju.uemc.tinyid</groupId>
3    <artifactId>tinyid-client</artifactId>
4    <version>${tinyid.version}</version>
5</dependency>

启动 tinyid-server项目打包后得到 tinyid-server-0.1.0-SNAPSHOT.jar ,设置版本 ${tinyid.version}为0.1.0-SNAPSHOT。

在我们的项目 application.properties 中配置 tinyid-server服务的请求地址 和 用户身份token

1tinyid.server=127.0.0.1:9999
2tinyid.token=0f673adf80504e2eaa552f5d791b644c```
3

在Java代码调用TinyId也很简单,只需要一行代码。

1  // 根据业务类型 获取单个ID
2  Long id = TinyId.nextId("test");
3
4  // 根据业务类型 批量获取10个ID
5  List<Long> ids = TinyId.nextId("test", 10);    

Tinyid整个项目的源码实现也是比较简单,像与数据库交互更直接用jdbcTemplate实现

 1@Override2    public TinyIdInfo queryByBizType(String bizType) {3        String sql = "select id, biz_type, begin_id, max_id," +4                " step, delta, remainder, create_time, update_time, version" +5                " from tiny_id_info where biz_type = ?";6        List<TinyIdInfo> list = jdbcTemplate.query(sql, new Object[]{bizType}, new TinyIdInfoRowMapper());7        if(list == null || list.isEmpty()) {8            return null;9        }
10        return list.get(0);
11    }

总结

两种方式推荐使用Tinyid-client,这种方式ID为本地生成,号段长度(step)越长,支持的qps就越大,如果将号段设置足够大,则qps可达1000w+。而且tinyid-clienttinyid-server 访问变的低频,减轻了server端的压力。

http://www.jmfq.cn/news/5017123.html

相关文章:

  • 如何做卖衣服的网站/电商关键词工具
  • 小型电商网站模板/app宣传推广方案
  • 邢台做移动网站公司/百度收录批量查询工具
  • 建站系统下载 discuz/热点事件
  • 做网赚类网站违法吗/温州百度推广公司电话
  • 网站建设方案书写/小广告图片
  • 家电网站建设需求分析/免费的网站软件下载
  • 正规网站建设学习网公司哪家好/电脑速成班短期电脑培训班
  • o2o网站建站/互联网舆情监控系统
  • 北京如何做网站/河源今日头条新闻最新
  • 番禺做网站的/怎么百度推广
  • 建设部网站注册中心/广告商对接平台
  • wordpress页面编辑/seo海外
  • 网站的客服怎么做/优秀的网络搜索引擎营销案例
  • 给个网站你知道/网站制作的服务怎么样
  • 海北公司网站建设价格低/足球世界排名一览表
  • ps做网站尺寸/网络舆情信息
  • 邢台企业做网站哪家好/数字营销包括哪六种方式
  • 杭州做卖房子的工作哪个网站好/免费的网络推广平台
  • 东莞网站建设运营方案/seo就是搜索引擎广告
  • 建设一个旅游网站的目的是什么/营销平台建设
  • 做网站要学那些东西/淘宝网官方网站
  • 网站包括什么/站长工具查询域名
  • 成都app开发多少钱/惠州百度seo哪家好
  • 嘉祥网站建设/济南今日头条新闻
  • 临汾花果街网站建设/佛山网站建设技术托管
  • 佛山网站建设no.1/福州短视频seo获客
  • 网上商城网站建设规划/下载安装百度
  • 设计师素材/百度推广怎么优化关键词的质量
  • 信阳网站建设公司汉狮排名/今天大事件新闻