理想汽車 1 月交付 5379 輛車,宣佈在上海設立研發中心
技術標籤:javaredisredissonredisredisson分散式java資料庫
場景:一家網上商城做商品限量秒殺。
1 單機環境下的鎖
將商品的數量存到Redis中。每個使用者搶購前都需要到Redis中查詢商品數量(代替mysql資料庫。不考慮事務),如果商品數量大於0,則證明商品有庫存。然後我們在進行庫存扣減和接下來的操作。因為多執行緒併發問題,我們不得不在get()方法內部使用同步程式碼塊。這樣可以保證查詢庫存和減庫存操作的原子性。
package springbootdemo.demo.controller; /* * @auther 頂風少年 * @mail [email protected]
* @date 2020-01-13 11:19 * @notify * @version 1.0 */ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisLock { @Autowired private RedisTemplate<String, String> redisTemplate; @GetMapping(value = "buy") public String get() { synchronized (this) { String phone = redisTemplate.opsForValue().get("phone"); Integer count = Integer.valueOf(phone); if (count > 0) { redisTemplate.opsForValue().set("phone", String.valueOf(count - 1)); System.out.println("搶到了" + count + "號商品"); }return ""; } } }
2 分散式情況下使用Redis鎖。
但是由於業務上升,併發數量變大。公司不得不將原有系統複製一份,放到新的伺服器。然後使用nginx做負載均衡。為了模擬高併發環境這裡使用了 Apache JMeter工具。
很明顯,現在的執行緒鎖不管用了。於是我們需要換一把鎖,這把鎖必須和兩套系統沒有任何的耦合度。
使用Redies的API如果key不存在,則設定一個key。這個key就是我們現在使用的一把鎖。每個執行緒到此處,先設定鎖,如果設定鎖失敗,則表明當前有執行緒獲取到了鎖,就返回。最後我們為了減庫存和其他業務丟擲異常,而沒有釋放鎖。把釋放鎖的操作放到了finally程式碼塊中。看起來是比較完美了。
package springbootdemo.demo.controller;
/*
* @auther 頂風少年
* @mail [email protected]
* @date 2020-01-13 11:19
* @notify
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisLock {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping(value = "buy")
public String get() {
Boolean phoneLock = redisTemplate.opsForValue().setIfAbsent("phoneLock", "");
if (!phoneLock) {
return "";
}
try{
String phone = redisTemplate.opsForValue().get("phone");
Integer count = Integer.valueOf(phone);
if (count > 0) {
redisTemplate.opsForValue().set("phone", String.valueOf(count - 1));
System.out.println("搶到了" + count + "號商品");
}
}finally {
redisTemplate.delete("phoneLock");
}
return "";
}
}
3 一臺服務宕機,導致無法釋放鎖
如果try中丟擲了異常,進入finally,這把鎖依然會釋放,不會影響其他執行緒獲取鎖,那麼如果在finally也丟擲了異常,或者在finally中服務直接關閉了,那其他的服務再也獲取不到鎖。最終導致商品賣不出去。
package springbootdemo.demo.controller;
/*
* @auther 頂風少年
* @mail [email protected]
* @date 2020-01-13 11:19
* @notify
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisLock {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping(value = "buy")
public String get() {
int i = 0;
Boolean phoneLock = redisTemplate.opsForValue().setIfAbsent("phoneLock", "");
if (!phoneLock) {
return "";
}
try {
String phone = redisTemplate.opsForValue().get("phone");
Integer count = Integer.valueOf(phone);
if (count > 0) {
i = count;
redisTemplate.opsForValue().set("phone", String.valueOf(count - 1));
System.out.println("搶到了" + count + "號商品");
}
} finally {
if (i == 20) {
System.exit(0);
}
redisTemplate.delete("phoneLock");
}
return "";
}
}
4 給每一把鎖加上過期時間
問題就出現在如果出現意外,這把鎖無法釋放。這裡我們在引入Redis的API,對key進行過期時間的設定。這樣如果拿到鎖的執行緒,在任何情況下沒有來得及釋放鎖,當Redis的key時間到,也會自動釋放鎖。但是這樣還是存在問題
如果在key過期後,鎖釋放了,但是當前執行緒沒有執行完畢。那麼其他執行緒就會拿到鎖,繼續搶購商品,而這個較慢的執行緒則會在執行完畢後,釋放別人的鎖。導致鎖失效!
package springbootdemo.demo.controller;
/*
* @auther 頂風少年
* @mail [email protected]
* @date 2020-01-13 11:19
* @notify
* @version 1.0
*/
import javafx.concurrent.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
@RestController
public class RedisLock {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping(value = "buy")
public String get() {
Boolean phoneLock = redisTemplate.opsForValue().setIfAbsent("phoneLock", "", 3, TimeUnit.SECONDS);
if (!phoneLock) {
return "";
}
try {
String phone = redisTemplate.opsForValue().get("phone");
Integer count = Integer.valueOf(phone);
if (count > 0) {
try {
Thread.sleep(99999999999L);
} catch (Exception e) {
}
redisTemplate.opsForValue().set("phone", String.valueOf(count - 1));
System.out.println("搶到了" + count + "號商品");
}
} finally {
redisTemplate.delete("phoneLock");
}
return "";
}
}
5 延長鎖的過期時間,解決鎖失效
問題的出現就是,當一條執行緒的key已經過期,但是這個執行緒的任務確確實實沒有執行完畢,這個交易沒有結束。但是鎖沒了。現在我們必須對鎖的時間進行延長。在判斷商品有庫存時,第一時間建立一個執行緒不停的給key續命,
防止key過期。然後在交易結束後,停止定時器,釋放鎖。
package springbootdemo.demo.controller;
/*
* @auther 頂風少年
* @mail [email protected]
* @date 2020-01-13 11:19
* @notify
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
@RestController
public class RedisLock {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping(value = "buy")
public String get() {
Boolean phoneLock = redisTemplate.opsForValue().setIfAbsent("phoneLock", "", 3, TimeUnit.SECONDS);
if (!phoneLock) {
return "";
}
Timer timer = null;
try {
String phone = redisTemplate.opsForValue().get("phone");
Integer count = Integer.valueOf(phone);
if (count > 0) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
redisTemplate.opsForValue().set("phoneLock", "", 3, TimeUnit.SECONDS);
}
}, 0, 1);
redisTemplate.opsForValue().set("phone", String.valueOf(count - 1));
System.out.println("搶到了" + count + "號商品");
}
} finally {
if (timer != null) {
timer.cancel();
}
redisTemplate.delete("phoneLock");
}
return "";
}
}
6 使用Redisson簡化程式碼
在步驟5我們的程式碼已經很完善了,不會出現高併發問題。但是程式碼確過於冗餘,我們為了使用Redis鎖,我們需要設定一個定長的key,然後當購買完成後,將key刪除。但為了防止key提前過期,我們不得不新增一個執行緒執行定時任務。下面我們可以使用Redissson框架簡化程式碼。getLock()方法代替了Redis的setIfAbsent(),lock()設定過期時間。最終我們在交易結束後釋放鎖。延長鎖的操作則有Redisson框架替我們完成,它會使用輪詢去檢視key是否過期,
在交易沒有完成時,自動重設Redis的key過期時間
package springbootdemo.demo.controller;
/*
* @auther 頂風少年
* @mail [email protected]
* @date 2020-01-13 11:19
* @notify
* @version 1.0
*/
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
@RestController
public class RedissonLock {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Autowired
private Redisson redisson;
@GetMapping(value = "buy2")
public String get() {
RLock phoneLock = redisson.getLock("phoneLock");
phoneLock.lock(3, TimeUnit.SECONDS);
try {
String phone = redisTemplate.opsForValue().get("phone");
Integer count = Integer.valueOf(phone);
if (count > 0) {
redisTemplate.opsForValue().set("phone", String.valueOf(count - 1));
System.out.println("搶到了" + count + "號商品");
}
} finally {
phoneLock.unlock();
}
return "";
}
}