|
|
@@ -1,22 +1,22 @@ |
|
|
|
# idgenerator |
|
|
|
锘�# idgenerator |
|
|
|
|
|
|
|
## 调用示例(Rust) |
|
|
|
## 璋冪敤绀轰緥锛圧ust锛� |
|
|
|
|
|
|
|
第1步,**全局** 初始化(应用程序启动时执行一次): |
|
|
|
绗�1姝ワ紝**鍏ㄥ眬** 鍒濆�鍖栵紙搴旂敤绋嬪簭鍚�姩鏃舵墽琛屼竴娆★級锛� |
|
|
|
``` |
|
|
|
// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId: |
|
|
|
// 鍒涘缓 IdGeneratorOptions 瀵硅薄锛岃�鍦ㄦ瀯閫犲嚱鏁颁腑杈撳叆 WorkerId锛� |
|
|
|
let mut options = IdGeneratorOptions::New(1); |
|
|
|
// options.WorkerIdBitLength = 10; // WorkerIdBitLength 默认值6,支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64,可设置更大的 WorkerIdBitLength |
|
|
|
// ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。 |
|
|
|
// options.WorkerIdBitLength = 10; // WorkerIdBitLength 榛樿�鍊�6锛屾敮鎸佺殑 WorkerId 鏈€澶у€间负2^6-1锛岃嫢 WorkerId 瓒呰繃64锛屽彲璁剧疆鏇村ぇ鐨� WorkerIdBitLength |
|
|
|
// ...... 鍏跺畠鍙傛暟璁剧疆鍙傝€� IdGeneratorOptions 瀹氫箟锛屼竴鑸�潵璇达紝鍙��鍐嶈�缃� WorkerIdBitLength 锛堝喅瀹� WorkerId 鐨勬渶澶у€硷級銆� |
|
|
|
|
|
|
|
// 保存参数(必须的操作,否则以上设置都不能生效): |
|
|
|
// 淇濆瓨鍙傛暟锛堝繀椤荤殑鎿嶄綔锛屽惁鍒欎互涓婅�缃�兘涓嶈兘鐢熸晥锛夛細 |
|
|
|
YitIdHelper::SetIdGenerator(options); |
|
|
|
// 以上初始化过程只需全局一次,且必须在第2步之前设置。 |
|
|
|
// 浠ヤ笂鍒濆�鍖栬繃绋嬪彧闇€鍏ㄥ眬涓€娆★紝涓斿繀椤诲湪绗�2姝ヤ箣鍓嶈�缃�€� |
|
|
|
``` |
|
|
|
|
|
|
|
第2步,生成ID: |
|
|
|
绗�2姝ワ紝鐢熸垚ID锛� |
|
|
|
``` |
|
|
|
// 初始化以后,即可在任何需要生成ID的地方,调用以下方法: |
|
|
|
// 鍒濆�鍖栦互鍚庯紝鍗冲彲鍦ㄤ换浣曢渶瑕佺敓鎴怚D鐨勫湴鏂癸紝璋冪敤浠ヤ笅鏂规硶锛� |
|
|
|
long newId = YitIdHelper::NextId(); |
|
|
|
``` |
|
|
|
|
|
|
|