自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于Redis的分布式鎖和Redlock算法

存儲 存儲軟件 分布式 算法 Redis
在單進程的系統(tǒng)中,當存在多個線程可以同時改變某個變量(可變共享變量)時,就需要對變量或代碼塊做同步,使其在修改這種變量時能夠線性執(zhí)行消除并發(fā)修改變量。

[[403381]]

 本文轉載自微信公眾號「UP技術控」,作者conan5566。轉載本文請聯(lián)系UP技術控公眾號。

在單進程的系統(tǒng)中,當存在多個線程可以同時改變某個變量(可變共享變量)時,就需要對變量或代碼塊做同步,使其在修改這種變量時能夠線性執(zhí)行消除并發(fā)修改變量。

而同步的本質是通過鎖來實現(xiàn)的。為了實現(xiàn)多個線程在一個時刻同一個代碼塊只能有一個線程可執(zhí)行,那么需要在某個地方做個標記,這個標記必須每個線程都能看到,當標記不存在時可以設置該標記,其余后續(xù)線程發(fā)現(xiàn)已經(jīng)有標記了則等待擁有標記的線程結束同步代碼塊取消標記后再去嘗試設置標記。這個標記可以理解為鎖。

不同地方實現(xiàn)鎖的方式也不一樣,只要能滿足所有線程都能看得到標記即可。如 Java 中 synchronize 是在對象頭設置標記,Lock 接口的實現(xiàn)類基本上都只是某一個 volitile 修飾的 int 型變量其保證每個線程都能擁有對該 int 的可見性和原子修改,linux 內核中也是利用互斥量或信號量等內存數(shù)據(jù)做標記。

除了利用內存數(shù)據(jù)做鎖其實任何互斥的都能做鎖(只考慮互斥情況),如流水表中流水號與時間結合做冪等校驗可以看作是一個不會釋放的鎖,或者使用某個文件是否存在作為鎖等。只需要滿足在對標記進行修改能保證原子性和內存可見性即可。

1 什么是分布式?

分布式的 CAP 理論告訴我們:

任何一個分布式系統(tǒng)都無法同時滿足一致性(Consistency)、可用性(Availability)和分區(qū)容錯性(Partition tolerance),最多只能同時滿足兩項。

目前很多大型網(wǎng)站及應用都是分布式部署的,分布式場景中的數(shù)據(jù)一致性問題一直是一個比較重要的話題?;? CAP理論,很多系統(tǒng)在設計之初就要對這三者做出取舍。在互聯(lián)網(wǎng)領域的絕大多數(shù)的場景中,都需要犧牲強一致性來換取系統(tǒng)的高可用性,系統(tǒng)往往只需要保證最終一致性。

分布式場景

此處主要指集群模式下,多個相同服務同時開啟.

在許多的場景中,我們?yōu)榱吮WC數(shù)據(jù)的最終一致性,需要很多的技術方案來支持,比如分布式事務、分布式鎖等。很多時候我們需要保證一個方法在同一時間內只能被同一個線程執(zhí)行。在單機環(huán)境中,通過 Java 提供的并發(fā) API 我們可以解決,但是在分布式環(huán)境下,就沒有那么簡單啦。

  • 分布式與單機情況下最大的不同在于其不是多線程而是多進程。
  • 多線程由于可以共享堆內存,因此可以簡單的采取內存作為標記存儲位置。而進程之間甚至可能都不在同一臺物理機上,因此需要將標記存儲在一個所有進程都能看到的地方。

什么是分布式鎖?

  • 當在分布式模型下,數(shù)據(jù)只有一份(或有限制),此時需要利用鎖的技術控制某一時刻修改數(shù)據(jù)的進程數(shù)。
  • 與單機模式下的鎖不僅需要保證進程可見,還需要考慮進程與鎖之間的網(wǎng)絡問題。(我覺得分布式情況下之所以問題變得復雜,主要就是需要考慮到網(wǎng)絡的延時和不可靠。。。一個大坑)
  • 分布式鎖還是可以將標記存在內存,只是該內存不是某個進程分配的內存而是公共內存如 Redis、Memcache。至于利用數(shù)據(jù)庫、文件等做鎖與單機的實現(xiàn)是一樣的,只要保證標記能互斥就行。

