国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟

詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟

2021-06-08 14:02王 帥 Java教程

本文就在項(xiàng)目中來(lái)集成 UidGenerator這一工程來(lái)作為項(xiàng)目的全局唯一 ID生成器。接下來(lái)通過(guò)實(shí)例代碼給大家詳解詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟,感興趣的朋友一起看看吧

spring boot中全局唯一流水號(hào)id生成器集成實(shí)驗(yàn)

概述

流水號(hào)生成器(全局唯一 id生成器)是服務(wù)化系統(tǒng)的基礎(chǔ)設(shè)施,其在保障系統(tǒng)的正確運(yùn)行和高可用方面發(fā)揮著重要作用。而關(guān)于流水號(hào)生成算法首屈一指的當(dāng)屬 snowflake 雪花算法,然而 snowflake本身很難在現(xiàn)實(shí)項(xiàng)目中直接使用,因此實(shí)際應(yīng)用時(shí)需要一種可落地的方案。

uidgenerator 由百度開(kāi)發(fā),是java實(shí)現(xiàn)的, 基于 snowflake算法的唯一id生成器。uidgenerator以組件形式工作在應(yīng)用項(xiàng)目中, 支持自定義workerid位數(shù)和初始化策略, 從而適用于 docker等虛擬化環(huán)境下實(shí)例自動(dòng)重啟、漂移等場(chǎng)景。

本文就在項(xiàng)目中來(lái)集成 uidgenerator這一工程來(lái)作為項(xiàng)目的全局唯一 id生成器。

本文內(nèi)容腦圖如下:

詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟

基礎(chǔ)工程創(chuàng)建

只需創(chuàng)建一個(gè) multi-moudule的 maven項(xiàng)目即可,然后我們集成進(jìn)兩個(gè) module:

  • uid-generator :源碼在此
  • uid-consumer :消費(fèi)者( 使用uid-generator產(chǎn)生全局唯一的流水號(hào) )

uid-generator 模塊我就不多說(shuō)了,源碼拿過(guò)來(lái)即可,無(wú)需任何改動(dòng);而關(guān)于 uid-consumer 模塊,先在 pom.xml中添加相關(guān)依賴如下:

?
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
<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.mybatis.spring.boot</groupid>
    <artifactid>mybatis-spring-boot-starter</artifactid>
    <version>1.3.2</version>
  </dependency>
  <!--for mysql-->
  <dependency>
    <groupid>mysql</groupid>
    <artifactid>mysql-connector-java</artifactid>
    <scope>runtime</scope>
    <version>8.0.12</version>
  </dependency>
  <!-- druid -->
  <dependency>
    <groupid>com.alibaba</groupid>
    <artifactid>druid-spring-boot-starter</artifactid>
    <version>1.1.9</version>
  </dependency>
  <!--必須放在最后-->
  <dependency>
    <groupid>cn.codesheep</groupid>
    <artifactid>uid-generator</artifactid>
    <version>1.0</version>
  </dependency>
</dependencies>

然后在 application.properties配置文件中添加一些配置(主要是 mysql和 mybatis配置)

?
1
2
3
4
5
6
7
server.port=9999
spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?useunicode=true&characterencoding=utf-8&usessl=false
spring.datasource.username=root
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

完成之后工程縮影如下圖所示:

詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟

下面我們來(lái)一步步集成 uidgenerator 的源碼。

數(shù)據(jù)庫(kù)建表

首先去 mysql數(shù)據(jù)庫(kù)中建一個(gè)名為 worker_node 的數(shù)據(jù)表,其 sql如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
drop table if exists worker_node;
create table worker_node
(
id bigint not null auto_increment comment 'auto increment id',
host_name varchar(64) not null comment 'host name',
port varchar(64) not null comment 'port',
type int not null comment 'node type: actual or container',
launch_date date not null comment 'launch date',
modified timestamp not null comment 'modified time',
created timestamp not null comment 'created time',
primary key(id)
)
 comment='db workerid assigner for uid generator',engine = innodb;

