<ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>


      目錄

      * 簡介 <https://www.cnblogs.com/coldairarrow/p/11509881.html#簡介>
      * 產(chǎn)生背景 <https://www.cnblogs.com/coldairarrow/p/11509881.html#產(chǎn)生背景>
      * 使用方式 <https://www.cnblogs.com/coldairarrow/p/11509881.html#使用方式>
      * 原始版 <https://www.cnblogs.com/coldairarrow/p/11509881.html#原始版>
      * 完美版 <https://www.cnblogs.com/coldairarrow/p/11509881.html#完美版>
      * 測試 <https://www.cnblogs.com/coldairarrow/p/11509881.html#測試>
      * 結(jié)尾 <https://www.cnblogs.com/coldairarrow/p/11509881.html#結(jié)尾>
      簡介

      IdHelper是一個(gè).NET(支持.NET45+或.NET Standard2+)生成分布式趨勢自增Id組件,有兩個(gè)版本:原始版
      為基于雪花Id(不了解請自行百度)方案,需要手動(dòng)管理設(shè)置WorkerId;完美版
      在原始版的基礎(chǔ)上使用Zookeeper來解決原始版中的WorkerId的分配問題和時(shí)間回?fù)軉栴}。

      原始版安裝方式:Nuget安裝IdHelper即可

      完美版安裝方式:Nuget安裝IdHelper.Zookeeper即可

      請按需選擇,強(qiáng)烈推薦完美版

      項(xiàng)目地址:https://github.com/Coldairarrow/IdHelper
      <https://github.com/Coldairarrow/IdHelper>

      產(chǎn)生背景


      分布式趨勢自增Id的生成方案比較多,其中雪花Id是比較常用的,但是雪花Id及其依賴WorkerId的分配和機(jī)器時(shí)鐘。WorkerId分配問題:傳統(tǒng)雪花Id是需要分配數(shù)據(jù)中心Id和機(jī)器Id(即WorkerId),我為了使用方便(項(xiàng)目比較小),用不到數(shù)據(jù)中心Id,就把數(shù)據(jù)中心Id去掉并補(bǔ)充到機(jī)器Id,使機(jī)器Id可分配范圍為1~1023,每個(gè)服務(wù)機(jī)器Id不能重復(fù),若手工去為每個(gè)服務(wù)設(shè)置無疑十分麻煩還容易搞錯(cuò)(其實(shí)是
      懶)。時(shí)鐘回?fù)軉栴}:由于強(qiáng)依賴機(jī)器時(shí)鐘,因此當(dāng)時(shí)間回?fù)軙r(shí)將發(fā)生災(zāi)難性問題,雖然這種概率很小,但是實(shí)際存在。為了解決上述兩個(gè)問題,本組件應(yīng)運(yùn)而生。

      使用方式

      原始版

      Nuget安裝包:IdHelper

      剛出爐的包,排名比較靠后,請認(rèn)準(zhǔn)作者:Coldairarrow
      using Coldairarrow.Util; using System; namespace Demo { class Program { static
      void Main(string[] args) { new IdHelperBootstrapper() //設(shè)置WorkerId
      .SetWorkderId(1) .Boot();
      Console.WriteLine($"WorkerId:{IdHelper.WorkerId},Id:{IdHelper.GetId()}");
      Console.ReadLine(); } } }
      完美版

      1:安裝并配置JAVA環(huán)境(Zookeeper需要用JAVA) 教程:連接
      <https://blog.csdn.net/qq_42040731/article/details/82598034>

      2:安裝并啟動(dòng)Zookeeper,教程:鏈接 <https://blog.csdn.net/ring300/article/details/80446918>

      3:Nuget安裝包:IdHelper.Zookeeper
      using Coldairarrow.Util; using System; namespace Demo.Zookeeper { class
      Program { static void Main(string[] args) { new IdHelperBootstrapper()
      //使用Zookeeper自動(dòng)分配管理WorkerId,解決時(shí)間回退問題和自動(dòng)分配問題 .UseZookeeper("127.0.0.1:2181",
      200, "Test") .Boot();
      Console.WriteLine($"WorkerId:{IdHelper.WorkerId},Id:{IdHelper.GetId()}");
      Console.ReadLine(); } } }
      測試
      using Coldairarrow.Util; using System; using System.Collections.Concurrent;
      using System.Collections.Generic; using System.Diagnostics; using System.Linq;
      using System.Threading.Tasks; namespace Demo.Test { class Program { static void
      Main(string[] args) { string conString = "127.0.0.1:2181"; new
      IdHelperBootstrapper() .UseZookeeper(conString, 200, "Test") .Boot();
      Console.WriteLine($"WorkerId:{IdHelper.WorkerId}"); Stopwatch watch = new
      Stopwatch(); watch.Start(); List<Task> tasks = new List<Task>();
      BlockingCollection<string> ids = new BlockingCollection<string>(); for (int i =
      0; i < 4; i++) { tasks.Add(Task.Run(() => { for (int j = 0; j < 1000000; j++) {
      ids.Add(IdHelper.GetId()); } })); } Task.WaitAll(tasks.ToArray());
      watch.Stop();
      Console.WriteLine($"耗時(shí):{watch.ElapsedMilliseconds}ms,是否有重復(fù):{ids.Count !=
      ids.Distinct().Count()}"); } } }
      結(jié)尾

      以上所有示例在源碼中都有,若覺得不錯(cuò)請點(diǎn)贊加星星,希望能夠幫助到大家。

      有任何問題請及時(shí)反饋或加群交流

      QQ群1:(已滿)

      QQ群2:579202910

      友情鏈接
      ioDraw流程圖
      API參考文檔
      OK工具箱
      云服務(wù)器優(yōu)惠
      阿里云優(yōu)惠券
      騰訊云優(yōu)惠券
      京東云優(yōu)惠券
      站點(diǎn)信息
      問題反饋
      郵箱:[email protected]
      QQ群:637538335
      關(guān)注微信

        <ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>
          亲子乱婬一级A片 | 国产无套 | youjizzxxxx18 | 蜜臀AV国产精品又黃又粗又大 | 99热草| 啊灬好深啊灬轻一点灬app | 丁香婷婷激情小说 | 免费无遮挡男女交性视频 | 男女吃奶做爰猛烈紧视频 | 久久精品国产亚洲沈樵 |