2 我們需要怎樣的分布式鎖?

可以保證在分布式部署的應用集群中,同一個方法在同一時間只能被一臺機器上的一個線程執(zhí)行。

  • 這把鎖要是一把可重入鎖(避免死鎖)
  • 這把鎖最好是一把阻塞鎖(根據(jù)業(yè)務需求考慮要不要這條)
  • 這把鎖最好是一把公平鎖(根據(jù)業(yè)務需求考慮要不要這條)

有高可用的獲取鎖和釋放鎖功能

獲取鎖和釋放鎖的性能要好

代碼實現(xiàn)

  1. public interface IDistributedLock 
  2.     { 
  3.         ILockResult Lock(string resourceKey); 
  4.         ILockResult Lock(string resourceKey, TimeSpan expiryTime); 
  5.         ILockResult Lock(string resourceKey, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime); 
  6.         ILockResult Lock(string resourceKey, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, CancellationToken cancellationToken); 
  7.         Task<ILockResult> LockAsync(string resourceKey); 
  8.         Task<ILockResult> LockAsync(string resourceKey, TimeSpan expiryTime); 
  9.         Task<ILockResult> LockAsync(string resourceKey, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime); 
  10.         Task<ILockResult> LockAsync(string resourceKey, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, CancellationToken cancellationToken); 
  11.     } 
  12.  
  13.     public interface ILockResult : IDisposable 
  14.     { 
  15.         string LockId { get; } 
  16.         bool IsAcquired { get; } 
  17.         int ExtendCount { get; } 
  18.     } 
  1. class EndPoint:RedLock.RedisLockEndPoint 
  2.     { 
  3.         private readonly string _connectionString; 
  4.         public EndPoint(string connectionString) 
  5.         { 
  6.             _connectionString = connectionString; 
  7.             //139.196.40.252,password=xstudio,defaultDatabase=9 
  8.             var connection = connectionString.Split(','); 
  9.             var dict = new Dictionary<string, string>(); 
  10.             foreach (var item in connection
  11.             { 
  12.                 var keypar = item.Split('='); 
  13.                 if (keypar.Length>1) 
  14.                 { 
  15.                     dict[keypar[0]] = keypar[1]; 
  16.                 } 
  17.             } 
  18.             this.EndPoint = new System.Net.DnsEndPoint(connection[0], 6379); 
  19.             if (dict.TryGetValue("password"out string password)) 
  20.             { 
  21.                 this.Password = password
  22.             } 
  23.             if (dict.TryGetValue("defaultDatabase"out string defaultDatabase) && int.TryParse(defaultDatabase,out int database)) 
  24.             { 
  25.                 RedisDatabase = database
  26.             } 
  27.         } 
  28.     } 
  1. [Export(typeof(IDistributedLock))] 
  2.     class InnerLock : IDistributedLock 
  3.     { 
  4.         private static Lazy<RedLock.RedisLockFactory> _factory; 
  5.  
  6.         static InnerLock() 
  7.         { 
  8.             _factory = new Lazy<RedisLockFactory>(() => new RedisLockFactory(new EndPoint(ConfigurationManager.AppSettings["Redis"])), System.Threading.LazyThreadSafetyMode.ExecutionAndPublication); 
  9.         } 
  10.         public ILockResult Lock(string resourceKey) 
  11.         { 
  12.             return new LockResult(_factory.Value.Create(resourceKey, TimeSpan.FromDays(1))); 
  13.         } 
  14.  
  15.         public ILockResult Lock(string resourceKey, TimeSpan expiryTime) 
  16.         { 
  17.             return new LockResult(_factory.Value.Create(resourceKey, expiryTime)); 
  18.         } 
  19.  
  20.         public ILockResult Lock(string resourceKey, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime) 
  21.         { 
  22.             return new LockResult(_factory.Value.Create(resourceKey, expiryTime, waitTime, retryTime)); 
  23.         } 
  24.  
  25.         public ILockResult Lock(string resourceKey, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, CancellationToken cancellationToken) 
  26.         { 
  27.             return new LockResult(_factory.Value.Create(resourceKey, expiryTime, waitTime, retryTime, cancellationToken)); 
  28.         } 
  29.  
  30.         public async Task<ILockResult> LockAsync(string resourceKey) 
  31.         { 
  32.             var result = await _factory.Value.CreateAsync(resourceKey, TimeSpan.FromDays(1)); 
  33.             return new LockResult(result); 
  34.         } 
  35.  
  36.         public async Task<ILockResult> LockAsync(string resourceKey, TimeSpan expiryTime) 
  37.         { 
  38.             var result = await _factory.Value.CreateAsync(resourceKey, expiryTime); 
  39.             return new LockResult(result); 
  40.         } 
  41.  
  42.         public async Task<ILockResult> LockAsync(string resourceKey, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime) 
  43.         { 
  44.             var result = await _factory.Value.CreateAsync(resourceKey, expiryTime, waitTime, retryTime); 
  45.             return new LockResult(result); 
  46.         } 
  47.  
  48.         public async Task<ILockResult> LockAsync(string resourceKey, TimeSpan expiryTime, TimeSpan waitTime, TimeSpan retryTime, CancellationToken cancellationToken) 
  49.         { 
  50.             var result = await _factory.Value.CreateAsync(resourceKey, expiryTime, waitTime, retryTime, cancellationToken); 
  51.             return new LockResult(result); 
  52.         } 
  53.     } 
  54.  
  55.     class LockResult : ILockResult 
  56.     { 
  57.         private IRedisLock _lock; 
  58.         public LockResult(IRedisLock redisLock) 
  59.         { 
  60.             _lock = redisLock; 
  61.         } 
  62.  
  63.         public string LockId => _lock.LockId; 
  64.  
  65.         public bool IsAcquired => _lock.IsAcquired; 
  66.  
  67.         public int ExtendCount => _lock.ExtendCount; 
  68.  
  69.         public void Dispose() 
  70.         { 
  71.             _lock.Dispose(); 
  72.         } 
  73.     } 

 

責任編輯:武曉燕 來源: UP技術控
相關推薦

2021-07-30 00:09:21

Redlock算法Redis

2019-06-19 15:40:06

分布式鎖RedisJava

2022-03-08 07:22:48

Redis腳本分布式鎖

2017-10-24 11:28:23

Zookeeper分布式鎖架構

2017-04-13 10:51:09

Consul分布式

2021-09-17 07:51:24

RedissonRedis分布式

2022-06-16 08:01:24

redis分布式鎖

2023-08-21 19:10:34

Redis分布式

2022-01-06 10:58:07

Redis數(shù)據(jù)分布式鎖

2019-02-26 09:51:52

分布式鎖RedisZookeeper

2021-11-01 12:25:56

Redis分布式

2022-10-27 10:44:14

分布式Zookeeper

2024-02-20 09:50:02

Redis分布式

2024-01-09 09:27:08

RedLock分布式鎖Redis

2021-07-16 07:57:34

ZooKeeperCurator源碼

2022-03-08 15:24:23

BitMapRedis數(shù)據(jù)

2023-03-01 08:07:51

2024-10-07 10:07:31

2020-11-16 12:55:41

Redis分布式鎖Zookeeper

2022-09-19 08:17:09

Redis分布式
點贊
收藏

51CTO技術棧公眾號