spring詳細(xì)配置

cacheduidgenerator 配置

uidgenerator 有兩個(gè)具體的實(shí)現(xiàn)類,分別是 defaultuidgenerator 和 cacheduidgenerator ,不過(guò)官方也推薦了對(duì)于性能比較敏感的項(xiàng)目應(yīng)使用后者,因此本文也使用 cacheduidgenerator ,而對(duì)于 defaultuidgenerator 不做過(guò)多闡述。

我們引入 uidgenerator源碼中的 cached-uid-spring.xml 文件,里面都是默認(rèn)配置,我目前沒(méi)有做任何修改

?
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
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
 <!-- uid generator -->
 <bean id="disposableworkeridassigner" class="com.baidu.fsg.uid.worker.disposableworkeridassigner" />
 <bean id="cacheduidgenerator" class="com.baidu.fsg.uid.impl.cacheduidgenerator">
 <property name="workeridassigner" ref="disposableworkeridassigner" />
 <!-- 以下為可選配置, 如未指定將采用默認(rèn)值 -->
 <!-- ringbuffer size擴(kuò)容參數(shù), 可提高uid生成的吞吐量. -->
 <!-- 默認(rèn):3, 原buffersize=8192, 擴(kuò)容后buffersize= 8192 << 3 = 65536 -->
 <!--<property name="boostpower" value="3"></property>-->
 
 <!-- 指定何時(shí)向ringbuffer中填充uid, 取值為百分比(0, 100), 默認(rèn)為50 -->
 <!-- 舉例: buffersize=1024, paddingfactor=50 -> threshold=1024 * 50 / 100 = 512. -->
 <!-- 當(dāng)環(huán)上可用uid數(shù)量 < 512時(shí), 將自動(dòng)對(duì)ringbuffer進(jìn)行填充補(bǔ)全 -->
 <!--<property name="paddingfactor" value="50"></property>-->
 
 <!-- 另外一種ringbuffer填充時(shí)機(jī), 在schedule線程中, 周期性檢查填充 -->
 <!-- 默認(rèn):不配置此項(xiàng), 即不實(shí)用schedule線程. 如需使用, 請(qǐng)指定schedule線程時(shí)間間隔, 單位:秒 -->
 <!--<property name="scheduleinterval" value="60"></property>-->
 
 <!-- 拒絕策略: 當(dāng)環(huán)已滿, 無(wú)法繼續(xù)填充時(shí) -->
 <!-- 默認(rèn)無(wú)需指定, 將丟棄put操作, 僅日志記錄. 如有特殊需求, 請(qǐng)實(shí)現(xiàn)rejectedputbufferhandler接口(支持lambda表達(dá)式) -->
 <!--<property name="rejectedputbufferhandler" ref="xxxxyourputrejectpolicy"></property>-->
 
 <!-- 拒絕策略: 當(dāng)環(huán)已空, 無(wú)法繼續(xù)獲取時(shí) -->
 <!-- 默認(rèn)無(wú)需指定, 將記錄日志, 并拋出uidgenerateexception異常. 如有特殊需求, 請(qǐng)實(shí)現(xiàn)rejectedtakebufferhandler接口(支持lambda表達(dá)式) -->
 <!--<property name="rejectedputbufferhandler" ref="xxxxyourputrejectpolicy"></property>-->
 
 </bean>
</beans>

mybatis mapper xml 配置

即原樣引入 uidgenerator源碼中關(guān)于工作節(jié)點(diǎn)(worker node)操作的 mapper xml 文件: worker_node.xml ,其內(nèi)容如下:

?
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
<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.baidu.fsg.uid.worker.dao.workernodedao">
 <resultmap id="workernoderes"
   type="com.baidu.fsg.uid.worker.entity.workernodeentity">
 <id column="id" jdbctype="bigint" property="id" />
 <result column="host_name" jdbctype="varchar" property="hostname" />
 <result column="port" jdbctype="varchar" property="port" />
 <result column="type" jdbctype="integer" property="type" />
 <result column="launch_date" jdbctype="date" property="launchdate" />
 <result column="modified" jdbctype="timestamp" property="modified" />
 <result column="created" jdbctype="timestamp" property="created" />
 </resultmap>
 <insert id="addworkernode" usegeneratedkeys="true" keyproperty="id"
 parametertype="com.baidu.fsg.uid.worker.entity.workernodeentity">
 insert into worker_node
 (host_name,
 port,
 type,
 launch_date,
 modified,
 created)
 values (
 #{hostname},
 #{port},
 #{type},
 #{launchdate},
 now(),
 now())
 </insert>
 <select id="getworkernodebyhostport" resultmap="workernoderes">
 select
 id,
 host_name,
 port,
 type,
 launch_date,
 modified,
 created
 from
 worker_node
 where
 host_name = #{host} and port = #{port}
 </select>
</mapper>

編寫(xiě)業(yè)務(wù)代碼

config 類創(chuàng)建與配置

新建 uidconfig 類,為我們引入上文的 cached-uid-spring.xml 配置

?
1
2
3
4
@configuration
@importresource(locations = { "classpath:uid/cached-uid-spring.xml" })
public class uidconfig {
}

service 類創(chuàng)建與配置

新建 uidgenservice ,引入 uidgenerator 生成 uid的業(yè)務(wù)接口

?
1
2
3
4
5
6
7
8
9
10
@service
public class uidgenservice {
 
  @resource
  private uidgenerator uidgenerator;
 
  public long getuid() {
    return uidgenerator.getuid();
  }
}

controller 創(chuàng)建與配置

新建 uidtestcontroller ,目的是方便我們用瀏覽器測(cè)試接口并觀察效果:

?
1
2
3
4
5
6
7
8
9
10
11
@restcontroller
public class uidtestcontroller {
 
  @autowired
  private uidgenservice uidgenservice;
 
  @getmapping("/testuid")
  public string test() {
    return string.valueof( uidgenservice.getuid() );
  }
}

實(shí)驗(yàn)測(cè)試

我們每啟動(dòng)一次 spring boot工程,其即會(huì)自動(dòng)去 mysql數(shù)據(jù)的 worker_node 表中插入一行關(guān)于工作節(jié)點(diǎn)的記錄,類似下圖所示:

詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟

接下來(lái)我們?yōu)g覽器訪問(wèn):http://localhost:9999/testuid

詳解Spring Boot工程集成全局唯一ID生成器 UidGenerator的操作步驟

ok,全局唯一流水號(hào)id已經(jīng)成功生成并返回!

總結(jié)

以上所述是小編給大家介紹的spring boot工程集成全局唯一id生成器 uidgenerator,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.codesheep.cn/2018/10/24/springbt-uid-generator/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91久久国产 | 91在线免费网站 | 亚洲精品永久视频 | 国产高清精品一区 | 理伦影院 | 精品久久97| 国产一级免费 | 中文日韩在线 | 久草在线视频网 | 久久中文免费 | 久久久久久亚洲精品视频 | 一区二区三区视频 | 在线观看a视频 | 婷婷激情五月 | av网站免费线看 | 成人精品国产免费网站 | 日韩av一区二区在线观看 | 成人欧美一区二区三区在线播放 | 在线a视频| 久久精品电影 | 国产亚洲精品美女久久久久久久久久 | 91色视频在线观看 | 亚洲国内精品 | 黄色av网站免费看 | 日本在线免费视频 | 在线观看国产中文字幕 | 久久久免费少妇高潮毛片 | 香蕉成人啪国产精品视频综合网 | 成人av观看| 免费av在线播放 | 中文久久精品 | 国产精品一区二区三区四区五区 | 国产一区二区在线免费观看 | 九九九色 | 视频网站免费观看 | 亚洲国产中文字幕 | 久久精品一区二区三区不卡牛牛 | 欧美一区二区三区免费 | 毛片在线一区二区观看精品 | 免费的污网站 | 成年人在线免费观看视频网站 |